Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/diffpy/srfit/util/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@


def sortKeyForNumericString(s):
"""\ Compute key for sorting strings according to their integer numeric
"""Compute key for sorting strings according to their integer numeric
value.

Each string gets split to string and integer segments to create keys
Expand All @@ -45,6 +45,7 @@ def sortKeyForNumericString(s):
for i, w in enumerate(rx.split(s)))
return rv


sortKeyForNumericString._rx = None

# End of file
5 changes: 3 additions & 2 deletions src/diffpy/srfit/util/nameutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ def isIdentifier(s):

From http://code.activestate.com/recipes/413487/
"""
if reident.match(s) is None: return False
if reident.match(s) is None:
return False
return True


Expand All @@ -38,7 +39,7 @@ def validateName(name):
"""
# Check that the name is valid
if not isIdentifier(name):
raise ValueError("Name '%s' is not a valid identifier"%name)
raise ValueError(f"Name {name} is not a valid identifier")
return

# End of file
37 changes: 18 additions & 19 deletions src/diffpy/srfit/util/observable.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,56 +21,54 @@ class Observable(object):
"""Provide notification support for classes that maintain dynamic
associations with multiple clients.

Observers, i.e. clients of the observable, register event handlers that will be invoked to
notify them whenever something interesting happens to the observable. The nature of what is
being observed is defined by Observable descendants and their managers. For example,
instances of pyre.calc.Node are observable by other nodes whose value depends on them so
that the dependents can be notified about value changes and forced to recompute their own
value.
Observers, i.e. clients of the observable, register event handlers
that will be invoked to notify them whenever something interesting
happens to the observable. The nature of what is being observed is
defined by Observable descendants and their managers. For example,
instances of pyre.calc.Node are observable by other nodes whose
value depends on them so that the dependents can be notified about
value changes and forced to recompute their own value.

The event handlers are callables that take the observable instance as their single
argument.
The event handlers are callables that take the observable instance
as their single argument.

interface:
addObserver: registers its callable argument with the list of handlers to invoke
removeObserver: remove an event handler from the list of handlers to invoke
notify: invoke the registered handlers in the order in which they were registered
addObserver: registers its callable argument with the list of
handlers to invoke removeObserver: remove an event handler from
the list of handlers to invoke notify: invoke the registered
handlers in the order in which they were registered
"""


def notify(self, other=()):
"""Notify all observers."""
# build a list before notification, just in case the observer's callback behavior
# involves removing itself from our callback set
# build a list before notification, just in case the observer's
# callback behavior involves removing itself from our callback set
semaphors = (self,) + other
for callable in tuple(self._observers):
callable(semaphors)
return


# callback management

def addObserver(self, callable):
"""Add callable to the set of observers."""
f = weak_ref(callable, fallback=_fbRemoveObserver)
self._observers.add(f)
return


def removeObserver(self, callable):
"""Remove callable from the set of observers."""
f = weak_ref(callable)
self._observers.remove(f)
return


def hasObserver(self, callable):
"""True if `callable` is present in the set of observers."""
f = weak_ref(callable)
rv = f in self._observers
return rv


# meta methods

def __init__(self, **kwds):
super(Observable, self).__init__(**kwds)
self._observers = set()
Expand All @@ -80,6 +78,7 @@ def __init__(self, **kwds):

# Local helpers --------------------------------------------------------------


def _fbRemoveObserver(fobs, semaphors):
# Remove WeakBoundMethod `fobs` from the observers of notifying object.
# This is called from Observable.notify when the WeakBoundMethod
Expand Down
11 changes: 0 additions & 11 deletions src/diffpy/srfit/util/tagmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,10 @@ def __init__(self):
self.silent = True
return


def alltags(self):
"""Get all tags managed by the TagManager."""
return self._tagdict.keys()


def tag(self, obj, *tags):
"""Tag an object.

Expand All @@ -62,7 +60,6 @@ def tag(self, obj, *tags):
oset.add(obj)
return


def untag(self, obj, *tags):
"""Remove tags from an object.

Expand All @@ -84,7 +81,6 @@ def untag(self, obj, *tags):

return


def tags(self, obj):
"""Get all tags on an object.

Expand All @@ -93,7 +89,6 @@ def tags(self, obj):
tags = [k for (k, v) in self._tagdict.items() if obj in v]
return tags


def hasTags(self, obj, *tags):
"""Determine if an object has all passed tags.

Expand All @@ -103,7 +98,6 @@ def hasTags(self, obj, *tags):
result = all(obj in s for s in setgen)
return result


def union(self, *tags):
"""Get all objects that have any of the passed tags.

Expand All @@ -115,7 +109,6 @@ def union(self, *tags):
objs = functools.reduce(set.union, setgen)
return objs


def intersection(self, *tags):
"""Get all objects that have all of the passed tags.

Expand All @@ -127,7 +120,6 @@ def intersection(self, *tags):
objs = functools.reduce(set.intersection, setgen)
return objs


def verifyTags(self, *tags):
"""Check that tags are all extant.

Expand All @@ -138,10 +130,8 @@ def verifyTags(self, *tags):
for tag in tags:
if tag not in keys:
raise KeyError("Tag '%s' does not exist" % tag)

return True


def __getObjectSet(self, tag):
"""Helper function for getting an object set with given tag.

Expand All @@ -154,7 +144,6 @@ def __getObjectSet(self, tag):
oset = set()
return oset


# End class TagManager

# End of file
9 changes: 1 addition & 8 deletions src/diffpy/srfit/util/weakrefcallable.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@


class WeakBoundMethod(object):
"""\ Callable wrapper to a bound method stored as a weak reference.
"""Callable wrapper to a bound method stored as a weak reference.

Support storage of bound methods without keeping the associated objects
alive forever. Provide facility for a fallback function to be used
Expand Down Expand Up @@ -69,7 +69,6 @@ def __init__(self, f, fallback=None):
self._wref = weakref.ref(f.__self__)
return


def __call__(self, *args, **kwargs):
"""Call the wrapped method if the weak-referenced object is alive.

Expand All @@ -95,20 +94,17 @@ def __call__(self, *args, **kwargs):
emsg = "Object bound to {} does not exist.".format(self.function)
raise ReferenceError(emsg)


# support use of this class in hashed collections

def __hash__(self):
return hash((self.function, self._wref))


def __eq__(self, other):
rv = (self.function == other.function and
(self._wref == other._wref or
None is self._wref() is other._wref()))
return rv


def __ne__(self, other):
return not self.__eq__(other)

Expand All @@ -126,7 +122,6 @@ def __getstate__(self):
state = (self._class, nm, self.fallback, mobj)
return state


def __setstate__(self, state):
"""Restore the weak reference in this wrapper upon unpickling."""
(self._class, nm, self.fallback, mobj) = state
Expand All @@ -139,14 +134,12 @@ def __setstate__(self, state):
self._wref = weakref.ref(mobj)
return


@staticmethod
def __mimic_empty_ref():
return None

# end of class WeakBoundMethod

# ----------------------------------------------------------------------------

def weak_ref(f, fallback=None):
"""Create weak-reference wrapper to a bound method.
Expand Down