Note that there are some explanatory texts on larger screens.

plurals
  1. POGet specific info about calling code in Python decorators
    text
    copied!<p>I have a Python decorator that I'm using for marking functions/methods as deprecated. It works <em>sufficiently</em>, but I'd like it to work <em>better</em>. Specifically, what I want for it to do is to be able to tell the <strong>exact</strong> line number of the call <em>to</em> the deprecated function. That way one does not have to grep through source code looking for it; instead the warning will point them directly to it. (That's not to say that someone wouldn't grep through the code anyway to look for other places that use call the deprecated function, but they shouldn't have to do that in response to such a warning, IMHO).</p> <pre><code>class deprecated(object): '''This class implements the deprecated decorator. This decorator is used to mark a function or a class as deprecated. A message indicating why it is deprecated (and what to use instead) must be provided. Also, a version number indicating when the function or class was deprecated must _also_ be provided. Optionally, a third parameter may be passed that indicates when the deprecated functionality will actually be removed.''' def __init__(self, message, version, to_be_removed_in_version = None): self._message = message self._version = version self._removal_version = to_be_removed_in_version return def __call__(self, func): def wrapper(*args, **kwargs): import warnings import inspect frame = inspect.currentframe().f_back where = 'Called from {0} somewhere after line {1}.' where = where.format(frame.f_code.co_filename, frame.f_code.co_firstlineno) msg = 'Call to deprecated function {0} (since {1}{2}) {3}' if self._removal_version is not None: removal = ', scheduled for removal by version {0}' removal = removal.format(self._removal_version) msg = msg.format(func.__name__, self._version, removal, where) warnings.warn_explicit(msg, category = DeprecationWarning, filename = func.func_code.co_filename, lineno = func.func_code.co_firstlineno + 1) return func(*args, **kwargs) wrapper.__name__ = func.__name__ wrapper.__doc__ = func.__doc__ wrapper.__dict__.update(func.__dict__) return wrapper </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload