show_py_txt.html (in 155.html)

def show(a,isP):
    #by: D@CC
    #date: 2021FJun26
    #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 "a", its class 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
    
    # 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
    valueA="3.14159"+" thon"
    if type(a) in [str]:
        isOK=True
        try:
            valueA=eval(a)
        except:
            isPrinted=False
            if(a==""):
                isPrinted=True
                print("ERROR: arg is Null")
            if(a is None):
                # doesn't happen . . . needs a str argument
                isPrinted=True
                print("ERROR: arg is None")               
            if isPrinted==False:
                if a=="/?":
                    print(' /? e.g. show("varA",p)')
                    print(' after p=1; varA=34')
                    print(" prints varA:  34")
                    print("   and")
                    print(' show("varA",p)')
                    print(' after p=0; varA=34')
                    print(" prints nothing")
                    print("   and")
                    print(' show("blah[1]",p)')
                    print(' after p=1, blah=["a","b","c"]')
                    print(" prints blah[1]:  "+'"b"')
                    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('#end def show")
                else:
                    print("ERROR:"+a+" is not defined")
                #end if
            #end if
            isOK=False
            return valueA
        finally:
            if isOK:
                typeA=type(valueA)
                if (isP==0)==False:
                    if typeA in [str]:
                        print(" "+a+":",typeA,'"'+valueA+'"')
                    else:
                        print(" "+a+":",typeA,valueA)
                    #end if
                return valueA
            #end if
    else:
        print("ERROR show() needs a str arg")
        # source: https://stackoverflow.com/questions/9437726/how-to-get-the-value-of-a-variable-given-its-name-in-a-string
    #end if        
    return valueA
#end def show()

Source: contains show_py.py by D@CC on 2021FJun26
/show_py_txt.html