Skip to main content

pyzf is Zeff Muks's enhancement for working with Python

Project description

PyZF

PyPI version

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:

  1. You cannot define optional methods in ABCs
  2. It's not easy to compose multiple ABCs
  3. 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_greet method, as it will be inherited.
  • We do not need to implement the optional_force_choke method, 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

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.2.tar.gz (5.7 kB view details)

Uploaded Source

Built Distribution

pyzf-1.0.2-py3-none-any.whl (6.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pyzf-1.0.2.tar.gz
  • Upload date:
  • Size: 5.7 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.2.tar.gz
Algorithm Hash digest
SHA256 8877a6d53c4bc83dd828fb9df5a03acd2db48f168e7ec62a647142fc89baf168
MD5 8ca3dffd792becaae70261c2b30c3488
BLAKE2b-256 2136e40960afa399356429cc640ebef901c3bf51b166a8d37fb1d45ddcf83105

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pyzf-1.0.2-py3-none-any.whl
  • Upload date:
  • Size: 6.0 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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 efa5775fbac71918b4407d3357b88e0b46487ca0d1e6975107d54169ca21c7c2
MD5 486c3cbbe37a4ca8b1dd9d13cc22b86f
BLAKE2b-256 9c9739862e80149f67a1ca6aa2d54544955612c8ff7262a33ccc9d839fabc8bb

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