Skip to main content

A lib to define class extendable at runtime.

Project description

CI codecov

Extendable

About

Extendable is a module that aims to provide a way to define extensible python classes. This module was born out of the need to find a way to allow the definition of modules whose behaviour can be extended by other modules by extending the initial definition of classes at runtime.

Quick start

Let's define a first python class.

from extendable import ExtendableMeta

class Person(metaclass=ExtendableMeta):

    def __init__(self, name: str):
        self.name = name

    def __repr__(self) -> str:
        return self.name

Someone using the module where the class is defined would need to extend the person definition with a firstname field.

from extendable import ExtendableMeta

class PersonExt(Person, extends=Person):
    def __init__(self, name: str):
        super().__init__(name)
        self._firstname = None

    @property
    def firstname(self) -> str:
        return self._firstname

    @firstname.setter
    def firstname(self, value:str) -> None:
        self._firstname = value

    def __repr__(self) -> str:
        res = super().__repr__()
        return f"{res}, {self.firstname or ''}"

At this time we have defined that PersonExt extends the initial definition of Person. To finalyse the process, we need to instruct the runtime that all our class declarations are done by building the final class definitions and making it available into the current execution context.

from extendable import context, registry

_registry = registry.ExtendableClassesRegistry()
context.extendable_registry.set(_registry)
_registry.init_registry()

Once it's done the Person and PersonExt classes can be used interchangeably into your code since both represent the same class...

p = Person("Mignon")
p.firstname = "Laurent"
print (p)
#> Mignon, Laurent

:warning: This way of extending a predefined behaviour must be used carefully and in accordance with the Liskov substitution principle It doesn't replace others design patterns that can be used to make your code pluggable.

Development

To run tests, use tox. You will get a test coverage report in htmlcov/index.html. An easy way to install tox is pipx install tox.

This project uses pre-commit to enforce linting (among which black for code formating, isort for sorting imports, and mypy for type checking).

To make sure linters run locally on each of your commits, install pre-commit (pipx install pre-commit is recommended), and run pre-commit install in your local clone of the extendable repository.

To release:

  • run ``bumpversion patch|minor|major --list
  • Check the new_version value returned by the previous command
  • run towncrier build.
  • Inspect and commit the updated HISTORY.rst.
  • git tag {new_version} ; git push --tags.

Contributing

All kind of contributions are welcome.

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

extendable-0.0.5.tar.gz (9.9 kB view details)

Uploaded Source

Built Distribution

extendable-0.0.5-py3-none-any.whl (10.2 kB view details)

Uploaded Python 3

File details

Details for the file extendable-0.0.5.tar.gz.

File metadata

  • Download URL: extendable-0.0.5.tar.gz
  • Upload date:
  • Size: 9.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/4.0.2 CPython/3.11.4

File hashes

Hashes for extendable-0.0.5.tar.gz
Algorithm Hash digest
SHA256 a248fb959e9ab65583d66a806bb309a6046d816b86f9e1b16ad3d38bbcd8105c
MD5 abb11dfd11bb5eb4e14186794eee50e0
BLAKE2b-256 df5bcbdfd791f1ef1baeb4c1712607062f285cb1057a05b1e32a9ceb3c4e90e4

See more details on using hashes here.

File details

Details for the file extendable-0.0.5-py3-none-any.whl.

File metadata

  • Download URL: extendable-0.0.5-py3-none-any.whl
  • Upload date:
  • Size: 10.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/4.0.2 CPython/3.11.4

File hashes

Hashes for extendable-0.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 6712b805176f03eb68b2e2d814b461a8009d22268090465f1e5c4a31e93ec972
MD5 3292a8d09542aee8c83e8b63b17e037b
BLAKE2b-256 422f3271c238a9678e7cec34590834f18d6507d5b48e850dbc67774fed65ef30

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