MCPcopy Create free account
hub / github.com/ActiveState/code / ShelfWrapper

Class ShelfWrapper

recipes/Python/496742_ShelfProxy/recipe-496742.py:22–68  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

20 pass
21
22class ShelfWrapper(object):
23 def __init__(self, shelf):
24 self.__shelf = shelf
25 self.__cache = {}
26
27 def __del__(self):
28 self.close()
29
30 def __getattr__(self, name):
31 return getattr(self.__shelf, name)
32
33 def __contains__(self, key):
34 return key in self.__shelf
35
36 def __len__(self, key):
37 return len(self.__shelf)
38
39 def __delitem__(self, key):
40 if key in self.__cache:
41 object.__setattr__(self.__cache[key], "_invalidated", True)
42 del self.__cache[key]
43 del self.__shelf[key]
44
45 def __getitem__(self, key):
46 try:
47 obj = self.__cache[key]
48 except KeyError:
49 self.__cache[key] = obj = ShelfProxy(self.__shelf[key], self.__shelf, key)
50 return obj
51
52 def __setitem__(self, key, value):
53 if key in self.__cache:
54 object.__setattr__(self.__cache[key], "_invalidated", True)
55 self.__cache[key] = ShelfProxy(value, self.__shelf, key)
56 self.__shelf[key] = value
57
58 def sync(self):
59 for obj in self.__cache.itervalues():
60 try:
61 sync_proxy(obj)
62 except InvalidationError:
63 pass
64
65 def close(self):
66 self.sync()
67 self.__cache.clear()
68 self.__shelf.close()
69
70
71def sync_proxy(proxy):

Callers 1

openFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected