PyZF
PyZF is Zeff Muks's enhancements for working with Python.
pip install pyzf
Interfaces
Those coming from other languages like Go or TypeScript will be familiar with the concept of interfaces. It enables you to define a contract without worrying about concrete classes. While Python has Abstract Base Classes, there are some limitations:
- You cannot define optional methods in ABCs
- It's not easy to compose multiple ABCs
- Hard to ensure a class implements an ABC
PyZF provides a robust and flexible way to define and implement interfaces in Python. It offers features like interface declarations, default methods, optional methods, and interface composition.
Features
PyZF Interfaces provides a powerful way to define contracts between different parts of your code, improving maintainability and reducing errors.
1. Interface Definition
Define interfaces using the Interface class:
from pyzf.interfaces import Interface, method, default_method, optional_method
class Jedi(Interface):
@method
def speak(self) -> str:
pass
@method
def force_power(self) -> int:
pass
@default_method
def default_greet(self) -> str:
return f"May the Force be with you. I am {self.speak()}"
2. Method Types
In Go, all methods are required but in pyzf, you can define also define optional methods.
@method: Regular methods that must be implemented@default_method: Methods with default implementations@optional_method: Methods that can be optionally implemented
3. Interface Composition
Combine multiple interfaces to create more complex ones:
class Sith(Interface):
@method
def force_lightning(self) -> None:
pass
@optional_method
def optional_force_choke(self) -> None:
pass
class ForceUser(Jedi, Sith):
pass
4. Implementation and Validation
Use the @implements decorator to ensure a class correctly implements an interface:
@implements(ForceUser)
class DarthVader:
def speak(self) -> str:
return "I am Darth Vader, Dark Lord of the Sith"
def force_power(self) -> int:
return 950
def force_lightning(self) -> None:
print("⚡️ Force Lightning!")
In the above example:
- We do not need to implement the
default_greetmethod, as it will be inherited. - We do not need to implement the
optional_force_chokemethod, as it is optional.
If you try to instantiate a class that does not implement an interface, you will get an InterfaceError.
from pyzf.interfaces import InterfaceError
@implements(ForceUser)
class DarthVader:
pass
vader = DarthVader() # InterfaceError
In Go, this is equivalent an interface assertion:
var _ ForceUser = (*DarthVader)(nil) # Compiler error if DarthVader does not implement ForceUser
In TypeScript, this is equivalent to an interface assertion:
const vader = new DarthVader(); // Compiler error if DarthVader does not implement ForceUser
5. Usage
Use interfaces for type hinting and polymorphism:
def use_the_force(obj: ForceUser):
print(f"Type of obj: {type(obj)}")
print(obj.default_greet())
vader = DarthVader()
use_the_force(vader)
In Go, this is equivalent to:
type ForceUser interface {
// ...
}
func use_the_force(obj ForceUser) { // ForceUser is an interface type
// ...
}
License
Metadata
Release files for pyzf 1.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| pyzf-1.1.0.tar.gz | 5.0 kB | Without provenance | Details for pyzf-1.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| pyzf-1.1.0-py3-none-any.whl | Python 3 | none | any | Details for pyzf-1.1.0-py3-none-any.whl |
Total release size: 10.2 kB
Release files / pyzf-1.1.0.tar.gz
| Download URL | pyzf-1.1.0.tar.gz |
|---|---|
| Size | 5.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
9bf260ed962de0fb46518e966338c6ee7e2b3cc2f21005c9f63af5c3d2a41f42
|
|
BLAKE2b-256 checksum How to use checksums |
ca9dfbcde83497bfb87526f18edb0dffdca95018cf6935b0c5e324880bf42638
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.11.11
|
Release files / pyzf-1.1.0-py3-none-any.whl
| Download URL | pyzf-1.1.0-py3-none-any.whl |
|---|---|
| Size | 5.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
9e0f9da9508023e1e46e042850f8a828918f6534bc4bc4340b612f9f804c8061
|
|
BLAKE2b-256 checksum How to use checksums |
688556a3db7271b8baec22c0e0a5b0221787e89af8e43848d053c115b93ba2e8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.11.11
|
Security for pyzf 1.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.