Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/zechcodes/fast-protocol
A very simple Python module to declare protocols for checking if objects provide the desired interface.
https://github.com/zechcodes/fast-protocol
Last synced: 1 day ago
JSON representation
A very simple Python module to declare protocols for checking if objects provide the desired interface.
- Host: GitHub
- URL: https://github.com/zechcodes/fast-protocol
- Owner: ZechCodes
- License: mit
- Created: 2022-06-12T14:38:15.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2022-06-12T15:43:13.000Z (over 2 years ago)
- Last Synced: 2024-12-15T23:47:33.913Z (25 days ago)
- Language: Python
- Homepage:
- Size: 9.77 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# fast-protocol
A very simple Python module to declare protocols for checking if objects provide the desired interface.## Installation
```shell
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.
```python
from fast_protocol import protocoldef 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`.
```python
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:**
```python
from fast_protocol import FastProtocolBuilderCallable = FastProtocolBuilder("Callable")("__call__")
```
**Alternative approach:**
```python
from fast_protocol import protocolCallable = protocol["Callable"]("__call__")
```