A minimalist wrapper for configparser.
Project description
snekcfg
is a minimalist wrapper over
configparser that offers
a more streamlined user experience.
Expected options and their types are predefined to prevent errors and minimize repetitive logic within your codebase. This allows for simple, direct access to your configuration within your program without worrying about typos or type conversion.
example.py
import snekcfg
cfg = snekcfg.Config('example.cfg')
sct = cfg.section('server')
# the default value is used to automatically type as an int
sct.define('port', 8080)
# or, you can be explicit
sct.define('host', default='127.0.0.1', type=str)
# some common types like set[str] are already built-in,
# but here is an example of adding a codec for a new type.
# *type* can be a type object, or just a string like 'str_set'
cfg.register_type(
type=set[str],
encode=lambda v: ','.join(v),
decode=lambda v: set(v.split(',')))
# sections can be accessed using dot notation (one level deep)
cfg.define('users.whitelist', default=set(), type=set[str])
# update values with dot notation
users = {'graham', 'john', 'terryg', 'eric', 'terryj', 'michael'}
cfg['users.whitelist'].update(users)
# or through the section
cfg.section('server')['port'] = 1337
# write to 'example.cfg'
cfg.write()
# let's tweak the config file externally
with open('example.cfg', 'r+') as f:
s = f.read()
f.seek(0)
f.write(s.replace('1337', '1234'))
# read from 'example.cfg'
cfg.read()
# types are preserved
assert cfg['server.port'] == 1234
assert cfg['users.whitelist'] == users, users
example.cfg
[server]
host = 127.0.0.1
port = 1337
[users]
whitelist = eric,graham,michael,john,terryg,terryj
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
No source distribution files available for this release.See tutorial on generating distribution archives.
Built Distribution
File details
Details for the file snekcfg-0.1.1-py2.py3-none-any.whl
.
File metadata
- Download URL: snekcfg-0.1.1-py2.py3-none-any.whl
- Upload date:
- Size: 5.0 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.8.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b60b00805b912e4e86f2376d7f9cd26701f26fdbffb0df2ebd47942822752149 |
|
MD5 | c32f57856cdeb7d030d4c49fd7aae87f |
|
BLAKE2b-256 | 5dec21cd3a9a929b6e2bea4292570e4a3fc2ae36dbe1fb77da3380803a5be5c7 |