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 into class variables.

How to install

To install this library just enter:

pip install py-app-properties

How To Use

The main purpose of this library is to simplify work with an application config. Using decorator @properties with all required params and type hinting you can inject required settings from *.yml or *.json formats.

Example:

# my_app_properties.yml
my_class:
  int_var: 10
  list_int_var:
    - 12
    - 14
  dict_with_list_var:
    key1:
      - val1
      - val2
    key2:
      - val3
      - val4
  bool_var: true
from app_properties import properties

@properties(filename="my_app_properties.yml", root="my_class")
class MyClass:
    int_var: int
    list_int_var: list[int]
    dict_with_list_var: dict[str, list[str]]
    bool_var: bool
    
    def __init__(self, param1: int, param2: str) -> None:
        self._param1 = param1
        self._param2 = param2
    
    def some_method(self) -> None:
        ...  # using class variables and instance variables here

About params:

  • filename - the name of a file with config. By default, it is application.yml which is located in the same directory as a file with a used decorator. Use a relative path with ../ to read the file from a parent directory.
  • type_cast - used to know whether you want to cast config values to field type. By default, it's True, which means values in a config file will be cast according to the type hints. If False, type hinting is ignored, and available types are limited by a file format - only list, dict, int, str, float, bool, and none are available. If True - set, frozenset, tuple, NamedTuple, TypedDict, and dataclass will be available additionally to types above. Also nested types will be recursively cast.
  • override_default - used to know whether you want to override default values of class variables. By default, it is False.
  • root - root key in the config. Could be a nested value with separation by dots, for example:
# example.yml
some:
  nested:
    config:
      key: value
@properties(filename="example.yml", root="some.nested.config")
class MyClass:
    key: str

About contributing

You will make this library better if you open issues or create pull requests with improvements.

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

Uploaded Source

File details

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

File metadata

  • Download URL: py-app-properties-1.2.0.tar.gz
  • Upload date:
  • Size: 7.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.8.13

File hashes

Hashes for py-app-properties-1.2.0.tar.gz
Algorithm Hash digest
SHA256 a74827ab84d6fa608f7406acb607280fd25b0f9f6ec8661505e2b7f4817c9ef3
MD5 07b300e7895576978797928c50e97485
BLAKE2b-256 5c170151735e2d62c5ab7910e38cf2be0aff45a33d710d7930f053213bef32ee

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