Skip to content

Commit a2ca46d

Browse files
author
smcq
committed
first commit
0 parents  commit a2ca46d

File tree

8 files changed

+730
-0
lines changed

8 files changed

+730
-0
lines changed

prototype/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

prototype/__init__.pyc

134 Bytes
Binary file not shown.

prototype/prototype.py

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
import new
2+
3+
class PrototypeException(Exception):
4+
pass
5+
6+
class PrototypeSwitcher(object):
7+
def __init__(self, delegate, extend=False):
8+
self._delegate = delegate
9+
self._replacement = None
10+
self._extend = extend
11+
12+
def _delegate_call(self, key):
13+
def delegator(eat_self, *args, **kwargs):
14+
bound_fn = getattr(self._delegate, key)
15+
if bound_fn:
16+
return bound_fn(*args, **kwargs)
17+
return delegator
18+
19+
def _decorate(self, cls):
20+
old_getattr = cls.__getattr__ if '__getattr__' in cls.__dict__ else None
21+
delegate_bind = self._delegate #for speed
22+
def __getattr__(self, name):
23+
24+
if old_getattr:
25+
try:
26+
print "using old getattr"
27+
return old_getattr(self, name)
28+
except AttributeError:
29+
pass
30+
return getattr(delegate_bind, name)
31+
cls.__getattr__ = __getattr__
32+
cls.__call__ = self._delegate_call('__call__')
33+
return cls
34+
35+
@property
36+
def new(self):
37+
if not self._replacement:
38+
@self._decorate
39+
class PrototypeChild(object):
40+
pass
41+
self._replacement = PrototypeChild()
42+
return self._replacement
43+
44+
def __call__(self, cls):
45+
if getattr(self._delegate, '__call__', False) and not self._extend:
46+
raise PrototypeException("You attempted to use a prototype as a decorator, but the object you're extending already has a __call__ method. You will need to explicitly use the .extend syntax like this: @prototype(obj, True) in this case. You will see this error on multi-level prototypes")
47+
return self._decorate(cls)
48+
49+
def __getattr__(self, name):
50+
if getattr(self._delegate, 'new', False):
51+
raise PrototypeException("You are using a raw prototype as a clone, but the parent defined a new attribute. You must explicitly instantiate using @prototype(obj).new in this case. You will see this error on multi-level prototypes.")
52+
return getattr(self.new, name)
53+
54+
55+
def prototype(delegate, extend=False):
56+
"""
57+
Implement javascript prototypal inheritance in python. This
58+
implementation will allow you to create new prototypes on the fly,
59+
or set prototypes for classes.
60+
61+
Usage:
62+
63+
You can use it to make lightweight children from an existing object.
64+
65+
obj = Parent()
66+
obj_child = prototype(obj)
67+
68+
You can use it to make static prototypes for classes.
69+
70+
obj = Parent()
71+
72+
@prototype(obj)
73+
class Child(object):
74+
pass
75+
76+
obj2 = Child()
77+
78+
In some rare cases you must explicitly force a clone or a
79+
decorator to be created. If the object contains a `new` property
80+
you must explicitly call .new to force a clone to be made.
81+
82+
obj = Parent()
83+
obj.new = 1
84+
85+
obj_child = prototype(obj).new
86+
87+
88+
If the object contains a `__call__` property, you must explicitly
89+
create a decorator with the extend=True keyword argument.
90+
91+
obj = Parent()
92+
obj.__call__ = 1
93+
94+
@prototype(obj, extend=True)
95+
class Child(object):
96+
pass
97+
98+
Prototypes can extend prototypes as many levels as you want. One note,
99+
prototypes extending prototypes must use .new and extend=True.
100+
101+
obj = Parent()
102+
child = prototype(obj)
103+
grandchild = prototype(obj).new
104+
"""
105+
return PrototypeSwitcher(delegate, extend)
106+

prototype/prototype.pyc

4.57 KB
Binary file not shown.

prototype/test_helper.py

Lines changed: 254 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,254 @@
1+
#this is an insane class that
2+
3+
class FullyPython(object):
4+
"""docs go here"""
5+
6+
7+
def __init__(self):
8+
pass
9+
10+
def __del__(self):
11+
pass
12+
13+
def __repr__(self):
14+
pass
15+
16+
def __str__(self):
17+
pass
18+
19+
def __lt__(self):
20+
pass
21+
22+
def __le__(self):
23+
pass
24+
25+
def __eq__(self):
26+
pass
27+
28+
def __ne__(self):
29+
pass
30+
31+
def __gt__(self):
32+
pass
33+
34+
def __ge__(self):
35+
pass
36+
37+
def __cmp__(self):
38+
pass
39+
40+
def __rcmp__(self):
41+
pass
42+
43+
def __hash__(self):
44+
pass
45+
46+
def __nonzero__(self):
47+
return True
48+
49+
def __unicode__(self):
50+
pass
51+
52+
def __setattr__(self):
53+
pass
54+
55+
def __delattr__(self):
56+
pass
57+
58+
def __call__(self):
59+
pass
60+
61+
def __len__(self):
62+
pass
63+
64+
def __getitem__(self, k):
65+
pass
66+
67+
def __setitem__(self, k, v):
68+
pass
69+
70+
def __delitem__(self, key):
71+
pass
72+
73+
def __iter__(self):
74+
pass
75+
76+
def __reversed__(self):
77+
pass
78+
79+
def __contains__(self, item):
80+
pass
81+
82+
def __getslice__(self,i,j):
83+
pass
84+
85+
def __setslice__(self, i,j,seq):
86+
pass
87+
88+
def __delslice__(self,i,j):
89+
pass
90+
91+
def __add__(self,e):
92+
pass
93+
94+
def __sub__(self,e):
95+
pass
96+
97+
def __mul__(self,e):
98+
pass
99+
100+
def _floordiv__(self,e):
101+
pass
102+
103+
def __mod__(self,e):
104+
pass
105+
106+
def __divmod__(self,e):
107+
pass
108+
109+
def __pow__(self,e):
110+
pass
111+
112+
def __lshift__(self,e):
113+
pass
114+
115+
def __rshift__(self,e):
116+
pass
117+
118+
def __and__(self,e):
119+
pass
120+
121+
def __xor__(self,e):
122+
pass
123+
124+
def __or__(self,e):
125+
pass
126+
127+
def __div__(self,e):
128+
pass
129+
130+
def __truediv__(self,e):
131+
pass
132+
133+
def __radd__(self,e):
134+
pass
135+
136+
def __rsub__(self,e):
137+
pass
138+
139+
def __rmul__(self,e):
140+
pass
141+
142+
def __rdiv__(self,e):
143+
pass
144+
145+
def __rtruediv__(self,e):
146+
pass
147+
148+
def __rfloordiv__(self,e):
149+
pass
150+
151+
def __rmod__(self,e):
152+
pass
153+
154+
def __rdivmod__(self,e):
155+
pass
156+
157+
def __rpow__(self,exe):
158+
pass
159+
160+
def __rlshift__(self,e):
161+
pass
162+
163+
def __rrshift__(self,e):
164+
pass
165+
166+
def __rand__(self,e):
167+
pass
168+
169+
def __rxor__(self,e):
170+
pass
171+
172+
def __ror__(self,e):
173+
pass
174+
175+
def __iadd__(self, o):
176+
pass
177+
178+
def __isub__(self,o):
179+
pass
180+
181+
def __imul__(self,e):
182+
pass
183+
184+
def __idiv__(self,o):
185+
pass
186+
187+
def __itruediv__(self,o):
188+
pass
189+
190+
def __ifloordiv__(self,o):
191+
pass
192+
193+
def __imod__(self,o):
194+
pass
195+
196+
def __ipow__(self,o):
197+
pass
198+
199+
def __ilshift__(self,o):
200+
pass
201+
202+
def __irshift__(self,o):
203+
pass
204+
205+
def __iand__(self,o ):
206+
pass
207+
208+
def __ixor__(self, o):
209+
pass
210+
211+
def __ior__(self):
212+
pass
213+
214+
def __neg__(self):
215+
pass
216+
217+
def __pos__(self):
218+
pass
219+
220+
def __abs__(self):
221+
pass
222+
223+
def __invert__(self):
224+
pass
225+
226+
def __complex__(self):
227+
pass
228+
229+
def __int__(self):
230+
pass
231+
232+
def __long__(self):
233+
pass
234+
235+
def __float__(self):
236+
pass
237+
238+
def __oct__(self):
239+
pass
240+
241+
def __hex__(self):
242+
pass
243+
244+
def __index__(self):
245+
pass
246+
247+
def __coerce__(self, other):
248+
pass
249+
250+
def __enter__(self):
251+
pass
252+
253+
def __exit__(self, p2, p3, p4):
254+
pass

prototype/test_helper.pyc

13.1 KB
Binary file not shown.

0 commit comments

Comments
 (0)