Post Reply 
Python: TEXTOUT_P, CHOOSE, INPUT here is how it works 22.jan addded a TextOut functio
12-20-2021, 10:17 PM (This post was last modified: 01-24-2022 10:51 PM by Guenter Schink.)
Post: #1
Python: TEXTOUT_P, CHOOSE, INPUT here is how it works 22.jan addded a TextOut functio
edit 22.jan added a TxtOut function for easy interfacing with HOME post#4 /edit

edit 17.jan2022 Unfortunately it turned out that the STRING handling is a little bit more complicated than described in this post. I'll do a correction in post #3 /edit

Python on the Prime is quite impressive by its speed. It's a pity it has a lot of bugs. Let's hope they will be addressed in future releases.

While Python is really fast, it lacks some convenient functions like CHOOSE or (configurable) INPUT. But why not simply use what is available in HOME?

The key is the function eval() of the module hpprime. Once the principle is understood we have almost full access to what the HOME environment provides.
eval() simply passes a string to the interpreter in the HOME environment. Therefore we have to ensure to pass on a syntactically correct statement.

All examples have < from hppdir import * > as a prerequisite.

A simple example is the "WAIT" function of HOME. eval("WAIT") will wait until a key is pressed.

But consider you wish to wait for an arbitrary duration you've stored in var "a".
Code:
    10 a=5
    20 eval('Wait('+str(a)+')')
does it, by simply concatenating strings. In the following examples it is perhaps necessary to pass on real strings as arguments. That can be done by enclosing the entire eval() string with single quotes and the string that is an argument with double quotes. Therefore I recommend always using the single quotes [SHIFT () ] around what is called with eval().

Why use TEXTOUT_P?

The built-in function "textout" is rather limited as you neither can choose size nor background color. TEXTOUT_P provides you with all of that
Code:
 eval('TEXTOUT_P("Günter",G0,50,120,1,#FF0000h,100,RGB(192,192,192))')
gives that text with a very small red font on a grey background. All options (you'll find in the help) for TEXTOUT_P are available. Remember all arguments can be passed as variables through string concatenation.

So far these examples work in one direction. But for others we need something back from the HOME environment.

CHOOSE
Wouldn't it be nice to have that comfort in the Python environment too? This is how it works, e.g.
Code:
1 eval(‘CHOOSE(N,"Our next HHC"," Nashville","Reno","San José" ')
2 N=eval('N')
3 if   N==1:  print("Gene")
4 elif N==2: print("David")
5 elif N==3 :print (Monte?)
6 elif N==0: print(“no meeting?”)
In line 1 the CHOOSE function stores the number of the selected item into the HOME variable "N". In line 2 eval() retrieves "N" from the HOME environment and stores it into "N" of the Python environment. Thereafter your program may take appropriate action. Cool isn't it?

Again use of variables is available by string concatenation.

INPUT
Have a look at the syntax, and have fun making your own experiments.

Variables management:
Maybe you don't want to interfere with the common variables of the HOME environment. Then the AVars function comes in handy. Guess you wish to synchronize your variables in both environments you could do:
Code:
 MyVar=eval('AVars(“MyVar”)') #to store what's in HOME into the Python variable. 
#Vice versa: 
eval('AVars("MyVar"):='+str(MyVar)) #from Python to HOME
AVars need to be created beforehand. That can be done either directly in the HOME environment or as an initial procedure in your Python program.

Some examples:
edit: ref string handling refer to post #3 /edit
Code:
MyText=”some ranting” ;   eval('AVars("MyText"):=’ + str(MyText))
MyList  = [[1,2,3],[4,5,6]] ;  eval('AVars("MyList"):=' + str(MyList))
MyCompl = complex(5,6) ;              eval('AVars("MyCompl"):='+str(MyCompl) )
The variable names do not necessarily be the same in either Python or HOME, I simply did this for better orientation.

This way these variables are created in both environments with identical contents. Well, the delimiters of lists in Python [] are different from those in HOME {}. But it doesn't affect the function (I think). There is one caveat with lists at least. HOME list seem to only accept max 2 dimensional lists. But anyway there are better ways in Python to store values (I/O). These methods presented have their value for the exchanges described.



Comments, questions welcome

Günter
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
Python: TEXTOUT_P, CHOOSE, INPUT here is how it works 22.jan addded a TextOut functio - Guenter Schink - 12-20-2021 10:17 PM



User(s) browsing this thread: 1 Guest(s)