FQN Decorators
Installation
At the command line::
$ pip install fqn-decorators
Usage
import fqn_decorators.decorators
Introduction
By extending the Decorator class you can create simple decorators.
Implement the Decorator.before and/or Decorator.after methods to perform actions before or after execution of the decorated item.
The Decorator.before method can access the arguments of the decorated item by changing the Decorator.args and Decorator.kwargs attributes.
The Decorator.after method can access or change the result using the Decorator.result attribute.
The Decorator.exception method can be used for do something with an Exception that has been raised.
In all three methods the Decorator.fqn and Decorator.func attributes are available.
Simple decorator
Create a simple decorator:
import fqn_decorators
import time
class time_it(fqn_decorators.Decorator):
def before(self):
self.start = time.time()
def after(self):
duration = time.time() - self.start
print("{0} took {1} seconds".format(self.fqn, duration))
@time_it
def my_function():
time.sleep(1)
my_function()
# __main__.my_function took 1.00293397903 seconds
Decorator with arguments
It is also very easy to create a decorator with arguments. It is not possible to create decorators with non-keyworded arguments.
Example:
import fqn_decorators
import time
class threshold(fqn_decorators.Decorator):
def before(self):
self.start = time.time()
def after(self):
duration = time.time() - self.start
treshold = self.params.get('threshold')
if threshold and duration > threshold:
raise Exception('Execution took longer than the threshold')
@threshold(threshold=2)
def my_function():
time.sleep(3)
my_function()
# Exception: Execution took longer than the threshold
Async Decorator
There's also support for decorating coroutines (or any awaitable), for Python >=3.5 only.
The implementation is the same as with the sync version, just inherit from
fqn_decorators.asynchronous.AsyncDecorator instead.
Example:
import asyncio
import time
from fqn_decorators.asynchronous import AsyncDecorator
class time_it_async(AsyncDecorator):
def before(self):
self.start = time.time()
def after(self):
duration = time.time() - self.start
print("{0} took {1} seconds".format(self.fqn, duration))
@time_it_async
async def coro():
await asyncio.sleep(1)
asyncio.run(coro())
# __main__.coro took 1.001493215560913 seconds
Metadata
Release files for fqn-decorators 2.0.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| fqn-decorators-2.0.2.tar.gz | 11.1 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| fqn_decorators-2.0.2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 16.8 kB
Release files / fqn-decorators-2.0.2.tar.gz
| Download URL | fqn-decorators-2.0.2.tar.gz |
|---|---|
| Size | 11.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
7b884c5903f25f6eab99f64e02ab2eb2505d36d7d7d40d85192c6ad429012713
|
|
BLAKE2b-256 checksum How to use checksums |
16f837f234e782fd2c235ac58e47c48e6d9ffb63b9955939c72785785358e838
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.0 CPython/3.9.12
|
Release files / fqn_decorators-2.0.2-py3-none-any.whl
| Download URL | fqn_decorators-2.0.2-py3-none-any.whl |
|---|---|
| Size | 5.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
2b70a41803857f7163419f392e088f09d5169f4a9932b91f79016e603ff1cb0f
|
|
BLAKE2b-256 checksum How to use checksums |
b89896b54527490da0f3287c8daed185ff673b4d2357e8fcd9172e26d3cbee13
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.0 CPython/3.9.12
|
Security for fqn-decorators 2.0.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.