Skip to main content

saconfig

The saconfig module provides a simple and efficient way to manage and access environment variables within Python applications. This module abstracts the complexity of environment variable handling by providing utility functions for retrieving and validating variables.

IMPORTANT: module read only environment variables, it doesnt read the .env file, for this you can use python-dotenv

usage

> presets

from saconfig import MysqlConfig

mysql = MysqlConfig() # it`s going to read MYSQL_HOST and etc.
mysql_test = MysqlConfig(prefix="TEST") # it`s going to read TEST_MYSQL_HOST and etc.

Take a look of preset configs in saconfig.configs, there is configs for redis, postgresql, mysql, rabbit, django, etc.

> build a config

from saconfig import BaseConfig, Field

class ApiConfig(BaseConfig):
    # name_of_variable: type = Field(settings...)
    host: str = Field(key="HOST")
    api_key: str = Field(key="API_KEY")

api = ApiConfig()
api_1 = ApiConfig(prefix="API1")
api_2 = ApiConfig(prefix="API2")

If you want to reuse the config, it can be inited with different prefixes

> default type parsers

config

from saconfig import BaseConfig, Field

class ExampleConfig(BaseConfig):
    var1: list = Field(parser_value_type=int)
    var2: dict = Field(parser_value_type=int)
    var3: int = Field()
    var4: tuple = Field()
    var5: set = Field()
    var6: bool = Field()

data = ExampleConfig()
print(data.var1)
print(data.var2)
print(data.var3)
print(data.var4)
print(data.var5)
print(data.var6)

env

var1=1,2,3,4,5
var2=a:1,b:2,c:3
var3=6
var4=test,231,run
var5=test,test,3,3,3,1,2,3
var6=f

output

[1, 2, 3, 4, 5]
{'a': 1, 'b': 2, 'c': 3}
6
('test', '231', 'run')
{'3', 'test', '1', '2'}
False

More about parsers you can see in saconfig.parses

> fields configurations

from saconfig import BaseConfig, Field

class ExampleConfig(BaseConfig):
    var1: list = Field(
        key="ANOTHER_KEY_NAME", # key to look for value in the env, by default variable name(key1)
        required=True, # if the field required or not
        default=[1,2,3], # default value of the field if its not in env
        parser=None, # if you dont like default parser(in this case of list) or you need to parse more specific structures
        parser_value_type=int, # type of value for parsing list, dict, set, tuple, etc.
        pc_transformer=None # pc transformer class, will be explained later
    )

> example of env file generating

config

from saconfig import BaseConfig, Field
from saconfig import generate_env_example

class ExampleConfig(BaseConfig):
    var1: list = Field(parser_value_type=int, required=False)
    var2: dict = Field(parser_value_type=int, required=False)
    var3: int = Field(required=False)
    var4: tuple = Field(required=False)
    var5: set = Field(required=False)
    var6: bool = Field(required=False)

data = ExampleConfig()
data_2 = ExampleConfig(prefix="DATA_2")
generate_env_example(data, data_2)

output

# ExampleConfig
var1=
var2=
var3=
var4=
var5=
var6=


# DATA_2 ExampleConfig
DATA_2_var1=
DATA_2_var2=
DATA_2_var3=
DATA_2_var4=
DATA_2_var5=
DATA_2_var6=

> PCTransformers - parse/compile transformer

The most interesting part of this party. They allow you to transform one field into another ones, and compile field from them. The best example for it saconfig.transformers.URLPCTransformer with usage of databases configs, it allows you to represent connection to database like a url or like params.

config

from saconfig import MysqlConfig

mysql = MysqlConfig()
print(mysql.url)
print(mysql.host)
print(mysql.port)
print(mysql.username)
print(mysql.password)
print(mysql.database)

env

MYSQL_URL=mysql://username:pass@0.0.0.0:3306/test_db

# the same with

MYSQL_HOST=0.0.0.0
MYSQL_PORT=3306
MYSQL_USER=username
MYSQL_PASSWORD=pass
MYSQL_DATABASE=test_db

output

mysql://username:pass@0.0.0.0:3306/test_db
0.0.0.0
3306
username
pass
mysql

> django integrations

For presets sql database configs you can use .as_django() to setup connection in DATABASES

config

from saconfig import MysqlConfig

mysql = MysqlConfig()
print(mysql.as_django())

output

{'ENGINE': 'django.db.backends.mysql', 'USER': 'username', 'PASSWORD': 'pass', 'HOST': '0.0.0.0', 'PORT': '3306', 'NAME': 'test_db'}

For django basic settings its posible to use:

for k, v in DjangoCommonSettings().items():
    globals()[k] = v

Metadata

Release files for saconfig 0.0.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for saconfig 0.0.3
File Size Uploaded Provenance
saconfig-0.0.3.tar.gz 17.5 kB Without provenance Details

Release files / saconfig-0.0.3.tar.gz

Download URL saconfig-0.0.3.tar.gz
Size 17.5 kB
Tags Source
SHA-256 checksum
How to use checksums
76c163894e995b3a788d587d9fcd42799d4c944319dbf00f399d08611b70cb0b
BLAKE2b-256 checksum
How to use checksums
70aae23923965f78f4d51566d81027e9681b2767988486b0c7cd90928ee750c6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.11.10

Security for saconfig 0.0.3

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.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page