A very simple Python model for declaring a protocol for checking if objects provide the desired functionality.
Project description
fast-protocol
A very simple Python model for declaring a protocol for checking if objects provide the desired functionality.
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
Hashes for fast_protocol-1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 375815a6a2767e8f5d3984e613abd7f2db19e45fa525250c55519e72c9da77a6 |
|
MD5 | 05df21e82222274d7f6228ead77df8b9 |
|
BLAKE2b-256 | e25a47e16ffaaf81d5fac7815b0770c6b80694458e8c218d1a85a6e08837492e |