diff options
author | Michele Simionato <michele.simionato@gmail.com> | 2018-07-26 06:23:04 +0200 |
---|---|---|
committer | Michele Simionato <michele.simionato@gmail.com> | 2018-07-26 06:23:13 +0200 |
commit | fd411dd6def7cc66be45930a24c3a57d50fc57f5 (patch) | |
tree | c11bf1b71e3736352727684902c343df2dd43010 | |
parent | bbac0b4d046604ae5d17ed1b05bd0b01367d3be5 (diff) | |
download | python-decorator-git-fd411dd6def7cc66be45930a24c3a57d50fc57f5.tar.gz |
Talk about the decorator module
-rw-r--r-- | PITCHME.md | 127 |
1 files changed, 127 insertions, 0 deletions
diff --git a/PITCHME.md b/PITCHME.md new file mode 100644 index 0000000..4163fc4 --- /dev/null +++ b/PITCHME.md @@ -0,0 +1,127 @@ +# The decorator module is alive and kicking + +--- + +Have you ever heard of the decorator module? + ++++ + +Have you ever heard of IPython? + ++++ + +Every time to fire an IPython instance you are importing +the decorator module :-) + ++++ + +Several frameworks and tools depend on it, so it is one of the most +downloaded packages on PyPI + ++++ + +It is joy to maintain + +- next to zero bugs +- few questions +- I usually implement something new only when there is new Python release + ++++ + +For instance + +- Python 3.4 generic functions => decorator 4.0 +- Python 3.5/3.6 async/await => decorator 4.1 +- Python 3.7 dataclasses => decorator 4.2 + +--- + +```python +from functools import wraps + +def trace(f): + @wraps(f) + def wrapper(*args, **kwds): + print('Calling', f.__name__) + return f(*args, **kwds) + return wrapper + +@trace +def add(x, y): + """Sum two numbers""" + return x + y +``` +--- + +```python +>>> inspect.getfullargspec(add) +FullArgSpec(args=[], varargs='args', varkw='kwds', defaults=None, kwonlyargs=[], kwonlydefaults=None, annotations={}) + +>>> add.__code__.co_varnames +('args', 'kwds') +``` +--- + +decorator 0.1, May 2005: *this is a hack, surely they will fix the signature +in the next release of Python* + +... + ++++ + +# or maybe no + +--- + +```python +from decorator import decorator + +@decorator +def trace(f, *args, **kwds): + print('Calling', f.__name__) + return f(*args, **kwds) + +@trace +def add(a, b): + return a + b + +>>> add.__code__.co_varnames +('a', 'b') +``` + +--- + +```python +@decorator +async def trace(coro, *args, **kwargs): + print('Calling %s', coro.__name__) + await coro(*args, **kwargs) + +@trace +async def make_task(n): + for i in range(n): + await asyncio.sleep(1) + +``` +--- + +```python +import warnings +from decorator import decorator + +@decorator +def deprecated(func, message='', *args, **kw): + msg = '%s.%s has been deprecated. %s' % ( + func.__module__, func.__name__, message) + if not hasattr(func, 'called'): + warnings.warn(msg, DeprecationWarning, stacklevel=2) + func.called = 0 + func.called += 1 + return func(*args, **kw) + +@deprecated('Use new_function instead') +def old_function(): + 'Do something' +``` +--- +http://decorator.readthedocs.io/en/latest/ |