[Zope] Re: converting string to dict ?

Jason Cunliffe jasonic@nomadicsltd.com
Wed, 11 Apr 2001 08:42:36 -0400


----- 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']