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.2.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).
  • @staticmethod and 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.2.0.tar.gz (3.5 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.2.0-py3-none-any.whl (4.0 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for extensified-0.2.0.tar.gz
Algorithm Hash digest
SHA256 3c3260ea1222f97cf9ad3270a1ea7a8d570d08f33d82bb8152a90cfd50482f12
MD5 0c8d05c9117ea1b732653dd5057a2ed8
BLAKE2b-256 9a777c5ecad6eb33aaa06f78510cc07c8e77a9ecafe8ca62de9a8e84e04a3362

See more details on using hashes here.

File details

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

File metadata

  • Download URL: extensified-0.2.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.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2496a2dec694b3a59de21bce121528fd01ddf6be9aa5179d688b9b5322efdf7f
MD5 c6e51f5754c50a4b2d63c937eedfe496
BLAKE2b-256 9380d88d0e999b226c73c2ff095dc5a3dba2c602fd83dd7f4568695898a46e6f

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