Skip to content

Commit 054c1ea

Browse files
committed
Typo corrections, docstrings, naming
1 parent 1b9a0b3 commit 054c1ea

File tree

1 file changed

+23
-12
lines changed

1 file changed

+23
-12
lines changed

traitlets/traitlets.py

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -547,8 +547,12 @@ def tag(self, **metadata):
547547
# The HasTraits implementation
548548
#-----------------------------------------------------------------------------
549549

550-
class _CbWrapper(object):
550+
class _CallbackWrapper(object):
551+
"""An object adapting a on_trait_change callback into an observe callback.
551552
553+
The comparison operator __eq__ is implemented to enable removal of wrapped
554+
callbacks.
555+
"""
552556

553557
def __init__(self, cb):
554558
if callable(cb):
@@ -563,13 +567,13 @@ def __init__(self, cb):
563567

564568
def __eq__(self, other):
565569
# The wrapper is equal to the wrapped element
566-
if isinstance(other, _CbWrapper):
570+
if isinstance(other, _CallbackWrapper):
567571
return self.cb == other.cb
568572
else:
569573
return self.cb == other
570574

571575
def __call__(self, change):
572-
# The wrapper is callabled
576+
# The wrapper is callable
573577
if self.nargs == 0:
574578
self.cb()
575579
elif self.nargs == 1:
@@ -581,11 +585,11 @@ def __call__(self, change):
581585
elif self.nargs == 4:
582586
self.cb(change['name'], change['old'], change['new'], change['object'])
583587

584-
def _cb_wrapper(cb):
585-
if isinstance(cb, _CbWrapper):
588+
def _callback_wrapper(cb):
589+
if isinstance(cb, _CallbackWrapper):
586590
return cb
587591
else:
588-
return _CbWrapper(cb)
592+
return _CallbackWrapper(cb)
589593

590594

591595

@@ -630,6 +634,13 @@ class dict to the newly created class ``cls``.
630634

631635

632636
def observe(*names):
637+
""" A decorator which can be used to observe members on a class.
638+
639+
Parameters
640+
----------
641+
*names
642+
The str names of the attributes to observe on the object.
643+
"""
633644
return ObserveHandler(names)
634645

635646

@@ -762,7 +773,7 @@ def _notify_trait(self, name, old_value, new_value):
762773
else:
763774
warn("_[traitname]_changed change handlers are deprecated: use observe instead",
764775
DeprecationWarning, stacklevel=2)
765-
callables.append(_cb_wrapper(cb))
776+
callables.append(_callback_wrapper(cb))
766777

767778
# Call them all now
768779
for c in callables:
@@ -771,8 +782,8 @@ def _notify_trait(self, name, old_value, new_value):
771782
# Bound methods have an additional 'self' argument.
772783
offset = -1 if isinstance(c, types.MethodType) else 0
773784

774-
if isinstance(c, _CbWrapper):
775-
# _CbWrappers are not compatible with getargspec and have one argument
785+
if isinstance(c, _CallbackWrapper):
786+
# _CallbackWrappers are not compatible with getargspec and have one argument
776787
nargs = 1
777788
else:
778789
nargs = len(getargspec(c)[0]) + offset
@@ -831,7 +842,7 @@ def on_trait_change(self, handler, name=None, remove=False):
831842
"""
832843
warn("on_trait_change is deprecated: use observe instead",
833844
DeprecationWarning, stacklevel=2)
834-
self.observe(_cb_wrapper(handler), name=name, remove=remove)
845+
self.observe(_callback_wrapper(handler), name=name, remove=remove)
835846

836847
def observe(self, handler, name=None, remove=False):
837848
"""Setup a handler to be called when a trait changes.
@@ -847,14 +858,14 @@ def observe(self, handler, name=None, remove=False):
847858
- object : the HasTraits instance
848859
- old : the old value of the modified trait attribute
849860
- new : the new value of the modified trait attribute
850-
- name : the name ofthe modified trait attribute.
861+
- name : the name of the modified trait attribute.
851862
name : list, str, None
852863
If None, the handler will apply to all traits. If a list
853864
of str, handler will apply to all names in the list. If a
854865
str, the handler will apply just to that name.
855866
remove : bool
856867
If False (the default), then install the handler. If True
857-
then unintall it.
868+
then uninstall it.
858869
"""
859870
if remove:
860871
names = parse_notifier_name(name)

0 commit comments

Comments
 (0)