Rocket U2 | UniVerse & UniData

 View Only
  • 1.  PyObject in uniBASIC

    PARTNER
    Posted 07-23-2021 13:51
    Hi ,

    Using in-basic python I interface function like var=pyCallFuntion(module,function)  how can I define if the var is a uniBASIC Var or a PYOBJECT ? 

    Then, is there a methods from UNI BASIC to perform python str(var). 

    Any help is welcome.

    ------------------------------
    Manu Fernandes
    ------------------------------


  • 2.  RE: PyObject in uniBASIC
    Best Answer

    PARTNER
    Posted 07-24-2021 05:51
    OK, I found a method to never crash or check type of PYOBJECT 
    for sample, I build a GUID / UUID to use a free unique-key with uuid module -> uuid = PyCallFunction('uuid', 'uuid4')
    ok but may I use the uuid variable in my basic ??
    if it's a PYOBJECT I'll crash because Improper data type.
    so check it
    - load module builtins to call type() and str()
    - pyBuiltins.type return a class <type> then we must use pyBuiltins.str() to use it in uniBASIC

    sample BP UUID
    uuid = PyCallFunction('uuid', 'uuid4')
    
    py=PyImport('builtins') 
    localvar = '' ;* a uniBASICvar to test his py.type
    crt 'type localvar   <':PyCallMethod(py,'str',(PyCallMethod(py,'type',localvar))) :'>'
    crt 'type pyBuiltins <':PyCallMethod(py,'str',(PyCallMethod(py,'type',py)))  :'>'
    crt 'type uuid       <':PyCallMethod(py,'str',(PyCallMethod(py,'type',uuid))):'>'
    crt 'uuid as str()   <':PyCallMethod(py,'str', uuid) :'>'
    

    BASIC/RUN BP UUID

    type uuid <<class 'uuid.UUID'>>
    type var <<class 'str'>>
    type pyBuiltins <<class 'module'>>
    uuid as str() <bafb868e-f30c-42db-bd42-d29ef5d83d37>
    


    by extension, we can enumarate the members/types of the pyobject

    dir=PyCallMethod(py,'str',(PyCallMethod(py,'dir',uuid))) // return a array
    convert ",'[] " to @AM in dir // convert the array to dynArray
    loop remove v from dir setting eos 
         if v # '' then crt 'uuid.':v,PyCallMethod(py,'str',(PyCallMethod(py,'type',PyGetAttr(uuid,v))))   
    while eos do repeat 


    ​output 

    uuid.bytes_le       <class 'bytes'>        
    uuid.clock_seq      <class 'int'>          
    uuid.clock_seq_hi_variant     <class 'int'>
    uuid.clock_seq_low  <class 'int'>          
    uuid.fields         <class 'tuple'>        
    uuid.hex  <class 'str'>                    
    uuid.int  <class 'str'>                    
    uuid.node <class 'int'>                    
    uuid.time <class 'int'>                    
    uuid.time_hi_version          <class 'int'>
    uuid.time_low       <class 'int'>          
    uuid.time_mid       <class 'int'>          
    uuid.urn  <class 'str'>                    
    uuid.variant        <class 'str'>          
    uuid.version        <class 'int'>          
    ​

    Certainely usefull to work with PYOBJECT.

    enjoy.


    ------------------------------
    Manu Fernandes
    ------------------------------