Skip to main content

Extension methods for Python.

Project description

Extensified

Extension methods for Python.

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

Installation

pip install extensified==0.1.0

Usage

from dataclasses import dataclass
from datetime import datetime
from typing import Any

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).
  • @classmethod, 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.1.0.tar.gz (3.4 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.1.0-py3-none-any.whl (3.9 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for extensified-0.1.0.tar.gz
Algorithm Hash digest
SHA256 6f37551f440dd3ebf5923306767b67af1015be79d709ecf1f40949c401f36360
MD5 22018dcd2f7248a7b37cbf4189f94460
BLAKE2b-256 94fcdbf72ed54c9fd824cfe940203163c8813a6e1b944e1a6abd47f85165bd47

See more details on using hashes here.

File details

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

File metadata

  • Download URL: extensified-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 3.9 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.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0e0d7d157fc86a81f0f4f4adb8b01efe4ca38c262e2d31d424fa6fb8cd1c1f57
MD5 c3c886bd3a64f708ebbc66ddfc80e98a
BLAKE2b-256 e19ac21be7c1ca38f6fda283b1f79f1f481a177b1a67329360e6b7580f8923dc

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