Please help with MemoryError

Aahz aahz at pythoncraft.com
Fri Feb 12 09:21:19 EST 2010


In article <mailman.2426.1265976954.28905.python-list at python.org>,
Tim Chase  <python.list at tim.thechases.com> wrote:
>Aahz wrote:
>> Tim Chase  <python.list at tim.thechases.com> wrote:
>>>
>>> Just to add to the mix, I'd put the "anydbm" module on the gradient
>>> between "using a file" and "using sqlite".  It's a nice intermediate
>>> step between rolling your own file formats for data on disk, and having
>>> to write SQL since access is entirely like you'd do with a regular
>>> Python dictionary.
>> 
>> Not quite.  One critical difference between dbm and dicts is the need to
>> remember to "save" changes by setting the key's valud again.
>
>Could you give an example of this?  I'm not sure I understand 
>what you're saying.  I've used anydbm a bunch of times and other 
>than wrapping access in
>
>   d = anydbm.open(DB_NAME, "c")
>   # use d as a dict here
>   d.close()
>
>and I've never hit any "need to remember to save changes by 
>setting the key's value again".  The only gotcha I've hit is the 
>anydbm requirement that all keys/values be strings.  Slightly 
>annoying at times, but my most frequent use case.

Well, you're more likely to hit this by wrapping dbm with shelve (because
it's a little more obvious when you're using pickle directly), but
consider this:

    d = anydbm.open(DB_NAME, "c")
    x = MyClass()
    d['foo'] = x
    x.bar = 123

Your dbm does NOT have the change to x.bar recorded, you must do this
again:

    d['foo'] = x

With a dict, you have Python's reference semantics.
-- 
Aahz (aahz at pythoncraft.com)           <*>         http://www.pythoncraft.com/

"At Resolver we've found it useful to short-circuit any doubt and just        
refer to comments in code as 'lies'. :-)"



More information about the Python-list mailing list