duckt
A small Python package facilitating duck typing through attribute traverse utilities
This replaces try/except chains when trying to call different methods:
from duckt import Duck, DuckCall
class Lion:
def roar(self):
return "roar!"
creature = Lion()
assert Duck(creature).attr_call(
DuckCall("make_sound", ["woof"]), # tries to call creature.make_sound("woof")
DuckCall("make_sound", ["buzz", 100]), # tries to call creature.buzz("buzz", 100)
DuckCall("bark"), # creature.bark()
DuckCall("roar"), # creature.roar()
DuckCall("speak", ["Hello "], {"times": 3}) # creature.speak("Hello ", times=3)
) == "roar!"
# returns the output of the first successfull call
# if none of the call attempts was successful then the last raised error will be thrown
DuckCall class is fully replacable here with any custom callable accepting single argument. This argument is the Duck-wrapped object, so you can implement custom attribute extracting and/or calling behavior here. AttributeErrors and TypeErrors thrown from this callable is handled by the Duck instance.
Simplified interface for property extraction:
from duckt import Duck
class Person:
full_name = "John Doe"
some_person = Person()
name = Duck(some_person).attr('first_name', 'name', 'full_name')
# name now is equal to the first present attribute
# otherwise AttributeError is thrown
You may also use Duck as wrapper to a callable:
from duckt import Duck
def foo(some_string):
print(some_string)
duck = Duck(foo)
duck.call(
["hello", "world"], # foo("hello", "world")
["hello world"], # foo("hello world")
[[], {"hello": "world"}] # foo(hello="world")
)
Installation
pip install duckt
That's it.
Metadata
Release files for duckt 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 |
|---|---|---|---|---|
| duckt-0.1.1.tar.gz | 3.6 kB | Without provenance | Details for duckt-0.1.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| duckt-0.1.1-py3-none-any.whl | Python 3 | none | any | Details for duckt-0.1.1-py3-none-any.whl |
Total release size: 7.2 kB
Release files / duckt-0.1.1.tar.gz
| Download URL | duckt-0.1.1.tar.gz |
|---|---|
| Size | 3.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
120c6f7ec69732793d89feef8cc9d84a730c023aebce7625b8f92f679120861e
|
|
BLAKE2b-256 checksum How to use checksums |
2c153f855819b70521917cd11b341719b2349658b703f904dd31feda0f9d521b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.0.9 CPython/3.8.2 Linux/5.4.0-37-generic
|
Release files / duckt-0.1.1-py3-none-any.whl
| Download URL | duckt-0.1.1-py3-none-any.whl |
|---|---|
| Size | 3.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
00d28506f937608aa30e8ec5344e1c5d448e19af0b4a26c24bfb0b3634c094b9
|
|
BLAKE2b-256 checksum How to use checksums |
0a0792dfe674dabc0e9d3ab8259c11cf2fb3f7215fea9a11abf489f0dea32a0f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.0.9 CPython/3.8.2 Linux/5.4.0-37-generic
|
Security for duckt 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.