Created
August 23, 2013 19:42
-
-
Save justinvanwinkle/6323202 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
bad_attrs = ('__class__', '__doc__', '__init__', '__new__', | |
'__getattr__', '__getattribute__', '__subclasshook__', | |
'__str__', '__repr__', '__hash__', '__setattr__') | |
good_attrs = ('append',) | |
def get_ops(o): | |
ops = [x for x in dir(o) if | |
(x.startswith('__') and x not in bad_attrs) or | |
x in good_attrs] | |
return ops | |
ops = set(get_ops([]) + get_ops({}) + get_ops(()) + get_ops(1) + get_ops("")) | |
def caller_maker(op): | |
def caller(self, *args): | |
return getattr(self.val, op)(*args) | |
caller.func_name = op | |
return caller | |
def OpDecorator(cls): | |
for op in ops: | |
setattr(cls, op, caller_maker(op)) | |
return cls | |
@OpDecorator | |
class Faker(object): | |
def __init__(self): | |
self.val = None | |
i1, i2 = Faker(), Faker() | |
i1.val = 1 | |
i2.val = 3 | |
#assert i1 + i2 == 3 | |
assert i1 + 1 == 2 | |
assert 1 + i1 == 2 | |
i1.val = [] | |
i1.append(1) | |
assert len(i1) == 1 | |
assert i1[0] == 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What about the simple statement: