6720
Comment: introduce a table to collect a few more samples (right now it's mostly links to stdlib manual)
|
← Revision 6 as of 2023-11-28 03:47:40 ⇥
9661
|
Deletions are marked like this. | Additions are marked like this. |
Line 1: | Line 1: |
A page for useful (or potentially abusive?) decorator ideas. | See also PythonDecoratorLibrary. And apart from decorators in the standard library, there are also lots of PyPI packages with convenient, interesting or novel use cases: |
Line 4: | Line 4: |
|| builtin || [[https://docs.python.org/3/library/functions.html?highlight=property#property|@property]] || Turn function into propery accessor, also: `@property.setter` || | || builtin || [[https://docs.python.org/3/library/functions.html?highlight=property#property|@property]] || Turn function into property accessor, also: `@property.setter` || |
Line 13: | Line 13: |
|| stdlib || [[https://python.readthedocs.io/en/stable/library/typing.html#typing.overload|@typing.overload]] || Singledispatch based on function parameter or return types || | |
Line 18: | Line 19: |
|| stdlib || [[https://docs.python.org/3/library/xmlrpc.server.html?highlight=decorator#xmlrpc.server.SimpleXMLRPCServer.register_function|@xmlrpc.….register_function]] || ... || | || stdlib || [[https://docs.python.org/3a/library/xmlrpc.server.html?highlight=decorator#xmlrpc.server.SimpleXMLRPCServer.register_function|@xmlrpc.register_function]] || ... || || stdlib || [[https://docs.python.org/3/library/sys.html#sys.settrace|@sys.settrace]] || Register function as debugger callback || |
Line 24: | Line 26: |
|| threading || [[https://gitlab.com/chadgh/ornamentation|@threaded, @debug, @skip_if_env]] || Convenient threading wrapper, and testing wrappers || || threading || [[https://pypi.org/project/lockorator/|@lock_or_wait]] || Aquire and release locks for functions || |
|
Line 26: | Line 30: |
|| debugging || [[https://pypi.org/project/handy-decorators/|@trycatch ]] || Capture exceptions to the log || || debugging || [[https://pythonhosted.org/log_calls/intro.html|@log_calls]] || Record invocations with arguments || |
|
Line 30: | Line 36: |
|| decorator || [[https://pypi.org/project/pydecor/|@log_call, @intercept, @instead, @before]] || Simplifies decorator construnction/interactions, parameter handling || || decorator || [[https://pypi.org/project/undecorated/|undecorated(fn)]] || reversible decoration || |
|
Line 33: | Line 41: |
|| collect || [[https://pypi.org/project/pyplugs/|@expose, @register]] || Plugin interface/registration || | |
Line 34: | Line 43: |
|| typing || [[https://pypi.org/project/pedantic/|@pedantic, @trace, @dirty, …]] || Encourages cleaner and better documented code || || typing || [[https://pypi.org/project/classy-decorators/|@Decorator]] || OO-style class/method parameters || || typing || [[https://pypi.org/project/decorateme/|@mutable, @final, @auto_obj]] || decorators for str/repr, equality, immutability, and more || || typing || [[https://pypi.org/project/paprika/|@to_string, @data, @repeat]] || Reduces some common boilerplate for methods || || typing || [[https://pypi.org/project/pyAttributes/|@DataAtrribute("desc")]] || .NET-like attributes || || parameter || [[https://pypi.org/project/simplecurry/|@curried]] || Convenient decorator-alternative to partial() || || parameter || [[https://www.attrs.org/en/stable/|@attrs]] || object declarations || || parameter || [[https://pypi.org/project/python-args/|@arg.validate]] || simplifies parameter handling/assertion || |
|
Line 36: | Line 53: |
|| flow handling || [[https://pypi.org/project/retryz/|@retry]] || Rerun function in case of exceptions || || flow handling || [[https://pypi.org/project/self/|@self]] || method chaining (fluent interfaces) || |
|
Line 39: | Line 58: |
(Note: Not sure this is going anywhere. Relisting builtins is somewhat redundant, but topical. The focus is novel/interesting decorators in the wild. The categorization probably won't hold up.) |
(Note: Not sure this is going anywhere. Relisting builtins is somewhat redundant, but on topic here. The focus is novel/interesting decorators in the wild. The categorization probably won't hold up; and probably going to split this up into sections.) |
Line 42: | Line 62: |
=== Other decorator links === * [[https://github.com/lord63/awesome-python-decorator|Awesome Python decorator collection (GitHub)]] * [[https://pypi.org/search/?q=decorator|PyPI decorator packages]] (gets interesting around page 10) |
See also PythonDecoratorLibrary. And apart from decorators in the standard library, there are also lots of PyPI packages with convenient, interesting or novel use cases:
Category |
Decorator |
Summary |
builtin |
Turn function into property accessor, also: @property.setter |
|
builtin |
Use cls instead of self |
|
builtin |
Defines methods without self handle |
|
stdlib |
Retain modicum of meta infos (__doc__) of wrapped function |
|
stdlib |
Allows for divergent implementations per @f.register() |
|
stdlib/backports |
Wrap computationally expensive functions |
|
stdlib |
Transform an old-style comparison function to a key function. |
|
stdlib |
Adds any of the missing lt,le,eq,ge,gt comparison dunder methods |
|
stdlib |
Prepopulate a couple of function arguments |
|
stdlib |
Singledispatch based on function parameter or return types |
|
stdlib |
Uses generator protocol yield to turn function into with-able context mananger |
|
stdlib |
Adds various “dunder” methods to the class to streamline property access |
|
stdlib |
Run as shutdown function |
|
stdlib |
Turn into abstract base class |
|
stdlib |
Guarantee unique members in enum class |
|
stdlib |
... |
|
stdlib |
Register function as debugger callback |
|
asyncio |
Outdated scheme for async def, deprecated in 3.8 |
|
asyncio |
Reduce invocations per timeframe |
|
asyncio |
Log exceptions more instantly |
|
asyncio |
Basic benchmarking |
|
async |
make func async and execute in other process |
|
threading |
Convenient threading wrapper, and testing wrappers |
|
threading |
Aquire and release locks for functions |
|
testing |
Override function w/ signature |
|
testing |
Override function w/ signature |
|
debugging |
Capture exceptions to the log |
|
debugging |
Record invocations with arguments |
|
decorator |
Assists declaring signature-preserving and single dispatch decorators |
|
decorator |
Ease parameterizing decorators themselvess |
|
decorator |
Retain callees' function name to simplify tracebacks |
|
decorator |
Craft trivial enter/exit decorator |
|
decorator |
Simplifies decorator construnction/interactions, parameter handling |
|
decorator |
reversible decoration |
|
collect |
Argparse alternative |
|
collect |
Declarative dict structure for application settings |
|
collect |
Register function callbacks for pattern matching |
|
collect |
Plugin interface/registration |
|
typing |
Exhaustive function parameter constraints |
|
typing |
Encourages cleaner and better documented code |
|
typing |
OO-style class/method parameters |
|
typing |
decorators for str/repr, equality, immutability, and more |
|
typing |
Reduces some common boilerplate for methods |
|
typing |
.NET-like attributes |
|
parameter |
Convenient decorator-alternative to partial() |
|
parameter |
object declarations |
|
parameter |
simplifies parameter handling/assertion |
|
flow handling |
Automatically run a function if invocation script |
|
flow handling |
Reinvokes function a few times when encountering (temporary) exceptions, alt: pypi:retry-decorator |
|
flow handling |
Rerun function in case of exceptions |
|
flow handling |
method chaining (fluent interfaces) |
|
monkeypatching |
Shorthand assignment to modules/objects |
|
monkeypatching |
Swap out function in object, retain reference to original |
(Note: Not sure this is going anywhere. Relisting builtins is somewhat redundant, but on topic here. The focus is novel/interesting decorators in the wild. The categorization probably won't hold up; and probably going to split this up into sections.)
Other decorator links
PyPI decorator packages (gets interesting around page 10)
__main__
This decorator does not alter a function, but causes it to be executed if __name__ == '__main__'. This provides an experimental cleaner syntax to the traditional way of bootstrapping a python script. This should not be used on class methods.
The function gets a copied list of sys.argv arguments.