def show(a,isP): #by: D@CC #date: 2021FJun26 #updated: 2022BFeb23 # # NB beware: the variable name must be in quotes # # This is a python debugging aide. # Often, when debugging, it is very helpful to list the type # and value of variables at various stages of execution. # Even listing a comment string at crucial points is of help # This routine is designed to be turned on/off near the beginning # of the program. If arg2 is 0, no variables will be shown # unless an error condition exists (e.g. a variable is not defined). # if arg2 is 0, the program will not print any extraneous output. # If arg2 is 1, the program will print all debugging information. # Of course putting a "#" in front of a show() statement will # suppress it. Note that "continueDoh=0" or "pass" can be # inserted in an empty clause of an IF statement. # #tested ok # stored in /home/pi/Desktop/Python_new/ix_py_test/ # stored in C:/ . . . /155/ # useful for debugging Python code # prints the name of the class of "a", "a" itself and value # precede by p=0 (at top of code) to suppress showing valid variables # it will still do the error checking and report errors #these all are analysed correctly #abcd="a123b" #abcd=25 #abcd=0b110 #abcd=0x101 #abcd=25.3 #abcd=25E5 #0.1 #"0.1.2.3" !doh #abcd="-h" #blah=["a","b","c"] #abcd=True #"None" #abcd="Null" #"-h" #"Null" #"xyz" when not yet defined # eg blah=123 # p=1 #set p=0 to suppress printing # show("blah",p) # prints blah: 123 # Note without "try" produced system error if variable blah is not defined # abcd= 25 or "a123b" if a=="-h": # show help info print(' for help show("-h",p)') print(' usage: show("varA",p)') print(' after p=1; varA=34') print(" prints varA is 34") print(" and") print(' show("varA",p)') print(' after p=0; varA=34') print(" prints nothing") print(" and") print(' show("0.1.9",p)') print(' after p=0 or p=1') print(" prints 0.1.9 is 'doh!'") print(" and") print(' show("blah[1]",p)') print(' after p=1, blah=["a","b","c"]') print(" prints blah[1] is "+'"b"') if True: print(" only show in final version") print("if this fails, copy and use:") print("def show(a,isP):") print(" vA=eval(a); tA=type(a);") print(" if tA in [str]: vA='"'+vA+'"' ") print(' print(a+":",tA,vA)') print("#def end show") #if end return True else: # Normal processing begins here aIn=a #aIn "abcd" or "abcd" #valueA="3.14159"+" thon" valueA=a #valueA is either "abcd" or "abcd" #print("valueA:"+valueA) try: valueEval=eval(a) # eval(an int) creates isValid=True except: valueEval="Doh!" typeVar="" isValid=False #try end #debugging info #print("isValid:",isValid) #print("valueEval:",valueEval) valueE=valueEval # #valueE 25 or "a123b" #print("valueE:",valueE) #print("testing str(valueE):"+str(valueE)) #print("aIn:",aIn) #print("type(a):"+str(type(a))) #print("str(type(valueE)):"+str(type(valueE))) if isValid : typeVar=str(type(valueE)) #print("typeVar"+typeVar) #if end isPrinted=True if isPrinted==False: # help continueDoh=0 #if end isOK=False #return valueA+": = "+str(35) #print("enterring finally") isTypeStr=True if isTypeStr: typeA=type(valueA) if (isP==0)==False: if typeA in [str]: #print(" "+a+":",typeA,'"'+valueA+'"') continueDoh=0 else: #print(" "+a+":",typeA,valueA) valueA=aIn #print("Its value is:",aIn) #end if #normal return is here if isP==1 : # always show an answer areturn = typeVar+' '+aIn+' is "'+str(valueE) + '"' print(areturn) return #if end if isP==0 : #isP is 0 # only if error if typeVar=="": areturn = typeVar+' '+aIn+' is "'+str(valueE) + '"' print(areturn) return else: continueDoh=0 return #if end if isP==2 : print("isP:",isP) #if end else: print("not isTypeStr") continueDoh=0 #end if print("ERROR show() needs a str as arg1") # source: https://stackoverflow.com/questions/9437726/how-to-get-the-value-of-a-variable-given-its-name-in-a-string #end if return #if end # works: abcd=25 # works: abcd="a123b" # stored in G:\BUsByH\2023LDec04\_Fla95S032G\pi\Desktop\Python\PiIX\show_py.py #end def show()