Go Back   Velocity Reviews > Newsgroups > Python
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

Python - Question about creating dictionary like objects

 
Thread Tools Search this Thread
Old 11-06-2009, 10:06 PM   #1
Default Question about creating dictionary like objects


I'm working on a memcached based session library, and I've run into an
interesting problem.

Here's the full code for the session library - http://pastebin.com/m295fdfc2

What I'm doing is using __setitem__ to set an element as a list. When
I call .append() on that list, it appears to be modifying the list in
memory, but not running __setitem__ on the parent in order for the
information to get saved in memcached.

For example, here's a test I wrote in Tornado

class TestHandler(BaseHandler):
def get(self):
session = sessions.Session(req_obj = self)
if "test" in session:
self.write("should be appending")
session["test"].append(datetime.datetime.now())
else:
session["test"] = ["first"]
self.write(str(session))

This should have just "test" in the list on first load (which it
does), the append the datetime object on every refresh, so the list
should keep growing. What I'm seeing is that the datetime does get
appended to the list in the session object that is printed, but the
__setitem__() item is never called, so it never gets updated in
memcached.

Can someone tell me what I'm doing wrong?


bowman.joseph@gmail.com
  Reply With Quote
Old 11-06-2009, 10:55 PM   #2
Chris Rebert
 
Posts: n/a
Default Re: Question about creating dictionary like objects
On Fri, Nov 6, 2009 at 2:06 PM,
<> wrote:
> I'm working on a memcached based session library, and I've run into an
> interesting problem.
>
> Here's the full code for the session library - http://pastebin.com/m295fdfc2
>
> What I'm doing is using __setitem__ to set an element as a list. When
> I call .append() on that list, it appears to be modifying the list in
> memory, but not running __setitem__ on the parent in order for the
> information to get saved in memcached.


list.append() just appends the item to the list object. That's all it
does *and nothing else*.

It doesn't call __setitem__ on the "parent" object containing the
list; how would it even know about that object to begin with?
(pointers aren't reversible)
With normal containers, there's no need to store modified, mutable
items back in the container: the item got modified in-place, the
reference to it from the container is still valid, so storing it back
would be pointless (sounds like memcached is quirky (but probably
justifiably so) in not having this work).
Perhaps you thought some copying occurs when getting items out of
containers? That's not the case.

Since it sounds like you need to force a __setitem__ call on `session`
to have memcached update its storage, you'll have to do it explicitly.

Change:
session["test"].append(datetime.datetime.now())

To:
#use some name better than "foo" obviously
foo = session["test"] #fetch
foo.append(datetime.datetime.now()) #mutate
session["test"] = foo #store back explicitly so memcached knows there
was a change

Cheers,
Chris
--
http://blog.rebertia.com


Chris Rebert
  Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Medical PALM pilot programs (AKA PDA programs) in full working versions, Medical CDs - [Part 1], [Part 2], [Part 3 = MEDLINE 1986-1998] CDs, [Part 4 = Dragon Naturally Speaking CDs, and IBM Via Voice CDs, including Medical Solutions], [Part 5 = Math kashumoto_tokugawa Computer Information 0 11-07-2006 04:38 PM
hd partition question rabbit Computer Support 6 01-12-2006 04:05 AM
creating an F6 RAID disk question. =?Utf-8?B?amJhbGNpdXM=?= Windows 64bit 6 11-07-2005 07:08 PM
**>> Speed UP 187%(ave.) Your Computer Kirk Gregory Czuhai Computer Information 2 08-18-2004 09:34 PM
I ask of you a serious question... Glenn Shatz Computer Support 17 11-14-2003 02:41 PM




SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46