[Tutor] foreign lists

Andrew Wilkins toodles@yifan.net
Mon, 6 Aug 2001 18:30:08 +0800


>> is it possible to access a list from another file and save whatever
changes are made to the list after >> the original program has been run?

Sorry about the delay, I'm back at school now! Charlie did answer, I just
thought I'd point you to a class I created called ListShelf.
http://www.lowerstandard.com/python/ListShelf.py

Basically I made it for the same purpose as you (well in the technical side,
I didn't want to use it make a book database.) It opens a file and uses the
cPickle module to create a persistent list object. This works much like the
shelve module does, while shelve uses dictionaries, ListShelf (aptly named,
no?) uses lists.

You create an instance of the class, then you treat it as a list. If however
you want to pass the list to another object, you'd have to use the instances
'data' attribute.

Here's how you'd use it...

>>> import ListShelf
>>> x=ListShelf.ListShelf('my_list_file.dat')
>>> x.append(('Catcher in the Rye',('maturation','teenage')))
>>> x
[('Catcher in the Rye', ('maturation', 'teenage'))]
>>> del x; #Just to prove it is persistent...
>>> x=ListShelf.ListShelf('my_list_file.dat')
>>> x
[('Catcher in the Rye', ('maturation', 'teenage'))]

HTH,
Andrew