pyzf is Zeff Muks's enhancement for working with Python
Project description
PyZF
PyZF is Zeff Muks's enhancements for working with Python.
pip install PyZF
Interfaces
Those coming from other languages 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, they are not as flexible as interfaces.
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
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
@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!")
- We do not need to implement the
default_greet
method, as it will be inherited. - We do not need to implement the
optional_force_choke
method, as it is optional.
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)
PyZF Interfaces provides a powerful way to define contracts between different parts of your code, improving maintainability and reducing errors.
License
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 pyzf-1.0.1.tar.gz
.
File metadata
- Download URL: pyzf-1.0.1.tar.gz
- Upload date:
- Size: 5.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.9
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 23b0243f3283ebbb4dadbff25b54b8ac609e09d4eef3b8cc0da6c194e97d0288 |
|
MD5 | 5d18651c8212ff53b517a56f299140a2 |
|
BLAKE2b-256 | a761aefb464cac244072991f36b6e135fa8caa7c4fa4ea6bb02e2416b50bb634 |
File details
Details for the file pyzf-1.0.1-py3-none-any.whl
.
File metadata
- Download URL: pyzf-1.0.1-py3-none-any.whl
- Upload date:
- Size: 5.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.9
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a155456e7640998307c921250e87b429144c570427e165fae17f89d6b7829a37 |
|
MD5 | 55ef953331b34fa700c4fef05ab27853 |
|
BLAKE2b-256 | 059b33a0aa8caeb31ee0f02ec94dd01128f657aec32d1b619110f80cf63a1d6b |