Hello, I am trying to store a dictionary in a cookie which I would like to retrieve later and use. A test python script is as follows: ########## request = container.REQUEST response = request.RESPONSE dic = {'filter':'yes'} response.setCookie('myCookie', dic) if request.has_key('myCookie'): mydic = request.myCookie print mydic['filter'] return printed ########### When the script is run I get an error:
*Error Type: TypeError* *Error Value: sequence index must be integer*
It seems that the dictionary is returned as a string. How can I cast this string to a dictionary type? Thanks. Kashif
use request[somekey] and *not* request.somekey -aj --On Freitag, 6. Februar 2004 14:01 Uhr -0600 Kashif Jabbar <kjabbar@ssidecisions.com> wrote:
Hello,
I am trying to store a dictionary in a cookie which I would like to retrieve later and use. A test python script is as follows:
########## request = container.REQUEST response = request.RESPONSE
dic = {'filter':'yes'} response.setCookie('myCookie', dic) if request.has_key('myCookie'): mydic = request.myCookie print mydic['filter']
return printed ###########
When the script is run I get an error:
*Error Type: TypeError* *Error Value: sequence index must be integer*
It seems that the dictionary is returned as a string. How can I cast this string to a dictionary type?
Thanks. Kashif
_______________________________________________ Zope maillist - Zope@zope.org http://mail.zope.org/mailman/listinfo/zope ** No cross posts or HTML encoding! ** (Related lists - http://mail.zope.org/mailman/listinfo/zope-announce http://mail.zope.org/mailman/listinfo/zope-dev )
I changed the script as follows: ### request = container.REQUEST response = request.RESPONSE dic = {'filter':'yes'} response.setCookie('myCookie', dic) if request.has_key('myCookie'): mydic = request['myCookie'] <-------- note the change print mydic['filter'] return printed ### and I still get the same error. Andreas Jung wrote:
use request[somekey] and *not* request.somekey -aj
--On Freitag, 6. Februar 2004 14:01 Uhr -0600 Kashif Jabbar <kjabbar@ssidecisions.com> wrote:
Hello,
I am trying to store a dictionary in a cookie which I would like to retrieve later and use. A test python script is as follows:
########## request = container.REQUEST response = request.RESPONSE
dic = {'filter':'yes'} response.setCookie('myCookie', dic) if request.has_key('myCookie'): mydic = request.myCookie print mydic['filter']
return printed ###########
When the script is run I get an error:
*Error Type: TypeError* *Error Value: sequence index must be integer*
It seems that the dictionary is returned as a string. How can I cast this string to a dictionary type?
Thanks. Kashif
_______________________________________________ Zope maillist - Zope@zope.org http://mail.zope.org/mailman/listinfo/zope ** No cross posts or HTML encoding! ** (Related lists - http://mail.zope.org/mailman/listinfo/zope-announce http://mail.zope.org/mailman/listinfo/zope-dev )
Maybe because cookies are stored in a dedicated dict in the REQUEST object. Check the implementation in HTTPRequest.py. -aj --On Freitag, 6. Februar 2004 14:19 Uhr -0600 Kashif Jabbar <kjabbar@ssidecisions.com> wrote:
I changed the script as follows:
### request = container.REQUEST response = request.RESPONSE
dic = {'filter':'yes'} response.setCookie('myCookie', dic) if request.has_key('myCookie'): mydic = request['myCookie'] <-------- note the change print mydic['filter']
return printed ###
and I still get the same error.
Andreas Jung wrote:
use request[somekey] and *not* request.somekey -aj
--On Freitag, 6. Februar 2004 14:01 Uhr -0600 Kashif Jabbar <kjabbar@ssidecisions.com> wrote:
Hello,
I am trying to store a dictionary in a cookie which I would like to retrieve later and use. A test python script is as follows:
########## request = container.REQUEST response = request.RESPONSE
dic = {'filter':'yes'} response.setCookie('myCookie', dic) if request.has_key('myCookie'): mydic = request.myCookie print mydic['filter']
return printed ###########
When the script is run I get an error:
*Error Type: TypeError* *Error Value: sequence index must be integer*
It seems that the dictionary is returned as a string. How can I cast this string to a dictionary type?
Thanks. Kashif
_______________________________________________ Zope maillist - Zope@zope.org http://mail.zope.org/mailman/listinfo/zope ** No cross posts or HTML encoding! ** (Related lists - http://mail.zope.org/mailman/listinfo/zope-announce http://mail.zope.org/mailman/listinfo/zope-dev )
_______________________________________________ Zope maillist - Zope@zope.org http://mail.zope.org/mailman/listinfo/zope ** No cross posts or HTML encoding! ** (Related lists - http://mail.zope.org/mailman/listinfo/zope-announce http://mail.zope.org/mailman/listinfo/zope-dev )
Kashif Jabbar wrote at 2004-2-6 14:01 -0600:
I am trying to store a dictionary in a cookie which I would like to retrieve later and use.
You must understand what cookies are. Reading the cookie specification may help. You cannot expect that a Python dictionary used as a cookie value comes back as a Python dictionary. In fact, Zope transforms it to a string (as it knows, that a cookie value must be a string), and it comes back as this string (and not the original dictionary).
... mydic = request.myCookie print mydic['filter']
return printed ###########
When the script is run I get an error:
*Error Type: TypeError* *Error Value: sequence index must be integer*
It seems that the dictionary is returned as a string. Indeed.
How can I cast this string to a dictionary type?
You cannot. You might be able to use "eval" (in an External Method) to get the original dict back (it will work, if the dictionary contained only simple objects). Be warned, however, that "eval" is a dangerous function, when applied to untrusted strings (such as strings sent as cookies). -- Dieter
From: "Kashif Jabbar" <kjabbar@ssidecisions.com>
I am trying to store a dictionary in a cookie which I would like to retrieve later and use.
Cookies are stored by the clients. They have no idea what a python dictionary are. Do you really want to store it in a cookie, or do you just want to store ite temporarily? Because in the later case, using a session might be simpler. Otherwise I maybe you can use "eval" to convert it back to a dictionary?
I know that cookies are just text strings but I was hoping for a high level function that can intelligently parse a string and cast it to a dictionary, list, tuple, etc. It seems eval() might be applied here but there are issues with using eval. I have reevaluated my requirements and I don't really need to store this info in cookies and will use session variables instead where it is much easier to manipulate data. I appreciate everyones help on this matter. Thank you. Lennart Regebro wrote:
From: "Kashif Jabbar" <kjabbar@ssidecisions.com>
I am trying to store a dictionary in a cookie which I would like to retrieve later and use.
Cookies are stored by the clients. They have no idea what a python dictionary are. Do you really want to store it in a cookie, or do you just want to store ite temporarily? Because in the later case, using a session might be simpler.
Otherwise I maybe you can use "eval" to convert it back to a dictionary?
participants (4)
-
Andreas Jung -
Dieter Maurer -
Kashif Jabbar -
Lennart Regebro