persistent deque

inhahe inhahe at gmail.com
Wed May 21 18:51:07 EDT 2008


something randomly made me realize why my second solution didn't work, so i 
fixed it.  now you have a working persistent deque.

1. if you call somepdequeobject.load(filename) (as opposed to 
pdeque.load(filename)), do you want it to return a new object that it loaded 
from file, or do you want it to change somepdequeobject to be the object in 
the file?  i'm not sure which way is canonical.

2. i'm not sure if i actually have to use a temp file and then copy it. i 
don't know if pickle.dump as an "atomic commit".

3. cPickle would be faster but i think that means you might end up storing 
something in your deque that can't be pickled, like a circular reference, 
and dunno what else.

4. can someone tell me if the way i'm using the decorator is sane?  i've 
never used decorators before. it just seems ridiculous to a) define a lambda 
that just throws away the parameter, and b) define a meaningless function to 
pass to that lambda.

5. Of course, I am a genius.

from collections import deque
import os, pickle, random

class pdeque(deque):
  def __init__(self, filename, initial=None):
    if initial is None: initial = []
    deque.__init__(self, initial)
    self.filename = filename
    self.tempfilename = ''.join((random.choice("abcdefghijklmnopqrstuvwxyz") 
for x in xrange(10)))+".tmp"
    self.save()
  def save(self):
    pickle.dump(deque(self), open(self.tempfilename,'wb'))
    try: os.remove(self.filename)
    except: pass
    os.rename(self.tempfilename, self.filename)
  @classmethod
  def load(cls, filename):
    return pdeque(filename, pickle.load(open(filename,'rb')))

    #todo: change this so that if it's called from an object it loads
    #the data into that object?

def makefunc(func):
  def func2(instance, *args):
    result = func(instance, *args)
    instance.save()
    return result
  return lambda _: func2

for name, func in deque.__dict__.items():
  if (not name.startswith("_")) or name in ('__delitem__', '__setitem__'):
    @makefunc(func)
    def f(): pass
    setattr(pdeque, name, f)







"castironpi" <castironpi at gmail.com> wrote in message 
news:29451c2a-cb0a-43a6-b140-6c16e3cb46ac at c65g2000hsa.googlegroups.com...
> I'd like a persistent deque, such that the instance operations all
> commit atomicly to file system. 





More information about the Python-list mailing list