Introduction
Helps document and centralizing settings in a python project/library.
Facilitates looking up BaseSettings from retrievers, such as an environmental variable retriever.
Converts and standardizes any retrieved values to the type-hint on the setting attribute (such as bool, int, datetime, etc).
Interface to provide own custom retrievers, to grab settings/configuration from wherever you want.
Retrievers can be stacked, so multiple ones can be consulted when retrieving a setting.
See xsettings docs.
Documentation
📄 Detailed Documentation | 🐍 PyPi
Install
# via pip
pip install xsettings
# via poetry
poetry add xsettings
Quick Start
from xsettings import EnvVarSettings, SettingsField
from xsettings.errors import SettingsValueError
from typing import Optional
import dataclasses
import os
# Used to showcase looking up env-vars automatically:
os.environ['app_version'] = '1.2.3'
# Used to showcase complex setting types:
@dataclasses.dataclass
class DBConfig:
@classmethod
def from_dict(cls, values: dict):
return DBConfig(**values)
user: str
host: str
password: str
# Some defined settings:
class MySettings(EnvVarSettings):
app_env: str = 'dev'
app_version: str
api_endpoint_url: str
some_number: int
# For Full Customization, allocate SettingsField,
# In this case an alternate setting lookup-name
# if you want the attribute name to differ from lookup name:
token: Optional[str] = SettingsField(name='API_TOKEN')
# Or if you wanted a custom-converter for a more complex obj:
db_config: DBConfig = SettingsField(
converter=DBConfig.from_dict
)
# BaseSettings subclasses are singleton-like dependencies that are
# also injectables and lazily-created on first-use.
# YOu can use a special `BaseSettings.grab()` class-method to
# get the current settings object.
#
# So you can grab the current MySettings object lazily via
# its `grab` class method:
MySettings.grab().some_number = 3
assert MySettings.grab().some_number == 3
# You can also use a proxy-object, it will lookup and use
# the current settings object each time its used:
my_settings = MySettings.proxy()
# Here I showcase setting a dict here and using the converter
# I defined on the SettingsField to convert it for me:
my_settings.db_config = {
'user': 'my-user',
'password': 'my-password',
'host': 'my-host'
}
expected = DBConfig(
user='my-user',
password='my-password',
host='my-host'
)
# The dict gets converted automatically to the DBConfig obj:
assert MySettings.grab().db_config == expected
# If you set a setting with the same/exact type as
# it's type-hint, then it won't call the converter:
my_settings.db_config = expected
# It's the same exact object-instance still (ie: not changed/converted):
assert my_settings.db_config is expected
# Will use the default value of `dev` (default value on class)
# since it was not set to anything else and there is no env-var for it:
assert my_settings.app_env == 'dev'
# EnvVarSettings (superclass) is configured to use the EnvVar retriever,
# and so it will find this in the environmental vars since it was not
# explicitly set to anything on settings object:
assert my_settings.app_version == '1.2.3'
# Any BaseSettings subclass can use dependency-injection:
assert my_settings.token is None
with MySettings(token='my-token'):
assert my_settings.token == 'my-token'
# Parent is still consulted for any settings unset on child but set on parent:
assert my_settings.db_config == expected
# Can set settings like you expect,
# this will go into the child created in above `with` statement:
my_settings.app_env = 'prod'
assert my_settings.app_env == 'prod'
# After `with` child is not the current settings object anymore,
# reverts back to what it was before:
assert my_settings.token is None
try:
# If a setting is undefined and required (ie: not-optional),
# and it was not set to anything nor is there a default or an env-var for it;
# BaseSettings will raise an exception when getting it:
print(my_settings.api_endpoint_url)
except SettingsValueError as e:
assert True
else:
assert False
try:
# `SettingsValueError` inherits from both AttributeError and ValueError,
# as the error could be due to either aspect; so you can also do an except
# for either standard error:
print(my_settings.api_endpoint_url)
except ValueError as e:
assert True
else:
assert False
Licensing
This library is licensed under the MIT-0 License. See the LICENSE file.
Metadata
Release files for xsettings 1.6.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| xsettings-1.6.1.tar.gz | 19.1 kB | Without provenance | Details for xsettings-1.6.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| xsettings-1.6.1-py3-none-any.whl | Python 3 | none | any | Details for xsettings-1.6.1-py3-none-any.whl |
Total release size: 41.2 kB
Release files / xsettings-1.6.1.tar.gz
| Download URL | xsettings-1.6.1.tar.gz |
|---|---|
| Size | 19.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
ad92ce27805531de3b3af6aac071e8ebbf20b145610661274a0e3bcf57d4cc8f
|
|
BLAKE2b-256 checksum How to use checksums |
53cea9c1bdae430cccf579db54d1727b3877c58620b7d7ef79cb45719a627d19
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.12.9 {"installer":{"name":"uv","version":"0.12.9","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Release files / xsettings-1.6.1-py3-none-any.whl
| Download URL | xsettings-1.6.1-py3-none-any.whl |
|---|---|
| Size | 22.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
a1a6e2afd3863617a2678b75e997a9f102006b75a66c2c847882b3261b7ed67b
|
|
BLAKE2b-256 checksum How to use checksums |
9af5a0745bcb6dc7044506f1cf243f361a57994c7abcd3a47122572b9e01fc40
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.12.9 {"installer":{"name":"uv","version":"0.12.9","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Security for xsettings 1.6.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.