#::::::::::textpak=>inputWto,py # first python routine in IX_package_py_py.txt #ix_py.py # by D@CC # on 2022CMar12 ix_py.__init__= "ix_py.py is this library of ix functions by David@ColeCanada.com" inputWto.__init__="inputWto() allows timeOut secs to input a string" f3.__init__=" f3 is the function to print any number in a -999999.999 format" def inputWto(strPrompt,timeOut,mp3Prompt): # purpose: to read a string from the keyboard # Parm1: char str to be used as a prompt # if "" then a "?" is used # Parm2: N the timeOut period (in seconds) # set to 3 sec if <1 or >120 # Parm3: mp3 audio to play while waiting # can be audio/typing_l.mp3 # (not available yet) # returns: # Return1: the string that was read) or "" null String # Return2 returns True (else False if timed out) # NOTE: The "select" function waits for input efficiently # I presume that it sleeps while waiting for timeWait # NOTE: It ONLY works if you click at the # bottom of the RUN window before your keystrokes # source: www.pymotw.com/2/select if strPrompt=="-h" : print("inputWto is a function that accepts keyboard input from") print(" a user") print(" e.g.") print(' reply,isNull =inputWto("prompt",3,"name.mp3"') print if mp3Prompt=="" : pass else : pass # the following sounds are played before the # input is requested (not during) Why??? # uncomment the following to "sound" the typing prompt #control_audioTV("typing_3.mp3") #end if prompt=strPrompt #print("The prompt is supposed to be:") if type(timeOut)==str: timeWait=int(timeOut) if type(timeOut)==int: timeWait=timeOut if ((timeWait<1) or (timeWait>120)): timeWait=7 if prompt=="" : pass #leave an empty prompt unchanged else: # show them how long they have to respond strippedTimeWait=str(timeWait) prompt=prompt+"("+strippedTimeWait+"s):" #end if print(prompt, end='') #accept the response on the same line progName="in function inputWto.py" #by D@CC on 2021BFeb17 tested successfully # changed 2021GJul07 and fully tested # using Thonny on a Raspberry Pi 4B # 2 instances of Thonny cannot run simultaneously # using Terminal pi@raspberrypi:~ $ python3 Wait_Select_Stdin.py #This program waits N seconds for the operator to enter a string. #This program runs (but Times Out) if no Keyboard is plugged in. #This program runs using the RDC (Remote Desktop Connection) keyboard. #print("Click in the RUN window below, then. . . .") #print("You have", timeOut," seconds to type any string (then hit Enter).") rlist, wlist, xlist = select([sys.stdin], [], [], timeWait) # the two [] could contain other input lines e.g. serial ports #print("rlist:",rlist) if rlist: # the thread goes through here if a keyboard is active # meaning it exists but nothing has been typed yet # If nothing is typed, after timeWait seconds # rlist will be empty. The programmer should print # the prompt phrase here #rlist contains garbage but . . . #print("This string was returned from select():",rlist) isKeyBoardActive=True if len(prompt)==0 : prompt="?" bStr=input("") if len(bStr)==0: isKeyBoardActive=False #print("This string was typed on the Key-Board:",bStr) #Enter produced something like this in rlist # else: #print("in func. "select" it Timed Out") isKeyBoardActive=False bStr="" #if end #if isKeyBoardActive: #print("bStr:","'",bStr,"'") #if end #print("program will continue from here.") return bStr, isKeyBoardActive #From: # https://stackoverflow.com/questions/6179537/python-wait-x-secs-for-a-key-and-continue-execution-if-not-pressed # PYthon3 Notes re select() # https://docs.python.org/3/library/select.html #def end inputWto Desktop/Python/inputWto.py #/inputWto.py Desktop/Python/inputWto.py #def end inputWto.py #::::::::::textpak=>f3.py # f3() formats as f999999.999 where f means float (See endnotes) # MicroPython doesn't process complex numbers import re # to check for regular expressions in a string def f3(x): isDone=False xFloat=3.14159 xType="q" xReturn="" #print("into f3 xReturn:"+xReturn) # process a string #if isinstance(x,(float,int,str,list,dict,tuple)): if isinstance(x,(float,int,str)): if isinstance(x,str): if x.endswith("jJ"): isJend=True else: isJend=False #print("isJend:",isJend) #if(x.endswith("j)") or (x.endswith("J)"): #if((x.endswith("j")) or ((x.endswith("J")): if isJend: #almost complex but without the "(" and ")" xReturn="c" + "("+c+")" xType="c" isDone=True else: #not complex try: xFloat=float(x) xType="f" isDone=True except: xReturn="s"+x isDone=True #end try #end if # process an integer value elif isinstance(x,int): xFloat=float(x) xType="i" isDone=True # process a float value elif isinstance(x,float): xFloat=x xType="f" isDone=True #end if instances of int,float,str, else: #presume boolean if(bool(x)==1): xFloat=float(1) xType="b" isDone=True #end if if(bool(x)==0): xFloat=float(0) xType="b" isDone=True #end if #end of bool processing #end if #process unknown type if isDone==False: #nothing was recognized xReturn="u??????.???" else: if xReturn=="": #create something to return xReturn=xType + str(xFloat) else: #something was created to be returned #doh="doh" pass #end if #end if #print("xReturn:"+xReturn) return xReturn #def end f3.py #::::::::::textpak=>printSho.py def printSho() : print("This prints out the sho function.") print("so it can be 'copied' into the main program.") print("This function cannot be imported. Doh!") print("def sho(a,isP):") print(" vA=eval(a)") print(" tA=type(vA);") print(" print(tA)") print(" chDubl=chr(65)") print(" #chDub is the double quote character") print(" if tA in ['str']: vA=chDubl+vA+chDubl") print(" print(a"+":"+"tA,vA)") print("#end def show") #def end printSho.py #::::::::::textpak=>textpack.py import sys # load the system module marker = ':'*10 + 'textpak=>' # hopefully unique separator def pack( ): for name in sys.argv[1:]: # for all command-line arguments input = open(name, 'r') # open the next input file print marker + name # write a separator line print input.read( ), # and write the file's contents if __name__ == '__main__': pack( ) # pack files listed on cmdline #def end textpack.py # last python routine in IX_package_py_py.txt