Skip to main content

Java application properties for Python

Project description

Python Application Properties

Code style: black Checked with mypy Imports: isort Pre-commit: enabled

How To Use

The main purpose of this library is to simplify work with an application configs.

Let's assume that you have some email service which requires a couple message templates. It'll look like:

class EmailService:
    CONFIRM_REGISTRATION_TEMPLATE = "Thanks for your registration!"
    RESET_PASSWORD_TEMPLATE = "To reset a password follow the link - {link}"
    # and another templates...
    
    def __init__(self, mail_sender: IMailSender) -> None:
        self._sender = mail_sender
    
    def send_confirm_registration_mail(self, username: str) -> None:
        self._sender.send_mail(username, self.CONFIRM_REGISTRATION_TEMPLATE)
    
    def send_reset_password_email(self, username: str, link: str) -> None:
        template = self.RESET_PASSWORD_TEMPLATE.format(link=link)
        self._sender.send_mail(username, template)
    # and another methods...

As you can see, there are some problems connected with storing message templates in class variables:

  • Templates could have a big size and that decrease readability of a whole class.
  • It's also hard to maintain different versions of templates.

But if you have config file my_config.yml with content below, you can "inject" variables to the EmailService class:

email_templates:
  confirm_registration_template: Thanks for your registration!
  reset_password_template: To reset a password follow the link - {link}
  # and so on...
@properties(filename="my_config.yml", root="email_templates")
class EmailService:
    CONFIRM_REGISTRATION_TEMPLATE: str
    RESET_PASSWORD_TEMPLATE: str
    # and another templates...
    
    def __init__(self, mail_sender: IMailSender) -> None:
        self._sender = mail_sender
    
    def send_confirm_registration_mail(self, username: str) -> None:
        self._sender.send_mail(username, self.CONFIRM_REGISTRATION_TEMPLATE)
    
    def send_reset_password_email(self, username: str, link: str) -> None:
        template = self.RESET_PASSWORD_TEMPLATE.format(link=link)
        self._sender.send_mail(username, template)
    # and another methods...

And that's all. Just one decorator and some file with application properties. You can even inject all necessary data into dataclass object:

@properties(filename="my_config.yml", root="email_templates")
@dataclass(init=False)
class EmailTemplates:
    confirm_registration_template: str
    reset_password_template: str

init=False is required if you don't want to pass and override params during instance initialization.

Available features

  • Supporting .yaml and .json extensions of configs.
  • Accessing nested configs using root variable:
# "config.yaml"
some:
  very:
    nested:
      variable:
        key: value
@propeties(filename="config.yaml", root="some.very.nested.variable")
class Config:
    key: str
  • Overriding default values with override_default=True
  • Type casting for primitive types. Nested types will be added soon.
  • Case ignoring by default is true. You can switch off this option.

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

py-app-properties-0.2.0.tar.gz (5.2 kB view details)

Uploaded Source

File details

Details for the file py-app-properties-0.2.0.tar.gz.

File metadata

  • Download URL: py-app-properties-0.2.0.tar.gz
  • Upload date:
  • Size: 5.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.8.2

File hashes

Hashes for py-app-properties-0.2.0.tar.gz
Algorithm Hash digest
SHA256 617b64a59595764b206a88e744cfd790c04befc7c563180a2597e23fd93eedf4
MD5 d679396da946c1b93a6fcbf1b5b11705
BLAKE2b-256 5b34e3c55b453c68536f8229619a1ff8e0e3f497c325c61c72661164079a8055

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