Skip to main content

Java application properties for Python

Project description

Python Application Properties

PyPI - Python Version PyPI 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.2.2.tar.gz (5.7 kB view details)

Uploaded Source

Built Distribution

py_app_properties-0.2.2-py3-none-any.whl (5.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: py-app-properties-0.2.2.tar.gz
  • Upload date:
  • Size: 5.7 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.2.tar.gz
Algorithm Hash digest
SHA256 b2f8247132b63f4f94c71c110b7cf131cfb9356c98a3e7f9a7cc65faa721ffc5
MD5 9deb3df7d19c55698d39d09da2726d4a
BLAKE2b-256 fb382d2310034724d392fb3dba6a6036877d03cc67019294aa0fd0c11455c930

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for py_app_properties-0.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 7462ddc13cefc9720eb40b1976c646d7d7d0bd9785fb5481e79593d0f2a72f66
MD5 d58a51f9129fe9be4b5a9e65753c3c6a
BLAKE2b-256 a625f16feb3e6f5eac9da050216dc1e49948edeb0d6fcf01f7ec4b5fe46f0ce5

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