Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
AT&T devCentral & BlackBerry(r) Webcast Series: BlackBerry and GPS -Build Location Awareness into your BlackBerry Applications, July 10th-1:00PM EST. Register Today!
  #1  
Old February 14th, 2004, 04:26 PM
ataylder ataylder is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 4 ataylder User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
cookies and arrays

Is it possible to set a cookie to an array as in

a=['1','2']
RESPONSE.setCookie('lecturer',a)

as in the above example when I view my cookie all I see is "['1'"

Can anyone help ?

Reply With Quote
  #2  
Old February 14th, 2004, 06:51 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,529 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 17 h 19 m 5 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
The answer is yes, although it's not as easy as it could be. There are several ways you could do this so lets just step over a few.

1. store repr(list) and then reainmate this using the eval function. Although i only suggest doing this if you're are confident of the type of objects stored in the cookie because its possible to craft the cookie to execute arbitary code on the server!

2. Use the SerialCookie() or SmartCookie() class; but both of these have now been depreciated because of the security risk mentioned above and will eventually "go away" .

3. Parse the cookie string and build a new list containing the new values. This is a lot safer simply because you can run tests thoughout your code to make sure the values are what you're looking for! Definatly safer IMO!

Hope this helps,

Mark.
__________________
programming language development: www.netytan.com Hula


Reply With Quote
  #3  
Old February 15th, 2004, 02:37 PM
ataylder ataylder is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 4 ataylder User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thanks

Thanks for the quick reply. I was considering giving up....

If I understand you correctly if I go for option 3 I need to name the elements in my cookie and store each one separately

ie setCookie('lecturer', 'name0', a[0])

etc

Is that right ?


Alison

Quote:
Originally Posted by netytan
The answer is yes, although it's not as easy as it could be. There are several ways you could do this so lets just step over a few.

1. store repr(list) and then reainmate this using the eval function. Although i only suggest doing this if you're are confident of the type of objects stored in the cookie because its possible to craft the cookie to execute arbitary code on the server!

2. Use the SerialCookie() or SmartCookie() class; but both of these have now been depreciated because of the security risk mentioned above and will eventually "go away" .

3. Parse the cookie string and build a new list containing the new values. This is a lot safer simply because you can run tests thoughout your code to make sure the values are what you're looking for! Definatly safer IMO!

Hope this helps,

Mark.

Reply With Quote
  #4  
Old February 15th, 2004, 03:19 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,529 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 17 h 19 m 5 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
Ok thats option four , i was working on the assumtion that you had a list already stored in a cookie like this..

"[1, 2, 3, 4, 5, 6]"

Where as i was talking about splitting this string into a usable list i.e.

>>> string_list = "[1, 2, 3, 4, 5, 6]"
>>> string_list = string_list[1:-1]
>>> string_list
'1, 2, 3, 4, 5, 6'
>>> string_list.split(', ')
['1', '2', '3', '4', '5', '6']
>>>

Which do you want to use or, what do want to do exactly? Any info you could give me would be very useful!

Mark.

Reply With Quote
  #5  
Old February 16th, 2004, 03:56 AM
ataylder ataylder is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 4 ataylder User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thank you for your patience. As you can see I am not a natural born Python programmer. I am a Zope programmer discovering the heady powerfulness of python.

To give you some more info.

I am calling my python script from a form which amongst other things saves values from a multiple select list into Zope object properties. This all works hunkydory.

Then I wish to save the values of this multi select into a cookie so that when the user sees the form again the same values are preselected.

I appears that even when I save a python list into the cookie as you suggest:

['1','2','3']

the cookie value is always interpreted as a string and not a list. So in my form I cannot loop through the cookie values to select items in the multi select.

This my python code:

a=['1','2','3','4']
RESPONSE.setCookie('c_lecturer',a)

In the request variable I see the cookie set to "['1','2','3','4']" ie as a string. Which is not much use to me. I need to pass the cookie an array.

Does this make more sense ?

alison


Quote:
Originally Posted by netytan
Ok thats option four , i was working on the assumtion that you had a list already stored in a cookie like this..

"[1, 2, 3, 4, 5, 6]"

Where as i was talking about splitting this string into a usable list i.e.

>>> string_list = "[1, 2, 3, 4, 5, 6]"
>>> string_list = string_list[1:-1]
>>> string_list
'1, 2, 3, 4, 5, 6'
>>> string_list.split(', ')
['1', '2', '3', '4', '5', '6']
>>>

Which do you want to use or, what do want to do exactly? Any info you could give me would be very useful!

Mark.

Reply With Quote
  #6  
Old February 16th, 2004, 05:38 AM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,529 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 17 h 19 m 5 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
No poblem . I don't know how Zope loads cookies, i've only used it a few times, but i'm assuming by the nature of you're problem the results are being returned as a string something like this.

Code:
"a=['1','2','3','4']; b=['1','2','3','4']"


In python cgi you can use soemthing like this function to parse the 'HTTP_COOKIE' string into a useable form.

Code:
def cookieList(string):
	cookies = string.split('; ')
	handler = {}
	for cookie in cookies:
		key, value = cookie.split('=')
		if value:
			value = value[1:-1].split(',')
			handler[key] = value

cookieList("a=['1','2','3','4']; b=['1','2','3','4']")


Of course this all depends on how you're cookie string is returned and the function is pretty basic, could use some building on.

I'd still try looping over you're setCookie() calls and adding the values 1 by 1. Just a guess but it seems like a good plan.

Hope this helps,

Mark.

Reply With Quote
  #7  
Old February 20th, 2004, 04:57 AM
ataylder ataylder is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 4 ataylder User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thanks

Thanks for your help.

It took me a long time but finally I realised that I was experiencing two problems. The first was that as you correctly suggested I had to reformat my cookie string into a usable list which I did using a python script by stripping quotes and square brackets. But in order to get this far I had to realise that what was really screwing me up was that cookies on IE5.2 for Mac were not working properly. But this problem is another quest for another forum. My program now works on Mozilla and I am looking for an IE work around.

Alison



Quote:
Originally Posted by netytan
No poblem . I don't know how Zope loads cookies, i've only used it a few times, but i'm assuming by the nature of you're problem the results are being returned as a string something like this.

Code:
"a=['1','2','3','4']; b=['1','2','3','4']"


In python cgi you can use soemthing like this function to parse the 'HTTP_COOKIE' string into a useable form.

Code:
def cookieList(string):
	cookies = string.split('; ')
	handler = {}
	for cookie in cookies:
		key, value = cookie.split('=')
		if value:
			value = value[1:-1].split(',')
			handler[key] = value

cookieList("a=['1','2','3','4']; b=['1','2','3','4']")


Of course this all depends on how you're cookie string is returned and the function is pretty basic, could use some building on.

I'd still try looping over you're setCookie() calls and adding the values 1 by 1. Just a guess but it seems like a good plan.

Hope this helps,

Mark.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > cookies and arrays


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway