Clarification of a list of lists as a parameter to a python script.
Hello Thanks to all who have responded before. This is like some misunderstanding of the Python scripts working and just want to make clear to ourselves how things work here, so, please bear with us: A plain Python function does accept a "list of lists" as a parameter and then performs a wide number of different functions with that list of lists, like sorting it or addressing each of the sublists, even address any individual item on any of the sublists. It happens that I have been unable to pass a "list of lists" as a parameter to a Python Script (within Zope, of course), and perform the same functions that are done on the function outside of Zope. Besides some specific python functionality that it is not available due to security reasons and the use of the "return printed" for stuff that gets printed out from the python script, my understanding is that the python script should behave exactly the same if it is executed as a standalone *.py function (program) or if it is executed as a python script. Is this statement correct? If so, what is the correct way of passing a list of lists as a parameter to a python script ? I have the following example: The DTML Document: <form method="POST" action="myscript"> Data: <input type=text name="XX:list" size=50 > <input type="submit" value="ASIGNAR"></td> </form> The myscript python script with "XX" as the parameter print XX XX.sort() print XX if same_type(XX,[]): print "It is a list" return printed What is put into the form ( a list of lists) is: [['c','b','a'],['a','b','c'],['a','a','d']] What I get from the script is: ["[['c','b','a'],['a','b','c'],['a','a','d']]"] ["[['c','b','a'],['a','b','c'],['a','a','d']]"] It is a list Which is a list (indeed) BUT with only one element as it -somehow- gets surrounded by quotes, that is it is NOT a "list of lists" but a list with one string element. I'd expect to get the same list back, then the sorted lists within the main list and later on I could reference the elements with something like XX[sublistnum][elementnum] kind of construction. Any ideas on how to overcome this issue.? The idea is NOT to modify already working python routines -except for the printed variable and security stuff whenever necessary-, just to Zope-fy the application if you know what i mean. Well, any comments are welcome. Thanks in advance for your assistance. Felipe
On Sunday 14 April 2002 05:16, you wrote:
If so, what is the correct way of passing a list of lists as a parameter to a python script ? I have the following example:
The DTML Document:
<form method="POST" action="myscript"> Data: <input type=text name="XX:list" size=50 > <input type="submit" value="ASIGNAR"></td> </form>
The myscript python script with "XX" as the parameter
print XX XX.sort() print XX if same_type(XX,[]): print "It is a list" return printed
What is put into the form ( a list of lists) is:
[['c','b','a'],['a','b','c'],['a','a','d']]
sorry, but you put in a string, which is converted to ONE list due to the :list suffix to your name-attribut
What I get from the script is:
["[['c','b','a'],['a','b','c'],['a','a','d']]"] ["[['c','b','a'],['a','b','c'],['a','a','d']]"] It is a list
of course, ONE list which contents your string
Which is a list (indeed) BUT with only one element as it -somehow- gets surrounded by quotes, that is it is NOT a "list of lists" but a list with one string element.
I'd expect to get the same list back, then the sorted lists within the main list and later on I could reference the elements with something like XX[sublistnum][elementnum] kind of construction.
why not enter the lists in the input-field seperated by one special-char, for example | and the elements of the lists seperated by commas, and then split the string in yout script by | and then by comma , and construct your lists by hand ? p = 'a,b,c|c,b,a|a,a,d' <- your entered string in input field, no conversion to list, passes to python-script as string, then in script: # p as parameter with string l = [] for si in p.split('|'): l.append(si.split(',')) return l it should also be possible to convert the '[['a','b','c'],....] syntax, with a bit more code.
Any ideas on how to overcome this issue.? The idea is NOT to modify already working python routines -except for the printed variable and security stuff whenever necessary-, just to Zope-fy the application if you know what i mean.
ok, to modify your existing scripts less as possible, put the conversion in a seperate script and call it with your string as parameter and put the result, the listlist, to your existing script, like in dtml with <dtml-call expr="your_old_routine(conversion_routine(parameter_from_inputfield))">
Well, any comments are welcome. Thanks in advance for your assistance.
dunno if it helps, but ...
Felipe
Torsten Kühnel
participants (2)
-
Felipe E. Barousse B. -
Rudi Wurm