Hi, as a newbie to Zope and this list, I have a simple question I can't manage to get an answer. If, in a python script, I have a string ="{'aKey':'aValue'}" and I want to generate a dictionary from such a string, what should I do? BTW, the function dict() does not work (at least in Zope 2.4.3) TIA for any suggestion, p.t.
"p.t." wrote:
If, in a python script, I have a string ="{'aKey':'aValue'}" and I want to generate a dictionary from such a string, what should I do?
in normal python (Products, ExternalMethod), eval( yourstring ) would do. in Thru The Web objects (DTML, PythonScript) you cant use eval. cheers hans ------------------------------------------------------------- Who's got only a hammer sees the world as a nail hans augustin (software developer) hans@beehive.de beehive elektronische medien GmbH http://www.beehive.de phone: +49 30 847-82 0 fax: +49 30 847-82 299
hans wrote:
"p.t." wrote:
If, in a python script, I have a string ="{'aKey':'aValue'}" and I want to generate a dictionary from such a string, what should I do?
in normal python (Products, ExternalMethod), eval( yourstring ) would do. in Thru The Web objects (DTML, PythonScript) you cant use eval.
Where did the string come from? In most cases you should be able to return the dictionary rather than its representation string. But if you really need to, you can use the string functions split and replace to make the string into a dictionary. dictAsString ="{'aKey':'aValue'}" newdict = {} #remove left brace newstr = dictAsString.replace('{','') #remove right brace newstr = newstr.replace('}','') #split on commas in case we have multiple items in the dict splitdict = newstr.split(',') #go through the splitdict and add the items to the new dict for anitem in splitdict: #remove quotes noquotestr = anitem.replace("'",'') #now split on colon splititem = noquotestr.split(':') #now add to dict newdict[splititem[0]] = splititem[1] This is the general idea; you may want to fix cases where the right or left side may be other than strings. -- Jim Washington
p.t. writes:
as a newbie to Zope and this list, I have a simple question I can't manage to get an answer. If, in a python script, I have a string ="{'aKey':'aValue'}" and I want to generate a dictionary from such a string, what should I do? BTW, the function dict() does not work (at least in Zope 2.4.3) TIA for any suggestion, If you have write access to the file system (where Zope extensions live), I would recomment to make an External Method "safe_eval" and use it for the conversion.
"safe_eval" could have the following definition def safe_eval(s,dict=None): '''evaluate *s* in *dict*.''' if dict is None: dict= {} dict['__builtins__']= None return eval(s,dict) The "dict['__builtins__']= None" makes your "eval" half-way safe. It is still possible to let your server crash by creating excessively large objects: e.g. '1000000000 * "123"'. The package "RestrictedPython" probably allows you to define safer versions of "eval" but I did not yet look enough into it that I could give you a precise recipe. An alternative, but a bit indirect, would be to create a DTML object consisting of <dtml-return expr="your string"> and then call it. The result will be your string evaluated. Dieter
participants (4)
-
Dieter Maurer -
hans -
Jim Washington -
p.t.