27 """This module defines the ReadProxy class."""
29 from .persistence
import ReadProxyBase
33 """ReadProxy provides a lazy-loading object that is initialized by a
34 callback function set in ReadProxy's constructor. Adapted from
35 peak.util.proxies.LazyProxy, which was written by Phillip J. Eby
36 (peak@eby-sarna.com)."""
38 __slots__ = (
'__cache__',
'__callback__')
44 getattr(self.__subject__, attr)
47 setattr(self.__subject__, attr, val)
50 delattr(self.__subject__, attr)
53 return bool(self.__subject__)
56 return self.__subject__[arg]
59 self.__subject__[arg] = val
62 del self.__subject__[arg]
65 return self.__subject__[i:j]
68 self.__subject__[i:j] = val
71 del self.__subject__[i:j]
74 return ob
in self.__subject__
76 for name
in 'repr str hash len abs complex int long float iter oct hex'.split():
77 exec(
"def __%s__(self): return %s(self.__subject__)" % (name, name))
79 for name
in 'cmp',
'coerce',
'divmod':
80 exec(
"def __%s__(self, ob): return %s(self.__subject__, ob)" % (name,
84 (
'lt',
'<'), (
'gt',
'>'), (
'le',
'<='), (
'ge',
'>='),
85 (
'eq',
'=='), (
'ne',
'!=')
87 exec(
"def __%s__(self, ob): return self.__subject__ %s ob" % (name, op))
89 for name, op
in [(
'neg',
'-'), (
'pos',
'+'), (
'invert',
'~')]:
90 exec(
"def __%s__(self): return %s self.__subject__" % (name, op))
93 (
'or',
'|'), (
'and',
'&'), (
'xor',
'^'),
94 (
'lshift',
'<<'), (
'rshift',
'>>'),
95 (
'add',
'+'), (
'sub',
'-'), (
'mul',
'*'), (
'div',
'/'),
96 (
'mod',
'%'), (
'truediv',
'/'), (
'floordiv',
'//')
99 "def __%(name)s__(self,ob):\n"
100 " return self.__subject__ %(op)s ob\n"
102 "def __r%(name)s__(self,ob):\n"
103 " return ob %(op)s self.__subject__\n"
105 "def __i%(name)s__(self,ob):\n"
106 " self.__subject__ %(op)s=ob\n"
115 return divmod(ob, self.__subject__)
118 return pow(self.__subject__, *args)
121 self.__subject__ **= ob
125 return pow(ob, self.__subject__)
128 get_callback = ReadProxy.__callback__.__get__
129 set_callback = ReadProxy.__callback__.__set__
130 get_cache = ReadProxy.__cache__.__get__
131 set_cache = ReadProxy.__cache__.__set__
134 def _subject(self, get_cache=get_cache, set_cache=set_cache):
137 except AttributeError:
142 ReadProxy.__subject__ = property(_subject, set_cache)