| Conditions | 10 |
| Total Lines | 59 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like deprecate() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | import warnings |
||
| 162 | def deprecate(obj, message=message, name=name, alternative=alternative, |
||
| 163 | pending=pending, addendum=addendum): |
||
| 164 | import textwrap |
||
| 165 | |||
| 166 | if not name: |
||
| 167 | name = obj.__name__ |
||
| 168 | |||
| 169 | if isinstance(obj, type): |
||
| 170 | obj_type = "class" |
||
| 171 | old_doc = obj.__doc__ |
||
| 172 | func = obj.__init__ |
||
| 173 | |||
| 174 | def finalize(wrapper, new_doc): |
||
| 175 | try: |
||
| 176 | obj.__doc__ = new_doc |
||
| 177 | except (AttributeError, TypeError): |
||
| 178 | # cls.__doc__ is not writeable on Py2. |
||
| 179 | # TypeError occurs on PyPy |
||
| 180 | pass |
||
| 181 | obj.__init__ = wrapper |
||
| 182 | return obj |
||
| 183 | else: |
||
| 184 | obj_type = "function" |
||
| 185 | if isinstance(obj, classmethod): |
||
| 186 | func = obj.__func__ |
||
| 187 | old_doc = func.__doc__ |
||
| 188 | |||
| 189 | def finalize(wrapper, new_doc): |
||
| 190 | wrapper = functools.wraps(func)(wrapper) |
||
| 191 | wrapper.__doc__ = new_doc |
||
| 192 | return classmethod(wrapper) |
||
| 193 | else: |
||
| 194 | func = obj |
||
| 195 | old_doc = func.__doc__ |
||
| 196 | |||
| 197 | def finalize(wrapper, new_doc): |
||
| 198 | wrapper = functools.wraps(func)(wrapper) |
||
| 199 | wrapper.__doc__ = new_doc |
||
| 200 | return wrapper |
||
| 201 | |||
| 202 | message = _generate_deprecation_message( |
||
| 203 | since, message, name, alternative, pending, |
||
| 204 | obj_type, addendum) |
||
| 205 | |||
| 206 | def wrapper(*args, **kwargs): |
||
| 207 | warnings.warn(message, metpyDeprecation, stacklevel=2) |
||
| 208 | return func(*args, **kwargs) |
||
| 209 | |||
| 210 | old_doc = textwrap.dedent(old_doc or '').strip('\n') |
||
| 211 | message = message.strip() |
||
| 212 | new_doc = (('\n.. deprecated:: %(since)s' |
||
| 213 | '\n %(message)s\n\n' % |
||
| 214 | {'since': since, 'message': message}) + old_doc) |
||
| 215 | if not old_doc: |
||
| 216 | # This is to prevent a spurious 'unexected unindent' warning from |
||
| 217 | # docutils when the original docstring was blank. |
||
| 218 | new_doc += r'\ ' |
||
| 219 | |||
| 220 | return finalize(wrapper, new_doc) |
||
| 221 | |||
| 223 |