Data Representation?

Kris Caselden google at hanger.snowbird.net
Sun Oct 12 02:47:13 EDT 2003


Yes, thanks. Pickle/marshal/shelve are pretty close to what I'm
looking for. Shelve in particular. However, consider:

>>> d = shelve.open('data.txt',writeback=True)
>>> d['a']=[1]
>>> d['b']=d['a']
>>> d['c']=[d['a']]
>>> print d
{'a': [1], 'c': [[1]], 'b': [1]}
>>> d['a'][0]=2
>>> print d
{'a': [2], 'c': [[2]], 'b': [2]}
>>> d.close()
>>> d = shelve.open('data.txt',writeback=True)
>>> print d
{'a': [2], 'c': [[2]], 'b': [2]}
>>> d['a'][0]=3
>>> print d
{'a': [3], 'c': [[2]], 'b': [2]}
>>> 

Note how the dynamic link to d['a'] is lost. Is there any module
similar to shelve that could maintain this link?

AK <ak at nospam.com> wrote in message news:<PH0ib.32215$mQ2.5062 at newsread1.news.atl.earthlink.net>...
> In article <abc3fdd3.0310111540.37400ed6 at posting.google.com>, Kris
> Caselden wrote:
> > Say I have some data:
> > 
> >>>> a=[1]
> >>>> b=[2]
> >>>> link=[a,b]
> > 
> > The simplest why to write this to a file represents it as
> > 
> >>>> print str(link)
> > [[1], [2]]
> > 
> > Unfortunately, if this is read back in via execfile(), the whole
> > dynamic nature of changing 'link' by changing 'a' and 'b' is lost. Is
> > there any way to write data so that the list name is written instead
> > of the list's values? Essentially, '[[a], [b]]' instead of '[[1],
> > [2]]'? Any help most appreciated.
> 
> Sure, look at pickle and shelve modules in library reference.
> 
> shelve works kind of like this:
> 
> s = shelve.open('filename')
> s['a'] = a
> s.close()
> 
> s = shelve.open('filename')
> a = s['a']
> print a
> 
> code not tested..
> 
>  -AK




More information about the Python-list mailing list