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.
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 | |
|---|---|---|---|
| duckt-0.1.1.tar.gz | 3.6 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| duckt-0.1.1-py3-none-any.whl | Python 3 | none | any | Details |
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
|