Skip to main content

pyzf is Zeff Muks's enhancement for working with Python

Project description

PyZF Interfaces

PyZF Interfaces 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!")
  1. We do not need to implement the default_greet method, as it will be inherited.
  2. 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

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyzf-1.0.0.tar.gz (5.0 kB view details)

Uploaded Source

Built Distribution

pyzf-1.0.0-py3-none-any.whl (5.4 kB view details)

Uploaded Python 3

File details

Details for the file pyzf-1.0.0.tar.gz.

File metadata

  • Download URL: pyzf-1.0.0.tar.gz
  • Upload date:
  • Size: 5.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.9

File hashes

Hashes for pyzf-1.0.0.tar.gz
Algorithm Hash digest
SHA256 a8a51354efc5d2a813c9a2d7819e6fd526a18e54db685e401391b89f21f99c61
MD5 915d665ae12b8b75bf1f79018ff13fbf
BLAKE2b-256 6431020ef1fa6bdd4573ece5f52ea39aa8a5243b50c0fc9a5bf47aefdad26ad5

See more details on using hashes here.

File details

Details for the file pyzf-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: pyzf-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 5.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.9

File hashes

Hashes for pyzf-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 63906033567e6dcfcb83c22768545b78c0e03c11eeecdc222ef9ead2bde31419
MD5 4abc34ee150916c6ee6c8b580f06f39e
BLAKE2b-256 c722d0ade9af4e8b59c324c4942aedb21a6f64d2b1f7bec7a02206a8288126d9

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page