python persistence

castironpi at gmail.com castironpi at gmail.com
Tue Apr 1 05:48:35 CEST 2008


On Mar 31, 10:28 pm, castiro... at gmail.com wrote:
> On Mar 31, 9:27 pm, George Sakkis <george.sak... at gmail.com> wrote:
>
>
>
>
>
> > On Mar 31, 8:51 pm, castiro... at gmail.com wrote:
> > > On Mar 31, 7:14 pm, 7stud <bbxx789_0... at yahoo.com> wrote:
>
> > > > On Mar 31, 5:31 pm, castiro... at gmail.com wrote:
>
> > > > > Can you have a Python object stored entirely on disk?
>
> > > > import cPickle as cp
>
> > > > class Dog(object):
> > > >     def __init__(self, name):
> > > >         self.name = name
>
> > > > d = Dog("Spot")
>
> > > > f = open("data.txt", "w")
> > > > cp.dump(d, f)
> > > > f.close()
>
> > > > f = open("data.txt")
> > > > stored_obj = cp.load(f)
> > > > print stored_obj.name
>
> > > > --output:--
> > > > Spot
> > > >>> import pickle
> > > >>> pickle.loads( pickle.dumps( type('None',(),{}) ) )
>
> > > Traceback (most recent call last):
> > >   File "<stdin>", line 1, in <module>
> > >   File "C:\Programs\Python\lib\pickle.py", line 1303, in dumps
> > >     Pickler(f, protocol).dump(obj)
> > >   File "C:\Programs\Python\lib\pickle.py", line 221, in dump
> > >     self.save(obj)
> > >   File "C:\Programs\Python\lib\pickle.py", line 283, in save
> > >     f(self, obj) # Call unbound method with explicit self
> > >   File "C:\Programs\Python\lib\pickle.py", line 697, in save_global
> > >     (obj, module, name))
> > > pickle.PicklingError: Can't pickle <class '__main__.None'>: it's not
> > > found as __
> > > main__.None
>
> >http://docs.python.org/lib/node317.html-Hide quoted text -
>
> What's involved in Can you have a Python object stored entirely on
> disk?  No process to access: changes to contents written back.

We do have, but on Windows, file is not locked to multi-tasking,
shelve.  But why don't we have types in there, or non-string
primitives in keys?

>>> c= shelve.open( 'temp', 'c' )
>>> c['0']= 'a'
>>> c.sync()
>>> del c
>>> c= shelve.open( 'temp', 'c' )
>>> c['0']
'a'

>>> c['0'].append( 0 )
>>> c['0']
[]

And why don't primitive mutations modify contents of disk?  A
metaquestion.

>>> c['0']= type('None',(),{})
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Programs\Python\lib\shelve.py", line 114, in __setitem__
    p.dump(value)
  File "C:\Programs\Python\lib\pickle.py", line 221, in dump
    self.save(obj)
  File "C:\Programs\Python\lib\pickle.py", line 283, in save
    f(self, obj) # Call unbound method with explicit self
  File "C:\Programs\Python\lib\pickle.py", line 697, in save_global
    (obj, module, name))
pickle.PicklingError: Can't pickle <class '__main__.None'>: it's not
found as __
main__.None



More information about the Python-list mailing list