The python 'dict' object is ubiquitous when interacting with python modules, functions and classes.
The process to create a python DICT object using the U2 BASIC Python API is, well, interesting:
- Create a LIST containing just the 'name' and 'value' for the DICT entry
- Create a TUPLE from the list
- Append the TUPLE to another LIST that will be used to create the DICT
- When the LIST of TUPLE objects is completed, create a DICT using the LIST of TUPLES
This all works ok for strings and numbers, but I cannot get a boolean True or False value to use in the tuple.
I either get an integer value of 1, or 'None'.
This is the code I have tested so far, trying to get a python True object/value:
** Attempting to create a python True object using the PyBASIC API
pmo.BUILTINS = PyImport("builtins")if len(@PYEXCEPTIONTYPE) then gosub PY.BADNESS
* Using CallMethod to run the "bool(x)" commandtest1 = PyCallMethod( pmo.BUILTINS, "bool", (-1))rslt = PyCallMethod( pmo.BUILTINS, "print", 'test1', test1)
* Using CallMethod to run the "True" valuetest2 = PyCallMethod( pmo.BUILTINS, "True")rslt = PyCallMethod( pmo.BUILTINS, "print", 'test2', test2)
* Create callable PyBobject for bool()py_bool = PyGetAttr( pmo.BUILTINS, "bool")if len(@PYEXCEPTIONTYPE) then gosub PY.BADNESS* Create True using the __new__() methodtest3 = PyCallMethod( py_bool, "__new__", (1))rslt = PyCallMethod( pmo.BUILTINS, "print", 'test3', test3)
* Use the GetAttr method to get the True attributetest4 = PyGetAttr( pmo.BUILTINS, "True")rslt = PyCallMethod( pmo.BUILTINS, "print", 'test4', test4)
stop
PY.BADNESS:
crt " EXCEPTION TYPE = " :@PYEXCEPTIONTYPEcrt " EXCEPTION MESSAGE = " :squote(@PYEXCEPTIONMSG)crt " EXCEPTIONTRACEBACK = " :@PYEXCEPTIONTRACEBACK
stop
When I run this all I get is this result:>RUN GDS.BP TEST.PY.TRUEtest1 1test2 Nonetest3 Nonetest4 1>
Does anyone have any thoughts on how I can correctly create a True or False python value/object using the U2 BASIC Python API functions?
Whilst I could create python code to do this, I am trying to avoid the need to pre-deploy the python source as this logic is intended to be used as part of our python deployment processing (otherwise I have to deploy code so I can test the installation context to see if I need to deploy code ...).
------------------------------
Gregor Scott
Software Architect
Pentana Solutions Pty Ltd
Mount Waverley VIC AU
------------------------------