HP Forums
Python: TEXTOUT_P, CHOOSE, INPUT here is how it works 22.jan addded a TextOut functio - Printable Version

+- HP Forums (https://www.hpmuseum.org/forum)
+-- Forum: HP Calculators (and very old HP Computers) (/forum-3.html)
+--- Forum: HP Prime (/forum-5.html)
+--- Thread: Python: TEXTOUT_P, CHOOSE, INPUT here is how it works 22.jan addded a TextOut functio (/thread-17833.html)



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

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


RE: Python: TEXTOUT_P, CHOOSE, INPUT here is how it works - tcab - 12-29-2021 07:51 AM

Thank you Guenter for these continued, amazing discoveries about Python on the Prime!


RE: Python: TEXTOUT_P, CHOOSE, INPUT here is how it works - Guenter Schink - 01-17-2022 08:59 PM

Passing strings as such from the Python environment to the HOME environment is straightforward:

Prerequisite: from hppprime import *

Example:
Code:
00 from hppprime import *
01 eval(' AVars("Text"):="This is my text" ')
works flawlessly

but consider this where I try to pass the string as a variable

Code:
00 from hppprime import *
01 text= "This is my text"
02 eval(' AVars("Text"):=' + text )

This doesn't work! Why? The reason is that the content of the variable text isn't enclosed in quotes (or double quotes). Thus it is interpreted as a variable name by the HOME environment ==> Syntax Error

Solution: enclose the variable name text with double quotes. here is how it works:

Code:
00 from hppprime import *
01 text= "This is my text"
02 eval(' AVars("Text"):=' + ' " '+ text + ' " ' )

The spaces between the quotes and double quote are here only to show the two different kinds of quotes. Practically you'd omit them as they actually add blanks to the string.

In order to make life easier it is suitable to define a function for this

Code:
00 from hppprime import *

def string(text):
        return '"' + text + '"'    # remember ' " ' is how those quotings look with blanks

01 text= "This is my text"
02 eval(' AVars("Text"):=' + string(text) )

This function is specifically useful for the <TEXTOUT_P> function in the HOME environment as this function is much more powerful than the <textout> function in the Python environment. The next release of my Mandelbrot Explorer will demonstrate some use of it.

I hope:

a) This is understandable
b) of use for someone

Günter


RE: Python: TEXTOUT_P, CHOOSE, INPUT here is how it works Correction 17.Jan 2022 - Guenter Schink - 01-23-2022 11:16 PM

I've just published a new Version of my Mandelbrot Explorer.
included is a handy routine for use of HOME TEXTOUT_P. Here is the code snipped
Code:

from hpprime import *

def string(arg):                #create a string understood by HOME environment
    return '"' + str(arg) +'"'
    
def RGB(r,g,b):                 #make RGB understood by HOME environment
    return (r*256+g)*256+b
    
def strip(arg):                 #strip parantheses from a tuple converted to string
    return str(arg)[1:-1]

def TxtOut(t,x,y,f,c,*args):    #provide an easy to handle interface to TEXTOUT_P in HOME
    if len(args)>2:             #Syntax: TxtOut(string,x,y,font,color[,max_width[,background color]]
       raise(SyntaxError("too many arguments"))
    if True in [isinstance(i,list) for i in t]:
       raise(TypeError("can't convert 'list' object to str implicitly"))
    parms=(int(x),int(y),int(f),int(c))
    if len(args)>0:parms+=(int(args[0]),) #add max_width
    if len(args)>1:parms+=(int(args[1]),) #add background color
    return eval('TEXTOUT_P('+string(t)+','+strip(parms)+')')
Perhaps it is of use for someone

Günter


RE: Python: TEXTOUT_P, CHOOSE, INPUT here is how it works Correction 17.Jan 2022 - jfelten - 01-24-2022 06:34 PM

Supposedly, one of the recent firmware versions allows you to also change the formatting of the text (bold, italic, alignment, monospace, etc.), although I haven't been able to get that to work at all. Would you be able to take a look at that and explain how it works?


RE: Python: TEXTOUT_P, CHOOSE, INPUT here is how it works Correction 17.Jan 2022 - Guenter Schink - 01-24-2022 10:44 PM

(01-24-2022 06:34 PM)jfelten Wrote:  Supposedly, one of the recent firmware versions allows you to also change the formatting of the text (bold, italic, alignment, monospace, etc.), although I haven't been able to get that to work at all. Would you be able to take a look at that and explain how it works?

I had a look at it and quite frankly I couldn't decipher how that was supposed to work. Here is how it is described.

Quote:A new form of TEXTOUT:

TEXTOUT(object_to_print, [G], x, y, {["2D"], [{print out flags as in string}], [font], [c1], [width], [c2], flags}

Allows 2D printing, specification of the transformation from object to string (as in the STRING function), but also the “flag” parameter, which is a bit field allows to select various options:

elipsis= (flags&1)!=0;
NoPadding= (flags&2)!=0;
ClipLastChar= (flags&4)!=0;
centered= (flags&8)!=0;
rightaligned= (flags&16)!=0;
PointIsNotTopLeft= (flags&32)!=0;
Underline= (flags&64)!=0;
StrikeThrough= (flags&128)!=0;
Bold= (flags&256)!=0;
italic= (flags&512)!=0;
mono= (flags&1024)!=0;
EraseFullWidth= (flags&2048)!=0;

PRINT2D prints in 2D and allows some selection of format

print(object, [font], [color], flags(bold:1, underscore:2, strikethrough:4, centered: 8, right:16;)

note that some of the flags (bold, underscore, strike) only work when the object is a string).

If someone could decipher it, with working examples I'd be happy. And could give it a try

Günter


RE: Python: TEXTOUT_P, CHOOSE, INPUT here is how it works 22.jan addded a TextOut functio - jfelten - 01-25-2022 02:46 AM

Wait a minute... apparently this feature was removed in firmware version 2.1.14603. From what I recall, it was introduced in version 2.1.14596, which is supposedly a beta release. So, this is most likely an unfinished beta feature and, while useful, should probably not concern us just quite yet.


RE: Python: TEXTOUT_P, CHOOSE, INPUT here is how it works 22.jan addded a TextOut functio - Guenter Schink - 01-25-2022 09:44 PM

(01-25-2022 02:46 AM)jfelten Wrote:  Wait a minute... apparently this feature was removed in firmware version 2.1.14603. From what I recall, it was introduced in version 2.1.14596, which is supposedly a beta release. So, this is most likely an unfinished beta feature and, while useful, should probably not concern us just quite yet.

I have no idea where to look for these "enhanced features"