hxss.responsibility
Simple implementation of chain of responsibilities pattern.
result = chain(request) [ & DefaultLinkClass] [ | <callable|Link>] | chain
Installation
pip: hxss.responsibility
aur: python-hxss-responsibility
Usage
from hxss.responsibility import chain
def none(value):
pass
def increment(value):
return value + 1
def decrement(value):
return value - 1
request = 1
result = chain(request) | none | increment | decrement | chain
assert(result == 2)
result = chain(request) | none | decrement | increment | chain
assert(result == 0)
result = chain(request) | none | none | none | chain
assert(result == None)
Basics
Chain of responsibilities sequentially handles request by every link until response returned.
By default every not None value considered as valid response(some kind of Null coalescing operator):
handled = []
def wrap(func):
def wrapper(value):
handled.append(func.__name__)
return func(value)
return wrapper
result = chain(request) \
| wrap(none) \
| wrap(increment) \
| wrap(decrement) \
| chain
assert(result == 2)
assert(handled == ['none', 'increment'])
Lifecycle
chainfunction creates newRequest- every
callablepassed toRequestby|operator wraps intoLinkwhich handles the request - once
Linkreturns validResponseall nextcallableswill be ignored | chainoperator forces to return raw response value
Customization
- custom result validation:
from hxss.responsibility import Link
class FinalLink(Link):
def _validate_result(self, result): # see default Link implementation
return self._response(result)
result = chain(1) | FinalLink(none) | increment | decrement | chain
assert(result == None)
- custom independent link:
class CustomLink(Link):
def __init__(self):
pass
def handle(self, request):
result = calc_result(request)
return self._response(result)
result = chain(1) | increment | CustomLink() | decrement | chain
- custom default link:
class InversedLink(Link):
def _validate_result(self, result):
if (result is None):
return self._response(result)
result = chain(1) & InversedLink | increment | CustomLink() | decrement | chain
assert(result == None)
Metadata
Release files for hxss.responsibility 0.1.1
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 | Actions |
|---|---|---|---|---|
| hxss.responsibility-0.1.1.tar.gz | 4.6 kB | Without provenance | Details for hxss.responsibility-0.1.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| hxss.responsibility-0.1.1-py3-none-any.whl | Python 3 | none | any | Details for hxss.responsibility-0.1.1-py3-none-any.whl |
Total release size: 10.6 kB
Release files / hxss.responsibility-0.1.1.tar.gz
| Download URL | hxss.responsibility-0.1.1.tar.gz |
|---|---|
| Size | 4.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
b7c020e6031fa5373cec0c09ae7bf4ae07a2224b20e4312e00a0e282795a4e92
|
|
BLAKE2b-256 checksum How to use checksums |
9107d34b25944d1d655f55eaae2c56e708d1143690de8b61ac21f7cc58b4bb3a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.3.0 pkginfo/1.6.1 requests/2.25.1 setuptools/51.0.0 requests-toolbelt/0.9.1 tqdm/4.55.1 CPython/3.9.1
|
Release files / hxss.responsibility-0.1.1-py3-none-any.whl
| Download URL | hxss.responsibility-0.1.1-py3-none-any.whl |
|---|---|
| Size | 6.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
5d3e6d4dd63f3e40c60e147d1f42cd87ebac92fa104bf6a2ec3c0532c2c63b1d
|
|
BLAKE2b-256 checksum How to use checksums |
e842e0d8a0203f3c10d8c4cbcd39fc15b4fa593414d5e16d3ca1adeacc9d5cf7
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.3.0 pkginfo/1.6.1 requests/2.25.1 setuptools/51.0.0 requests-toolbelt/0.9.1 tqdm/4.55.1 CPython/3.9.1
|
Security for hxss.responsibility 0.1.1
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.