27 """This module defines the ReadProxy class."""
30 """ReadProxy provides a lazy-loading object that is initialized by a
31 callback function set in ReadProxy's constructor. Adapted from
32 peak.util.proxies.LazyProxy, which was written by Phillip J. Eby
33 (peak@eby-sarna.com)."""
35 __slots__ = (
'__cache__',
'__callback__',
'__subject__')
41 subject = oga(self,
'__subject__')
42 if attr ==
'__subject__':
44 return getattr(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__)
127 get_callback = ReadProxy.__callback__.__get__
128 set_callback = ReadProxy.__callback__.__set__
129 get_cache = ReadProxy.__cache__.__get__
130 set_cache = ReadProxy.__cache__.__set__
135 except AttributeError:
139 ReadProxy.__subject__ = property(__subject__, set_cache)