Skip to main content

Java application properties for Python

Project description

Python Application Properties

PyPI - Python Version PyPI - Package Version PyPI - License Build Status Coverage Status Code style: black Checked with mypy Imports: isort

What is this

This is a simple library to inject non-sensitive configurations like message templates, dictionaries, and lists with required settings into class variables.

How to install

To install this library just enter:

pip intall py-app-properties

How To Use

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

Let's assume that you have some email service that requires a couple of message templates (it could be also a dictionary with default HTTP headers and other kinds of mappings). It'll look like this:

class EmailService:
    CONFIRM_REGISTRATION_TEMPLATE = "Thanks for your registration!"
    RESET_PASSWORD_TEMPLATE = "To reset a password follow the link - {link}"
    # and other 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 other 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 decreases the 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 the 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 other 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 other methods...

And that's all. Just one decorator and a 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.

About contributing

You will make this library better if you open issues or create pull requests with improvements here. Also, you can write me directly in a private message if you have some questions or recommendations.

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.3.0.tar.gz (7.1 kB view details)

Uploaded Source

Built Distribution

py_app_properties-0.3.0-py3-none-any.whl (7.2 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for py-app-properties-0.3.0.tar.gz
Algorithm Hash digest
SHA256 dd7dd151ad8f94e291aaec7d410a3b16a1ea70a1cb1b348728e93aaaeb75e282
MD5 58592c7ac5a309aa6cf4c825fd339dee
BLAKE2b-256 13dd5af2de391650fcbf79be7c361df0f12abe8016f04b085db60ed54ae38d57

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for py_app_properties-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5badb260ab7397b0288fd283939c76440b415e7c1a96b3632801107ac97025e1
MD5 4eff178e8350ddc0ed81afb37085baa6
BLAKE2b-256 8d29300920d444d1a55c699c6ab175181880e05dc31b7d60facf083b644a7099

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