Application Properties
Application-properties is a Python library (current version 0.9.2) that provides a simple, unified manner of accessing program properties. It supports loading configuration from various formats including JSON, TOML, YAML, JSON5, and traditional .properties files. The library emphasizes ease of use, testability, extensibility, and hierarchical property access, releasing frequently with new features and fixes.
Warnings
- gotcha Starting from version 0.9.2, the library's loaders for JSON, YAML, and TOML gained enhanced support for handling `.` characters within keys. By default, `.` implies hierarchical structure. If you relied on `.` being treated as a literal character in keys for these formats in older versions, you might need to adjust your configuration or use the 'master switch' to control this behavior.
- gotcha Functionality for YAML, TOML, and JSON5 files requires installing optional dependencies. If these are not installed, attempts to load these file types will fail with import errors.
- breaking The `application-properties` library requires Python 3.10 or newer. Installing or running on older Python versions will result in environment or syntax errors.
Install
-
pip install application-properties -
pip install application-properties[yaml,toml,json5]
Imports
- ApplicationProperties
from application_properties import ApplicationProperties
Quickstart
import os
from application_properties import ApplicationProperties
# Create a dummy config file for demonstration
config_content = """
my.setting = some_value
another.setting = 123
"""
with open("config.properties", "w") as f:
f.write(config_content)
try:
# Load properties from a file named config.properties
app_props = ApplicationProperties("config.properties")
# Access a property
value = app_props.get_string_property("my.setting")
print(f"Value of 'my.setting': {value}")
# Access a property with a default value if not found
not_found_value = app_props.get_string_property("non.existent", "default_value")
print(f"Value of 'non.existent' (with default): {not_found_value}")
except Exception as e:
print(f"An error occurred: {e}")
finally:
# Clean up the dummy file
if os.path.exists("config.properties"):
os.remove("config.properties")