# pi_IX_function_library (piIX_libr.py)
# by D@CC on 2021BFeb12 
# ver f3_pyD uses isDone() not isNotDone()
#
# f3(x)	converts the numerical variable to a f6.3 string
#for printing. Note: decimal points should ALL line up
# resolution of int, float are limited by device & Python vsn
#  by D@CC 2021EMay31 (c)ich180r2
#
# f"abc{floatA}." works in 3.7 but not yet in MicroPython
# print(("hi {floatA} bye"))
# strLine="hi %a. bye" % (floatA)
#python function l
def f3(x):
# prepares to print all numbers using format "-99999.999"  i.e. 6f3
# accepts types:int,str,float,complex,bool
# requires strToComplex(xStr)
# inspiration is to not print out more than 3 digits to the right of the decimal point
# returns S999999.999 after chopping off extra left and extra right digits
# stored in pi_IX_library.py   IX means "DC" if you connect the top & bottom serifs of the font letters
# errors appear as "??????.???" or ">999999.999" or "<-999999.999"
# by D@CC on 2021BFeb12 (c) ich180r2

    print("debug: starting f3",x)
    isError= False
    isDone=True
    floatX=x
    strFloatX=str(floatX)
#    #strFloat= f"abc{floatA}." works in 3.7 but not in MicroPython
    print(" cb means {  c means , P means pc sign")
    print("f 1. strFloatX:",strFloatX," bye")
    print(("  2. cb floatX cb bye"))
    strFloat="hi %s. bye" % (floatX)
    print("    strFloat= q hi pc s. bye q pc (floatX)")
    print("P 3. strFloat: c strFloat c  bye")
    pyVer="MPg" #MicroPython generic" code C testing
    #pyVer="Py3" #Python version 3 code B works
    
    #returns complex numbers as type "u" for sample & time
    # where "u" means u16 x1000
    # Note u12 with 4 bits of 0 on right = u16
    # u16 can even have more digits to the left
    # type "u" is compatible with Pico ADC raw readings
    # even allowing for more precision in the future.
    # To get Pico ADC reading; divide "u" value by 16000
    # i.e. lowest non-zero Pico ADC reading is 16000 in "u"
    # which converts to the actual ADC reading of "1"
    # this will work with future "16 bit" ADC on any device
    # & allowing more bits to the right of the decimal pnt.
    # 
    #test complex
    if type(x)=="complex" :
        xUse=x
        isDone=True
    #end if

    xInt=int(x)
    print("at error, xInt:",xInt)
    if  (abs(xInt)>999999) :
        isError=True
        str_floatX = ">999999.999"
        #print("line  27: strFloatX",strFloatX)
    else:
        #print("xInt:",xInt)
        floatX = 1.0 * xInt
        #strFloatX=f'{floatX:.3f}'
        #if pyVer=="Py3":strFloatX=f"{floatX:.3f}"
        if pyVer=="MPg":strFloatX="%s" % floatX 
        print("line  54: strFloatX",strFloatX)
    #end if
    isDone=True
    
    #fix too big but negative
    if  (abs(xInt)>999999) :
        isError=True
        str_floatX="<-999999.999"
    else:
        floatX= 1.0*xInt
        #if pyVer="Py3": strFloatX=f'{floatX:.3f}'
        if pyVer=="MPg": strFloat="%s" % floatX
    #end if
    isDone=True
    
    #float, str and int
    print("at 85: before xUse=0")
    xUse=x    #to define it
    print("type(x),x is",type(x),"=",x)
    
    # check if Float
    if (isDone==False):
        print("76: check if Float")
        isFloat=False
        isFloat=isinstance(x,float)
        #strType=type(x)
        print("isFloat?",isFloat)
        if (isFloat):
            print("82: x is type float")
            print("before xUse=x")
            xUse=x
        elif (type(x)=="str") :
            print("86 x is type str")
            print("should call strToComplex here")
            #xUse=parseFloat(x)
            xUse=float(x)
            print("str")
        elif (type(x)=="int") :
            print("92: x is type int")
            xUse=1.0*x
            print("int")
        #end if
        
        # fix magnitude of xUse
        print("xUse:",xUse)
        print("x type is known, test magnitude of xUse which is x")
        absX=abs(xUse)
        print("absX:",absX)
        if (abs(xUse)>999999.) and (xUse>0) :
            print("103: too big")
            isError=True
            str_floatX="> 999999.000"
        else:
            floatX= 1.0*xUse
            strFloatX=str(floatX)
        print("strFloatX:",strFloatX)
        
        #test too low and negative
        isDone=False
        if  (abs(xUse)>999999) and (xUse<0.) :
            isError=True
            str_floatX="<-999999.000"
        else:
            floatX= 1.0*xUse
            isDone=True
        #isDone=True
    #if end
    str_floatX=str(floatX)
    print("str_floatX:",str_floatX)
    
    
    #print("test bool")
    #bool
    #str_floatX=str(1.) # assign it
    print("isDone:",isDone)
    if (isDone==False) :
        print("check if bool")
        if(x==True): str_floatX="b    1.000"
        else: str_floatX="b    0.000"
        isDone=True
    else:
        print("not bool")
    #if end
    # final error and finish the function
    if (isDone==False): str_floatX="??????.???" #output that indicates an error (bad type)
    #if end
    print("debug: f3 of floatX: returning",str_floatX)
    return str_floatX
#end def f3
#end f3
#/f3_py.txt
print("start of test_f3.py")
while True:
    print("click into the 'execution window' below, then:")
    inNumb=input("type in something")
    print("inStr:",inNumb)
    oTxt=str(f3(inNumb))
    print("in f3 format:",inNumb)
#end while
#source:https://realpython.com/python-f-strings/
# test_f3_pyD.py