Skip to main content

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

Metadata

Release files for impler 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for impler 0.1.0
File Size Uploaded
impler-0.1.0.tar.gz 8.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for impler 0.1.0
File Interpreter ABI Platform
impler-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 20.4 kB

Release files / impler-0.1.0.tar.gz

Download URL impler-0.1.0.tar.gz
Size 8.2 kB
Tags Source
SHA-256 checksum
How to use checksums
c08c082cd017ac8b30ec8b64257f20bc018c34e2c81fdc4fca199eb3924d91dc
BLAKE2b-256 checksum
How to use checksums
ff3b0e0df6c49d5c7e8cbefab1020f6d39b75588c19529e08514d072be4ccbb7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.8 CPython/3.9.5 Linux/5.11.0-46-generic

Release files / impler-0.1.0-py3-none-any.whl

Download URL impler-0.1.0-py3-none-any.whl
Size 12.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
eaac754a34787001f74aab5b53168f5cfe58ff62c56f117024e372650a1eb33b
BLAKE2b-256 checksum
How to use checksums
224f5b0f45fd0c7251b39b8d5d9650f266529b076c1252a8032478dbcabd2aac
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.8 CPython/3.9.5 Linux/5.11.0-46-generic

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page