persistent deque
inhahe
inhahe at gmail.com
Tue May 20 17:19:10 EDT 2008
"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.
ok, i made your persistent deque. but there are two important notes
regarding this module: 1) I am a genius. 2) it doesn't actually work for
some reason. i.e., loading an instance from file won't work. i don't know
why. the pickle module is doing something i didn't expect.
from collections import deque
import os, pickle, random
class pdeque(deque):
def __init__(self, filename, 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(self, open(self.tempfilename,'wb'))
try: os.remove(self.filename)
except: pass
os.rename(self.tempfilename, self.filename)
@classmethod
def load(cls, filename):
return pickle.load(open(filename,'rb'))
for name in "append appendleft extend extendleft remove rotate".split():
func = getattr(deque, name)
def func2(instance, arg, func=func):
result = func(instance, arg)
instance.save()
return result
setattr(pdeque, name, func2)
for name in ("pop", "popleft", "clear"):
func = getattr(deque, name)
def func2(instance, func=func):
result = func(instance)
instance.save()
return result
setattr(pdeque, name, func2)
---
you use it just like a deque object except that when initializing the first
parameter is a filename for it to store in. to load the deque object again
later, call .load(filename) on the pdeque class or a pdeque object (except
that it doesn't work.)
i'm sure somebody here knows what obscure feature of pickling it is that's
doing this counterintuitively. (disclaimer: i've never used pickle to store
a custom type before)
More information about the Python-list
mailing list