[DB-SIG] Question on shelve/pickle and namespaces...

Lloyd Kvam pythontutor at venix.com
Thu Feb 5 09:48:46 EST 2004


Chris Cogdon wrote:
> 
> On Feb 4, 2004, at 20:41, Stuart Hungerford wrote:
> 
>> Hi all,
>>
>> I hope this is a suitable forum to ask this shelve related
>> question: I figured if any group or SIG would know, it would
>> be this one.
>>
>> I have a python module foo.py which contains:
>>
>>    class A(object): ...
>>    class B(object): ...
>>
>> As well as a class that uses shelve (which in turn is
>> using dumbdbm):
>>
>>    class foo(object): ...
>>       // uses a shelve internally
>>
>> The methods of foo shelve and restore A and B instances
>> *but* shelve seems to be looking in the wrong places for
>> the definitions of A and B when they're "unshelved",
>> giving the error:
>>
>>    AttributeError: 'module' object has no attribute 'A'
>>
>> How can I persuade shelve to look in other namespaces for
>> A and B?
>>
>> Any advice much appreciated,
> 
> 
> Simply, make sure that the function or method that unshelves objects has 
> the same namespace setup (with regards to classes and modules) as the 
> setup that shelves them. The easiest way to do this is to have the 
> shelving and unshelving in the same location, or nearly same location, 
> in the code, such as putting all the shelving and unshelving into 
> methods of the same class.
> 
> 

If Chris's advice is not so easy to follow, I'd suggest:

import ABmodule; A = ABmodule.A; B = ABmodule.B

-- 
Lloyd Kvam
Venix Corp.
1 Court Street, Suite 378
Lebanon, NH 03766-1358

voice:	603-653-8139
fax:	801-459-9582

-- 
Lloyd Kvam
Venix Corp.
1 Court Street, Suite 378
Lebanon, NH 03766-1358

voice:	603-653-8139
fax:	801-459-9582




More information about the DB-SIG mailing list