Re: converting string to dict ?
----- Original Message ----- From: "Oleg Broytmann" <phd@phd.fep.ru> To: "Jason Cunliffe" <jasonic@nomadicsltd.com> Cc: <python-list@python.org> Sent: Wednesday, April 11, 2001 6:42 AM Subject: Re: converting string to dict ?
On Wed, 11 Apr 2001, Jason Cunliffe wrote:
A web input form passes {'name':'Cleopatra'} as a string. Please, is there a nice clean function to convert this string to a dict?
"eval"
Please, please send me your URL - I will hack you through your call to the eval! :))) Seriously, do not trust ANYTHING from the web. Do not convert things into objects, do not eval them - NEVER!
ok +Thanks! But seriously seriously please explain... I am passing data between Zope and Flash5. My impression that used well, Flash is much more secure than HTML/Javscript pages. There are ways to catch, open up and parse .swf files, but it is much less inviting.. Flash5's ActionsScript loadVariables() function accepts urlencoded Python dicts via 'POST' using a Python External Method. For example: #external method import urllib def gettext(self): #print "gettext" params = urllib.urlencode({'message': 'This is message from Zope?','name':'Zope Lover','status':'done'}) return params It works the other way too :-) #external method def puttext(self): #print 'puttext' v1 = self.REQUEST['v1'] v2 = self.REQUEST['v2'] newdict = {'v1':v1, 'v2':v2} print newdict return urllib.urlencode({'newdict':newdict}) where v1 and v2 are my variables in Flash So then I tried to do it all again in a Zope PythonScript, with thanks to Duncan Booth for How-To PythonScript urlencode tip: ## Script(Python) py_gettext ##parameterlist dict={'message': 'Do you understand now?','name':'Zope Lover','status':'done'} ##body def urlencode(dict): """Encode a dictionary of form entries into a URL query string.""" from Products.PythonScripts.standard import url_quote_plus from string import join l = [] for k, v in dict.items(): k = url_quote_plus(str(k)) v = url_quote_plus(str(v)) l.append(k + '=' + v) return join(l, '&') params = urlencode This works ok until I try to manipulate the contents of 'dict' paraemter directly via teh PythonScript 'test interface or from a typical DTML Method such as: <form action = "py_gettext" method="post"> enter dict here to test Py_gettext: <input type="text" name="dict":list size="40" value=""> <div class="form-element"> <input class="form-element" type="submit" name="SUBMIT" value="Save Changes"> </form> Error Type: AttributeError Error Value: 'string' object has no attribute 'items' ..snip.. File D:\Program Files\Zsite231\lib\python\Products\PythonScripts\PythonScript.py, line 336, in _exec (Object: py_gettext) (Info: ({'script': <PythonScript instance at 014B53D0>, 'context': <Folder instance at 014B5E90>, 'container': <Application instance at 014A1260>, 'traverse_subpath': []}, ("{'name':'Jason'}",), {}, ({'message': 'Do you understand now?', 'name': 'Zope Lover', 'status': 'done'},))) File Script (Python), line 13, in py_gettext File <string>, line 7, in urlencode AttributeError: (see above) My conclusion thus far is that I should stick with External Python Methods and be happy. But I have this peverse curiosity to make it work in Python Script also. The Docs say that eval() is not allowed in Scipts(Python). Is this the trouble I am having? Any ideas? Beynod the learning excerise, the main reason I want to get PythonScripts running smoothly for Python dict i/o was so that I could call the various methods ZPythonScript_edit(params, body): ZScriptHTML_tryParams(self): etc thus creating a Flash-driven interface to Zope. Ther are many aspects of Flash which mirror Zope's object-oriented container/context paradigm. So in those resepcts it makes an ideal user interface toolkit. Plus one can reduce pointless client-server exchanges or clunky HTML screen redraw and flashing.. much smoother usability. {Don't you just hate the way Zope has chronic display hiccups?:: blink blink pause +big white screen with little OK! sign::::>> doh{there must be a better way}} cheers - Jason ___________________________________________________________ Jason CUNLIFFE = NOMADICS['Interactive Art and Technology']
Hi, I'm having problems programming something that works from the user Interface, Root folder has a property "mytypes" of type lines, with three items From the management interface I can add a property to a method foo, value=myypes, type=selection This works and the Property interface shows all the types correctly. However I want to create a new method with the selected property. My method creates a method using manage_addDTMLDocument() I then call <dtml-call "manage_addProperty( 'Type', mytypes, type='selection' ) " > And I get the following error. Notice that the above adding the Property with an unselected option. Do I have to also follow this with a changeProperty call ? Thanks, Lee Marzke <lmarzke@adelphia.net> Zope Error Zope has encountered an error while publishing this resource. Error Type: TypeError Error Value: hasattr, argument 2: expected string, list found Troubleshooting Suggestions The URL may be incorrect. The parameters passed to this resource may be incorrect. A resource that this resource relies on may be encountering an error. For more detailed information about the error, please refer to the HTML source for this page. If the error persists please contact the site maintainer. Thank you for your patience. [Powered by Zope] Traceback (innermost last): File /home/httpd/Zope-2.3.0-src/lib/python/ZPublisher/Publish.py, line 222, in publish_module File /home/httpd/Zope-2.3.0-src/lib/python/ZPublisher/Publish.py, line 187, in publish File /home/httpd/Zope-2.3.0-src/lib/python/Zope/__init__.py, line 221, in zpublisher_exception_hook (Object: Traversable) File /home/httpd/Zope-2.3.0-src/lib/python/ZPublisher/Publish.py, line 171, in publish File /home/httpd/Zope-2.3.0-src/lib/python/ZPublisher/mapply.py, line 160, in mapply (Object: addCfiAction) File /home/httpd/Zope-2.3.0-src/lib/python/ZPublisher/Publish.py, line 112, in call_object (Object: addCfiAction) File /home/httpd/Zope-2.3.0-src/lib/python/OFS/DTMLMethod.py, line 189, in __call__ (Object: addCfiAction) File /home/httpd/Zope-2.3.0-src/lib/python/DocumentTemplate/DT_String.py, line 538, in __call__ (Object: addCfiAction) File /home/httpd/Zope-2.3.0-src/lib/python/DocumentTemplate/DT_With.py, line 146, in render (Object: instructors) File /home/httpd/Zope-2.3.0-src/lib/python/DocumentTemplate/DT_With.py, line 146, in render (Object: _.getitem(CfiId)) File /home/httpd/Zope-2.3.0-src/lib/python/DocumentTemplate/DT_Util.py, line 334, in eval (Object: manage_addProperty('Type', mytypes, type='selection')) (Info: manage_addProperty) File <string>, line 0, in ? File /home/httpd/Zope-2.3.0-src/lib/python/OFS/PropertyManager.py, line 314, in manage_addProperty (Object: 986995749) File /home/httpd/Zope-2.3.0-src/lib/python/OFS/PropertyManager.py, line 247, in _setProperty (Object: 986995749) TypeError: (see above)
I added quotes around 'mytypes' and the error went away. I guess that "selection" needs a variable name, not it's value. <dtml-call "manage_addProperty( 'Type', 'mytypes', type='selection' ) " > Now I am trying to set the value of property 'Type' : <dtml-call "manage_changeProperties( Type=selectedType ) " > This doesn't give an error, but the Type property is always the first listed in the associated lines property, and never changes. Is the syntax for manage_changeProperties correct ? Thanks, Lee Marzke <lmarzke@adelphial.net> At 09:12 AM 4/11/01 -0400, you wrote:
Hi,
I'm having problems programming something that works from the user Interface,
Root folder has a property "mytypes" of type lines, with three items
From the management interface I can add a property to a method foo, value=myypes, type=selection
This works and the Property interface shows all the types correctly.
However I want to create a new method with the selected property. My method creates a method using manage_addDTMLDocument()
I then call <dtml-call "manage_addProperty( 'Type', mytypes, type='selection' ) " >
And I get the following error.
Notice that the above adding the Property with an unselected option. Do I have to also follow this with a changeProperty call ?
Thanks,
Lee Marzke <lmarzke@adelphia.net>
Zope Error
Zope has encountered an error while publishing this resource.
Error Type: TypeError Error Value: hasattr, argument 2: expected string, list found
Troubleshooting Suggestions The URL may be incorrect. The parameters passed to this resource may be incorrect. A resource that this resource relies on may be encountering an error.
For more detailed information about the error, please refer to the HTML source for this page.
If the error persists please contact the site maintainer. Thank you for your patience.
[Powered by Zope]
Traceback (innermost last): File /home/httpd/Zope-2.3.0-src/lib/python/ZPublisher/Publish.py, line 222, in publish_module File /home/httpd/Zope-2.3.0-src/lib/python/ZPublisher/Publish.py, line 187, in publish File /home/httpd/Zope-2.3.0-src/lib/python/Zope/__init__.py, line 221, in zpublisher_exception_hook (Object: Traversable) File /home/httpd/Zope-2.3.0-src/lib/python/ZPublisher/Publish.py, line 171, in publish File /home/httpd/Zope-2.3.0-src/lib/python/ZPublisher/mapply.py, line 160, in mapply (Object: addCfiAction) File /home/httpd/Zope-2.3.0-src/lib/python/ZPublisher/Publish.py, line 112, in call_object (Object: addCfiAction) File /home/httpd/Zope-2.3.0-src/lib/python/OFS/DTMLMethod.py, line 189, in __call__ (Object: addCfiAction) File /home/httpd/Zope-2.3.0-src/lib/python/DocumentTemplate/DT_String.py, line 538, in __call__ (Object: addCfiAction) File /home/httpd/Zope-2.3.0-src/lib/python/DocumentTemplate/DT_With.py, line 146, in render (Object: instructors) File /home/httpd/Zope-2.3.0-src/lib/python/DocumentTemplate/DT_With.py, line 146, in render (Object: _.getitem(CfiId)) File /home/httpd/Zope-2.3.0-src/lib/python/DocumentTemplate/DT_Util.py, line 334, in eval (Object: manage_addProperty('Type', mytypes, type='selection')) (Info: manage_addProperty) File <string>, line 0, in ? File /home/httpd/Zope-2.3.0-src/lib/python/OFS/PropertyManager.py, line 314, in manage_addProperty (Object: 986995749) File /home/httpd/Zope-2.3.0-src/lib/python/OFS/PropertyManager.py, line 247, in _setProperty (Object: 986995749) TypeError: (see above)
_______________________________________________ Zope maillist - Zope@zope.org http://lists.zope.org/mailman/listinfo/zope ** No cross posts or HTML encoding! ** (Related lists - http://lists.zope.org/mailman/listinfo/zope-announce http://lists.zope.org/mailman/listinfo/zope-dev )
On Wed, 11 Apr 2001, Jason Cunliffe wrote:
This works ok until I try to manipulate the contents of 'dict' paraemter directly via teh PythonScript 'test interface or from a typical DTML Method such as:
Error Type: AttributeError Error Value: 'string' object has no attribute 'items'
Of course it doesn't work. Here, dict is only a string, and I recommend you to change your arrangements and don't allow the Web to send you dict or any other Python data. Oleg. ---- Oleg Broytmann http://www.zope.org/Members/phd/ phd@phd.pp.ru Programmers don't die, they just GOSUB without RETURN.
participants (3)
-
Jason Cunliffe -
Lee Marzke -
Oleg Broytmann