Skip to main content

No project description provided

Project description

Implementation pattern*

/* inspired by Rust

Useful when it is needed to extend a class (usually 3d party) with some methods or interfaces

Install

pip install impler

or

poetry add impler

Usage

Methods implementation

Using implementation pattern you can extend any class (even 3rd party) with regular, class or static methods.

from impler import impl
from pydantic import BaseModel


@impl(BaseModel)
def fields_count(self: BaseModel):
    return len(self.__fields__)


class Point(BaseModel):
    x: int = 0
    y: int = 1


point = Point()
print(point.fields_count())

Class methods

@impl_classmethod(BaseModel)
def fields_count(cls):
    return len(cls.__fields__)


# or

@impl(BaseModel)
@classmethod
def fields_count(cls):
    return len(cls.__fields__)

Static methods

@impl_staticmethod(BaseModel)
def zero(cls):
    return 0


# or

@impl(BaseModel)
@staticmethod
def zero(cls):
    return 0

Async methods

@impl(BaseModel)
async def zero(cls):
    await asyncio.sleep(1)
    return 0

Interfaces implementation

The same way you can extend any class with the whole interface

Here is example of the base interface, which

from pathlib import Path


class BaseFileInterfase:
    def dump(self, path: Path):
        ...

    @classmethod
    def parse(cls, path: Path):
        ...

This is how you can implement this interface for Pydantic BaseModel class:

from impler import impl
from pydantic import BaseModel
from pathlib import Path


@impl(BaseModel, as_parent=True)
class ModelFileInterface(BaseFileInterface):
    def dump(self, path: Path):
        path.write_text(self.json())
        
    @classmethod
    def parse(cls, path: Path):
        return cls.parse_file(path)

If as_parent parameter is True the implementation will be injected to the list of the target class parents.

Then you can check if the class or object implements the interface:

print(issubclass(BaseModel, BaseFileInterfase))
# True

print(issubclass(Point, BaseFileInterfase))
# True

print(isinstance(point, BaseFileInterface))
# True

The whole api documentation could be found by the link

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

impler-0.1.0.tar.gz (8.2 kB view details)

Uploaded Source

Built Distribution

impler-0.1.0-py3-none-any.whl (12.2 kB view details)

Uploaded Python 3

File details

Details for the file impler-0.1.0.tar.gz.

File metadata

  • Download URL: impler-0.1.0.tar.gz
  • Upload date:
  • Size: 8.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.8 CPython/3.9.5 Linux/5.11.0-46-generic

File hashes

Hashes for impler-0.1.0.tar.gz
Algorithm Hash digest
SHA256 c08c082cd017ac8b30ec8b64257f20bc018c34e2c81fdc4fca199eb3924d91dc
MD5 31a64e65f2b6ba5d9bc7af577555af13
BLAKE2b-256 ff3b0e0df6c49d5c7e8cbefab1020f6d39b75588c19529e08514d072be4ccbb7

See more details on using hashes here.

File details

Details for the file impler-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: impler-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 12.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.8 CPython/3.9.5 Linux/5.11.0-46-generic

File hashes

Hashes for impler-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 eaac754a34787001f74aab5b53168f5cfe58ff62c56f117024e372650a1eb33b
MD5 cfc0a8404c5139f435758cb60627f616
BLAKE2b-256 224f5b0f45fd0c7251b39b8d5d9650f266529b076c1252a8032478dbcabd2aac

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