From e524c826db12ffed029d627bd4afcfc03e7f899a Mon Sep 17 00:00:00 2001 From: Jason Gerard DeRose Date: Thu, 18 Sep 2008 21:45:25 +0000 Subject: 297: Added a better example in docstring for ReadOnly --- ipalib/plugable.py | 37 ++++++++++++++++++------------------- 1 file changed, 18 insertions(+), 19 deletions(-) (limited to 'ipalib/plugable.py') diff --git a/ipalib/plugable.py b/ipalib/plugable.py index dfefbe16..4e356783 100644 --- a/ipalib/plugable.py +++ b/ipalib/plugable.py @@ -48,26 +48,25 @@ class ReadOnly(object): For example: - >>> class givenname(ReadOnly): - >>> def __init__(self): - >>> self.whatever = 'some value' # Hasn't been locked yet - >>> lock(self) - >>> - >>> def finalize(self, api): - >>> # After the instance has been locked, attributes can still be - >>> # set, but only in a round-about, unconventional way: - >>> object.__setattr__(self, 'api', api) - >>> - >>> def normalize(self, value): - >>> # After the instance has been locked, trying to set an - >>> # attribute in the normal way will raise AttributeError. - >>> self.value = value # Not thread safe! - >>> return self.actually_normalize() - >>> - >>> def actually_normalize(self): - >>> # Again, this is not thread safe: - >>> return unicode(self.value).strip() + >>> ro = ReadOnly() # Initially unlocked, can setattr, delattr + >>> ro.name = 'John Doe' + >>> ro.message = 'Hello, world!' + >>> del ro.message + >>> ro.__lock__() # Now locked, cannot setattr, delattr + >>> ro.message = 'How are you?' + Traceback (most recent call last): + File "", line 1, in + File "/home/jderose/projects/freeipa2/ipalib/plugable.py", line 93, in __setattr__ + (self.__class__.__name__, name) + AttributeError: read-only: cannot set ReadOnly.message + >>> del ro.name + Traceback (most recent call last): + File "", line 1, in + File "/home/jderose/projects/freeipa2/ipalib/plugable.py", line 104, in __delattr__ + (self.__class__.__name__, name) + AttributeError: read-only: cannot del ReadOnly.name """ + __locked = False def __lock__(self): -- cgit