A very simple Python module to declare protocols for checking if objects provide the desired interface.
Project description
fast-protocol
A very simple Python module to declare protocols for checking if objects provide the desired interface.
Installation
pip install fast-protocol
Usage
To create a Fast Protocol just call the fast_protocol.protocol
function passing it the names of the methods/attributes that the protocol should support.
from fast_protocol import protocol
def example():
...
Callable = protocol("__call__") # Create a protocol that matches objects with a dunder call method
match example:
case Callable():
print("example is callable")
This can be used outside a match
statement using isinstance
.
if isinstance(example, Callable):
print("example is callable")
Protocols are generated with the name "FastProtocol"
. This name can be changed by creating a new instance of
FastProtocolBuilder
. The name can be set traditionally by passing the name of the protocol to the FastProtocolBuilder
class. Alternatively you can pass the protocol name as a subscript to an existing FastProtocolBuilder
which will return a new instance that uses that name.
Traditional approach:
from fast_protocol import FastProtocolBuilder
Callable = FastProtocolBuilder("Callable")("__call__")
Alternative approach:
from fast_protocol import protocol
Callable = protocol["Callable"]("__call__")
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 fast-protocol-1.0.1.tar.gz
.
File metadata
- Download URL: fast-protocol-1.0.1.tar.gz
- Upload date:
- Size: 3.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.13 CPython/3.10.4 Linux/5.13.0-1025-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 16c03f78d3f0f04a76659fb65ce494d82ee509a535b961f0897cb62549541ef5 |
|
MD5 | 44c02dceb616d41cb0a6df083deda4ad |
|
BLAKE2b-256 | 36cc388f3be05a210fa5cef52276823e6c4e2a9b40c222869f3ad16ff1b562a9 |
File details
Details for the file fast_protocol-1.0.1-py3-none-any.whl
.
File metadata
- Download URL: fast_protocol-1.0.1-py3-none-any.whl
- Upload date:
- Size: 3.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.13 CPython/3.10.4 Linux/5.13.0-1025-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 27bb531e44f70060a1d925b3a7d18fff5fab63bf909fd316cb2fd8b97414bbd0 |
|
MD5 | 5ca969aa727ded22c5ccc6b1ef045c0e |
|
BLAKE2b-256 | fb64ea2863803751b6eff80da723ee6fbc1b623063498eddd98ac212c3f5a0fd |