Python package facilitating duck typing through attribute traverse utilities
Project description
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.
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
File details
Details for the file duckt-0.1.1.tar.gz
.
File metadata
- Download URL: duckt-0.1.1.tar.gz
- Upload date:
- Size: 3.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.0.9 CPython/3.8.2 Linux/5.4.0-37-generic
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 120c6f7ec69732793d89feef8cc9d84a730c023aebce7625b8f92f679120861e |
|
MD5 | 90923e24ae039ca2263f671c232f796c |
|
BLAKE2b-256 | 2c153f855819b70521917cd11b341719b2349658b703f904dd31feda0f9d521b |
File details
Details for the file duckt-0.1.1-py3-none-any.whl
.
File metadata
- Download URL: duckt-0.1.1-py3-none-any.whl
- Upload date:
- Size: 3.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.0.9 CPython/3.8.2 Linux/5.4.0-37-generic
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 00d28506f937608aa30e8ec5344e1c5d448e19af0b4a26c24bfb0b3634c094b9 |
|
MD5 | b0620c6627aaffbb9842076aa4c7d08a |
|
BLAKE2b-256 | 0a0792dfe674dabc0e9d3ab8259c11cf2fb3f7215fea9a11abf489f0dea32a0f |