... self.saved = False |
... self.saved = False |
... def save_if_needed(self): |
... def save_if_needed(self): |
... if not self.saved: |
... if not self.saved: |
... print "saving" |
... print("saving") |
... self.saved = True |
... self.saved = True |
|
|
>>> class Thing: pass |
>>> class Thing: pass |
|
|
>>> something = Thing() |
>>> something = Thing() |
>>> Index(something, "x>y")["a"] = "b" |
>>> Index(something, "x>y")["a"] = "b" |
>>> dir(something) |
>>> [key for key in something.__dict__ if not isinstance(key, str)] |
['__doc__', '__module__', (<class 'Index'>, 'x>y')] |
[(<class 'Index'>, 'x>y')] |
|
|
>>> "a" in Index(something, "z<22") |
>>> "a" in Index(something, "z<22") |
False |
False |
>>> Index(something, "x>y").expression |
>>> Index(something, "x>y").expression |
'x>y' |
'x>y' |
|
|
>>> dir(something) |
>>> keys = [key for key in something.__dict__ if not isinstance(key, str)] |
['__doc__', '__module__', (<class 'Index'>, 'x>y'), (<class 'Index'>, 'z<22')] |
>>> keys.sort() |
|
>>> keys |
|
[(<class 'Index'>, 'x>y'), (<class 'Index'>, 'z<22')] |
|
|
>>> Index.exists_for(something, 'x>y') |
>>> Index.exists_for(something, 'x>y') |
True |
True |
>>> Leech(something, 42) |
>>> Leech(something, 42) |
Traceback (most recent call last): |
Traceback (most recent call last): |
... |
... |
TypeError: addon_key() takes exactly 1 argument (2 given) |
TypeError: addon_key() takes exactly 1 ...argument (2 given) |
|
|
Naturally, your ``addon_key()`` and ``__init__()`` (and/or ``__new__()``) |
Naturally, your ``addon_key()`` and ``__init__()`` (and/or ``__new__()``) |
methods should also agree on how many arguments there can be, and what they |
methods should also agree on how many arguments there can be, and what they |
usually be deleted as soon as the subject is:: |
usually be deleted as soon as the subject is:: |
|
|
>>> def deleting(r): |
>>> def deleting(r): |
... print "deleting", r |
... print("deleting "+str(r)) |
|
|
>>> from weakref import ref |
>>> from weakref import ref |
|
|
|
|
>>> class SpecialMethodRegistry(ClassAddOn): |
>>> class SpecialMethodRegistry(ClassAddOn): |
... def __init__(self, subject): |
... def __init__(self, subject): |
... print "init called for", subject |
... print("init called for "+str(subject)) |
... self.special_methods = {} |
... self.special_methods = {} |
... super(SpecialMethodRegistry, self).__init__(subject) |
... super(SpecialMethodRegistry, self).__init__(subject) |
... |
... |
... def created_for(self, cls): |
... def created_for(self, cls): |
... print "created for", cls.__name__ |
... print("created for "+cls.__name__) |
|
|
>>> class Demo: |
>>> class Demo: |
... def dummy(self, foo): |
... def dummy(self, foo): |
the subject is not ``None``:: |
the subject is not ``None``:: |
|
|
>>> SpecialMethodRegistry(float) |
>>> SpecialMethodRegistry(float) |
init called for <type 'float'> |
init called for <... 'float'> |
created for float |
created for float |
<SpecialMethodRegistry object at ...> |
<SpecialMethodRegistry object at ...> |
|
|
... from threading import RLock |
... from threading import RLock |
... self.lock = RLock() |
... self.lock = RLock() |
... def acquire(self): |
... def acquire(self): |
... print "acquiring" |
... print("acquiring") |
... self.lock.acquire() |
... self.lock.acquire() |
... def release(self): |
... def release(self): |
... self.lock.release() |
... self.lock.release() |
... print "released" |
... print("released") |
|
|
>>> def synchronized(func): |
>>> def synchronized(func): |
... def wrapper(self, *__args,**__kw): |
... def wrapper(self, *__args,**__kw): |
|
|
>>> class AnotherThing: |
>>> class AnotherThing: |
... def ping(self): |
... def ping(self): |
... print "ping" |
... print("ping") |
... ping = synchronized(ping) |
... ping = synchronized(ping) |
|
|
>>> AnotherThing().ping() |
>>> AnotherThing().ping() |