Skip to main content

Extension methods for Python.

Project description

Miss extension methods from other languages? Python can have those too.

Installation

pip install extensified==0.3.0

Usage

from dataclasses import dataclass
from datetime import datetime

from extensified import extension_on


@dataclass
class Customer:

    name: str
    email: str
    created_at: datetime = datetime.now()


@extension_on(Customer)
class CustomerStringReprExtension:

    @property
    def rfc5322_email(self) -> str:
        return f'{self.name} <{self.email}>'

    @property
    def ldap_dn(self) -> str:
        email_domain = self.email.split('@')[1]
        return f'cn={self.name},ou=Users,dc={email_domain.replace(".", ",dc=")}'

    @property
    def vcard_name(self) -> str:
        surname = self.name.split()[-1]
        given_names = ' '.join(self.name.split()[:-1])
        return f'FN:{self.name}\nN:{surname};{given_names};;;'


customer = Customer("John Doe", "john@example.com")
print(customer.rfc5322_email)
print(customer.ldap_dn)
print(customer.vcard_name)

Limitations

  • Make sure that extension class is imported before its methods are used (i.e. import it in your init code).
  • super() can't be used in an extension class for now.
  • Type checkers won't understand what's going on and will complain about attributes missing.

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

extensified-0.3.0.tar.gz (3.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

extensified-0.3.0-py3-none-any.whl (4.0 kB view details)

Uploaded Python 3

File details

Details for the file extensified-0.3.0.tar.gz.

File metadata

  • Download URL: extensified-0.3.0.tar.gz
  • Upload date:
  • Size: 3.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.9.19

File hashes

Hashes for extensified-0.3.0.tar.gz
Algorithm Hash digest
SHA256 33fd43c01432fd6418f2b01f6d709a18fe4751bf2d3b4d5da3db3acdcb3547f9
MD5 ccf7cb73a01d4a78a26675a7a4cc847e
BLAKE2b-256 fad8d3b15ff0c89fa8b87edb1d3405cb201d676e7f35303ab66ea268d3df8f51

See more details on using hashes here.

File details

Details for the file extensified-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: extensified-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 4.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.9.19

File hashes

Hashes for extensified-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 97d2a695a783ef87e605d69d4ff3d48056c7bb8dcc9c5bf9f6ec999e20f287b6
MD5 9fc654809bcf8c1c0b9db866ed7a99d6
BLAKE2b-256 e17bdf244b079d10f94d8a39fc04f6bbfa1d67d52205c863eef606fc732ccc37

See more details on using hashes here.

Supported by

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