A modern library for creating decorators. Static-typed.
Project description
Smart Decorator
A static-typed library for creating decorators.
from smart_decorator import simple_decorator
_events = {}
@simple_decorator
def register(callback, *, event_type: str = 'normal'):
_events[event_type] = callback
return callback
@register # Parentheses optional
def on_normal():
print('an event happened!')
@register(event_type='error')
def on_error():
print('an error!')
# can be used as a normal function, too
def on_close():
print('closing!')
register(on_close, event_type='close')
Documentation
@simple_decorator
Turn a function into a smart decorator, as shown above. The decorator must accept a func, and some optional keyword arguments. Simple decorators must either return the func unchanged, or a wrapper with the same signature (see @decorator for signature morphing!).
@simple_decorator
def my_function(func, *, option1, option2 = ...):
def wrapper(*args, **kwargs):
... # do something
return func(*args, **kwargs)
return wrapper
@decorator
The same as @simple_decorator but the decorator can return anything, such as a function with a different signature. Decorators declared this way should ideally be static-typed.
@decorator
def inject_foo[**P, R](
func: Callable[Concatenate[str, P], R], # static-typing to inject a parameter
*,
what_to_inject: str = 'foo'
) -> Callable[P, R]:
def wrapper_func(*args: P.args, **kwargs: P.kwargs) -> R:
return func(what_to_inject, *args, **kwargs)
return wrapper_func
@inject_foo
def add_stuff(what: str, cool: str) -> str:
return what + cool
assert add_stuff('bar') == 'foobar'
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file smart_decorator-0.1.0.tar.gz.
File metadata
- Download URL: smart_decorator-0.1.0.tar.gz
- Upload date:
- Size: 4.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
697da5713803ff021f1e175c16149808bb00b5eddaee079b76b48c724fc5e60f
|
|
| MD5 |
88169615b61a9c314fdb3dea1b5062c5
|
|
| BLAKE2b-256 |
6d87569ab6133ee3c09f1e51f7233a381bd2cdd364b88c21b340a201ed273c5f
|
Provenance
The following attestation bundles were made for smart_decorator-0.1.0.tar.gz:
Publisher:
python-publish.yml on mkzeender/smart_decorator
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
smart_decorator-0.1.0.tar.gz -
Subject digest:
697da5713803ff021f1e175c16149808bb00b5eddaee079b76b48c724fc5e60f - Sigstore transparency entry: 167388717
- Sigstore integration time:
-
Permalink:
mkzeender/smart_decorator@04574267f38f0a865feb6e444f811c8ffc7322f0 -
Branch / Tag:
refs/tags/v0.1.1-a - Owner: https://github.com/mkzeender
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@04574267f38f0a865feb6e444f811c8ffc7322f0 -
Trigger Event:
release
-
Statement type:
File details
Details for the file smart_decorator-0.1.0-py3-none-any.whl.
File metadata
- Download URL: smart_decorator-0.1.0-py3-none-any.whl
- Upload date:
- Size: 7.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7ec28b08470a58ec390cb12f1ca6d537509b0217673555a3cd15f5a042d0b512
|
|
| MD5 |
cc1cfa0801fc952afa0a43607c3663fa
|
|
| BLAKE2b-256 |
a5c1014bab820c7bc623e24d5984bb349a30f3ace87c07b0fce38cf8086b0a06
|
Provenance
The following attestation bundles were made for smart_decorator-0.1.0-py3-none-any.whl:
Publisher:
python-publish.yml on mkzeender/smart_decorator
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
smart_decorator-0.1.0-py3-none-any.whl -
Subject digest:
7ec28b08470a58ec390cb12f1ca6d537509b0217673555a3cd15f5a042d0b512 - Sigstore transparency entry: 167388718
- Sigstore integration time:
-
Permalink:
mkzeender/smart_decorator@04574267f38f0a865feb6e444f811c8ffc7322f0 -
Branch / Tag:
refs/tags/v0.1.1-a - Owner: https://github.com/mkzeender
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@04574267f38f0a865feb6e444f811c8ffc7322f0 -
Trigger Event:
release
-
Statement type: