From a95d12c54ad7fe696deb6f16183e3ecb078bf92c Mon Sep 17 00:00:00 2001
From: Maxima <maxime.arthaud@gmail.com>
Date: Mon, 24 Jun 2013 21:02:53 +0200
Subject: [PATCH] PEP 8 compliance.
---
django/dispatch/saferef.py | 34 +++++++++++++++++-----------------
1 file changed, 17 insertions(+), 17 deletions(-)
diff --git a/django/dispatch/saferef.py b/django/dispatch/saferef.py
index c7731d4..d27120c 100644
a
|
b
|
def safeRef(target, onDelete = None):
|
23 | 23 | if target.__self__ is not None: |
24 | 24 | # Turn a bound method into a BoundMethodWeakref instance. |
25 | 25 | # Keep track of these instances for lookup by disconnect(). |
26 | | assert hasattr(target, '__func__'), """safeRef target %r has __self__, but no __func__, don't know how to create reference"""%( target,) |
| 26 | assert hasattr(target, '__func__'), """safeRef target %r has __self__, but no __func__, don't know how to create reference""" % (target,) |
27 | 27 | reference = get_bound_method_weakref( |
28 | 28 | target=target, |
29 | 29 | onDelete=onDelete |
… |
… |
def safeRef(target, onDelete = None):
|
32 | 32 | if callable(onDelete): |
33 | 33 | return weakref.ref(target, onDelete) |
34 | 34 | else: |
35 | | return weakref.ref( target ) |
| 35 | return weakref.ref(target) |
36 | 36 | |
37 | 37 | class BoundMethodWeakref(object): |
38 | 38 | """'Safe' and reusable weak references to instance methods |
… |
… |
class BoundMethodWeakref(object):
|
70 | 70 | |
71 | 71 | _allInstances = weakref.WeakValueDictionary() |
72 | 72 | |
73 | | def __new__( cls, target, onDelete=None, *arguments,**named ): |
| 73 | def __new__(cls, target, onDelete=None, *arguments, **named): |
74 | 74 | """Create new instance or return current instance |
75 | 75 | |
76 | 76 | Basically this method of construction allows us to |
… |
… |
class BoundMethodWeakref(object):
|
83 | 83 | of already-referenced methods. |
84 | 84 | """ |
85 | 85 | key = cls.calculateKey(target) |
86 | | current =cls._allInstances.get(key) |
| 86 | current = cls._allInstances.get(key) |
87 | 87 | if current is not None: |
88 | | current.deletionMethods.append( onDelete) |
| 88 | current.deletionMethods.append(onDelete) |
89 | 89 | return current |
90 | 90 | else: |
91 | | base = super( BoundMethodWeakref, cls).__new__( cls ) |
| 91 | base = super(BoundMethodWeakref, cls).__new__(cls) |
92 | 92 | cls._allInstances[key] = base |
93 | | base.__init__( target, onDelete, *arguments,**named) |
| 93 | base.__init__(target, onDelete, *arguments, **named) |
94 | 94 | return base |
95 | 95 | |
96 | 96 | def __init__(self, target, onDelete=None): |
… |
… |
class BoundMethodWeakref(object):
|
112 | 112 | methods = self.deletionMethods[:] |
113 | 113 | del self.deletionMethods[:] |
114 | 114 | try: |
115 | | del self.__class__._allInstances[ self.key ] |
| 115 | del self.__class__._allInstances[self.key] |
116 | 116 | except KeyError: |
117 | 117 | pass |
118 | 118 | for function in methods: |
119 | 119 | try: |
120 | | if callable( function ): |
121 | | function( self ) |
| 120 | if callable(function): |
| 121 | function(self) |
122 | 122 | except Exception as e: |
123 | 123 | try: |
124 | 124 | traceback.print_exc() |
… |
… |
class BoundMethodWeakref(object):
|
127 | 127 | self, function, e) |
128 | 128 | ) |
129 | 129 | self.deletionMethods = [onDelete] |
130 | | self.key = self.calculateKey( target ) |
| 130 | self.key = self.calculateKey(target) |
131 | 131 | self.weakSelf = weakref.ref(target.__self__, remove) |
132 | 132 | self.weakFunc = weakref.ref(target.__func__, remove) |
133 | 133 | self.selfName = str(target.__self__) |
134 | 134 | self.funcName = str(target.__func__.__name__) |
135 | 135 | |
136 | | def calculateKey( cls, target ): |
| 136 | def calculateKey(cls, target): |
137 | 137 | """Calculate the reference key for this reference |
138 | 138 | |
139 | 139 | Currently this is a two-tuple of the id()'s of the |
140 | 140 | target object and the target function respectively. |
141 | 141 | """ |
142 | | return (id(target.__self__),id(target.__func__)) |
143 | | calculateKey = classmethod( calculateKey ) |
| 142 | return (id(target.__self__), id(target.__func__)) |
| 143 | calculateKey = classmethod(calculateKey) |
144 | 144 | |
145 | 145 | def __str__(self): |
146 | 146 | """Give a friendly representation of the object""" |
147 | | return """%s( %s.%s )"""%( |
| 147 | return """%s( %s.%s )""" % ( |
148 | 148 | self.__class__.__name__, |
149 | 149 | self.selfName, |
150 | 150 | self.funcName, |
… |
… |
class BoundMethodWeakref(object):
|
155 | 155 | def __hash__(self): |
156 | 156 | return hash(self.key) |
157 | 157 | |
158 | | def __bool__( self ): |
| 158 | def __bool__(self): |
159 | 159 | """Whether we are still a valid reference""" |
160 | 160 | return self() is not None |
161 | 161 | |
162 | | def __nonzero__(self): # Python 2 compatibility |
| 162 | def __nonzero__(self): # Python 2 compatibility |
163 | 163 | return type(self).__bool__(self) |
164 | 164 | |
165 | 165 | def __eq__(self, other): |