Skip to content

Commit c35e9e5

Browse files
authored
Merge pull request #105 from sbillinge/f8-2
fix: flake8 for utils dir
2 parents 417e57e + 31df321 commit c35e9e5

File tree

5 files changed

+24
-41
lines changed

5 files changed

+24
-41
lines changed

src/diffpy/srfit/util/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020

2121
def sortKeyForNumericString(s):
22-
"""\ Compute key for sorting strings according to their integer numeric
22+
"""Compute key for sorting strings according to their integer numeric
2323
value.
2424
2525
Each string gets split to string and integer segments to create keys
@@ -45,6 +45,7 @@ def sortKeyForNumericString(s):
4545
for i, w in enumerate(rx.split(s)))
4646
return rv
4747

48+
4849
sortKeyForNumericString._rx = None
4950

5051
# End of file

src/diffpy/srfit/util/nameutils.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ def isIdentifier(s):
2727
2828
From http://code.activestate.com/recipes/413487/
2929
"""
30-
if reident.match(s) is None: return False
30+
if reident.match(s) is None:
31+
return False
3132
return True
3233

3334

@@ -38,7 +39,7 @@ def validateName(name):
3839
"""
3940
# Check that the name is valid
4041
if not isIdentifier(name):
41-
raise ValueError("Name '%s' is not a valid identifier"%name)
42+
raise ValueError(f"Name {name} is not a valid identifier")
4243
return
4344

4445
# End of file

src/diffpy/srfit/util/observable.py

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,56 +21,54 @@ class Observable(object):
2121
"""Provide notification support for classes that maintain dynamic
2222
associations with multiple clients.
2323
24-
Observers, i.e. clients of the observable, register event handlers that will be invoked to
25-
notify them whenever something interesting happens to the observable. The nature of what is
26-
being observed is defined by Observable descendants and their managers. For example,
27-
instances of pyre.calc.Node are observable by other nodes whose value depends on them so
28-
that the dependents can be notified about value changes and forced to recompute their own
29-
value.
24+
Observers, i.e. clients of the observable, register event handlers
25+
that will be invoked to notify them whenever something interesting
26+
happens to the observable. The nature of what is being observed is
27+
defined by Observable descendants and their managers. For example,
28+
instances of pyre.calc.Node are observable by other nodes whose
29+
value depends on them so that the dependents can be notified about
30+
value changes and forced to recompute their own value.
3031
31-
The event handlers are callables that take the observable instance as their single
32-
argument.
32+
The event handlers are callables that take the observable instance
33+
as their single argument.
3334
3435
interface:
35-
addObserver: registers its callable argument with the list of handlers to invoke
36-
removeObserver: remove an event handler from the list of handlers to invoke
37-
notify: invoke the registered handlers in the order in which they were registered
36+
addObserver: registers its callable argument with the list of
37+
handlers to invoke removeObserver: remove an event handler from
38+
the list of handlers to invoke notify: invoke the registered
39+
handlers in the order in which they were registered
3840
"""
39-
40-
4141
def notify(self, other=()):
4242
"""Notify all observers."""
43-
# build a list before notification, just in case the observer's callback behavior
44-
# involves removing itself from our callback set
43+
# build a list before notification, just in case the observer's
44+
# callback behavior involves removing itself from our callback set
4545
semaphors = (self,) + other
4646
for callable in tuple(self._observers):
4747
callable(semaphors)
4848
return
4949

50-
5150
# callback management
51+
5252
def addObserver(self, callable):
5353
"""Add callable to the set of observers."""
5454
f = weak_ref(callable, fallback=_fbRemoveObserver)
5555
self._observers.add(f)
5656
return
5757

58-
5958
def removeObserver(self, callable):
6059
"""Remove callable from the set of observers."""
6160
f = weak_ref(callable)
6261
self._observers.remove(f)
6362
return
6463

65-
6664
def hasObserver(self, callable):
6765
"""True if `callable` is present in the set of observers."""
6866
f = weak_ref(callable)
6967
rv = f in self._observers
7068
return rv
7169

72-
7370
# meta methods
71+
7472
def __init__(self, **kwds):
7573
super(Observable, self).__init__(**kwds)
7674
self._observers = set()
@@ -80,6 +78,7 @@ def __init__(self, **kwds):
8078

8179
# Local helpers --------------------------------------------------------------
8280

81+
8382
def _fbRemoveObserver(fobs, semaphors):
8483
# Remove WeakBoundMethod `fobs` from the observers of notifying object.
8584
# This is called from Observable.notify when the WeakBoundMethod

src/diffpy/srfit/util/tagmanager.py

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,10 @@ def __init__(self):
4141
self.silent = True
4242
return
4343

44-
4544
def alltags(self):
4645
"""Get all tags managed by the TagManager."""
4746
return self._tagdict.keys()
4847

49-
5048
def tag(self, obj, *tags):
5149
"""Tag an object.
5250
@@ -62,7 +60,6 @@ def tag(self, obj, *tags):
6260
oset.add(obj)
6361
return
6462

65-
6663
def untag(self, obj, *tags):
6764
"""Remove tags from an object.
6865
@@ -84,7 +81,6 @@ def untag(self, obj, *tags):
8481

8582
return
8683

87-
8884
def tags(self, obj):
8985
"""Get all tags on an object.
9086
@@ -93,7 +89,6 @@ def tags(self, obj):
9389
tags = [k for (k, v) in self._tagdict.items() if obj in v]
9490
return tags
9591

96-
9792
def hasTags(self, obj, *tags):
9893
"""Determine if an object has all passed tags.
9994
@@ -103,7 +98,6 @@ def hasTags(self, obj, *tags):
10398
result = all(obj in s for s in setgen)
10499
return result
105100

106-
107101
def union(self, *tags):
108102
"""Get all objects that have any of the passed tags.
109103
@@ -115,7 +109,6 @@ def union(self, *tags):
115109
objs = functools.reduce(set.union, setgen)
116110
return objs
117111

118-
119112
def intersection(self, *tags):
120113
"""Get all objects that have all of the passed tags.
121114
@@ -127,7 +120,6 @@ def intersection(self, *tags):
127120
objs = functools.reduce(set.intersection, setgen)
128121
return objs
129122

130-
131123
def verifyTags(self, *tags):
132124
"""Check that tags are all extant.
133125
@@ -138,10 +130,8 @@ def verifyTags(self, *tags):
138130
for tag in tags:
139131
if tag not in keys:
140132
raise KeyError("Tag '%s' does not exist" % tag)
141-
142133
return True
143134

144-
145135
def __getObjectSet(self, tag):
146136
"""Helper function for getting an object set with given tag.
147137
@@ -154,7 +144,6 @@ def __getObjectSet(self, tag):
154144
oset = set()
155145
return oset
156146

157-
158147
# End class TagManager
159148

160149
# End of file

src/diffpy/srfit/util/weakrefcallable.py

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424

2525
class WeakBoundMethod(object):
26-
"""\ Callable wrapper to a bound method stored as a weak reference.
26+
"""Callable wrapper to a bound method stored as a weak reference.
2727
2828
Support storage of bound methods without keeping the associated objects
2929
alive forever. Provide facility for a fallback function to be used
@@ -69,7 +69,6 @@ def __init__(self, f, fallback=None):
6969
self._wref = weakref.ref(f.__self__)
7070
return
7171

72-
7372
def __call__(self, *args, **kwargs):
7473
"""Call the wrapped method if the weak-referenced object is alive.
7574
@@ -95,20 +94,17 @@ def __call__(self, *args, **kwargs):
9594
emsg = "Object bound to {} does not exist.".format(self.function)
9695
raise ReferenceError(emsg)
9796

98-
9997
# support use of this class in hashed collections
10098

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

104-
105102
def __eq__(self, other):
106103
rv = (self.function == other.function and
107104
(self._wref == other._wref or
108105
None is self._wref() is other._wref()))
109106
return rv
110107

111-
112108
def __ne__(self, other):
113109
return not self.__eq__(other)
114110

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

129-
130125
def __setstate__(self, state):
131126
"""Restore the weak reference in this wrapper upon unpickling."""
132127
(self._class, nm, self.fallback, mobj) = state
@@ -139,14 +134,12 @@ def __setstate__(self, state):
139134
self._wref = weakref.ref(mobj)
140135
return
141136

142-
143137
@staticmethod
144138
def __mimic_empty_ref():
145139
return None
146140

147141
# end of class WeakBoundMethod
148142

149-
# ----------------------------------------------------------------------------
150143

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

0 commit comments

Comments
 (0)