Skip to main content

Tosholi

tests pre-commit.ci status

Tosholi is a simple library for reading and writing TOML configuration files using Python dataclasses. Tosholi means to interpret or translate and comes from the Chickasaw and Choctaw languages.

Installation

$ pip install tosholi

Get Started

Create your configuration as a Python dataclass. Configuration dataclasses can be nested, but can only be made of TOML support types.

Consider the following config.toml that we want to read into a dataclass. (This example is based on toml.io).

title = "TOML Example"

[owner]
name = "Tom Preston-Werner"
dob = 1979-05-27T07:32:00-08:00

[database]
enabled = true
ports = [ 8000, 8001, 8002 ]
temp_targets = { cpu = 79.5, case = 72.0 }

[servers]

[servers.alpha]
ip = "10.0.0.1"
role = "frontend"

[servers.beta]
ip = "10.0.0.2"
role = "backend"

We can describe the configuration format with dataclasses.

from __future__ import annotations

import dataclasses
from datetime import datetime

@dataclasses.dataclass
class Owner:
    name: str
    dob: datetime.datetime

@dataclasses.dataclass
class Database:
    enabled: bool
    ports: list[int]
    temp_targets: dict[str, float]

@dataclasses.dataclass
class Server:
    ip: str
    role: str

@dataclasses.dataclass
class Config:
    title: str
    owner: Owner
    database: Database
    servers: dict[str, Server]

Then the configuration file can be read using the Config dataclass as a the template for parsing.

with open('config.toml', 'rb') as f:
    config = tosholi.load(Config, f)

Similarly, we can convert in the opposite direction. A Config instance can be converted to a str with tosholi.dumps() or written it to a file with tosholi.dump().

import tosholi
from datetime import tzinfo

config = Config(
    title='TOML Example',
    owner=Owner(
        name='Tom Preston-Werner',
        dob=datetime(1979, 5, 27, 7, 32, 0),
    ),
    database=Database(
        enabled=True,
        ports=[8000, 8001, 8002],
        temp_targets={'cpu': 79.5, 'case': 72.0},
    ),
    servers={
        'alpha': Server(ip='10.0.0.1', role='frontend'),
        'beta': Server(ip='10.0.0.2', role='backend'),
    }
)

with open('config.toml', 'wb') as f:
    tosholi.dump(config, f)

Developing

We use tox for testing and pre-commit for linting. Get started for local development with:

$ tox --devenv venv -e py311
$ . venv/bin/activate
$ pre-commit install

or

$ python -m venv venv
$ . venv/bin/activate
$ pip install -e .[dev]
$ pre-commit install

Metadata

Release files for tosholi 0.1.0

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

Source distribution (sdist)

Source distribution for tosholi 0.1.0
File Size Uploaded
tosholi-0.1.0.tar.gz 18.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for tosholi 0.1.0
File Interpreter ABI Platform
tosholi-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 25.9 kB

Release files / tosholi-0.1.0.tar.gz

Download URL tosholi-0.1.0.tar.gz
Size 18.7 kB
Tags Source
SHA-256 checksum
How to use checksums
326ca4b8d8310b32af99e8be73a3b6628b30f3c8b4dbb8e7bc60e42a25a91a52
BLAKE2b-256 checksum
How to use checksums
032fdee68e05e0e0b61a100c66354e02ff57535bcd4dd6eaf43eda3d4620b061
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.4

Release files / tosholi-0.1.0-py3-none-any.whl

Download URL tosholi-0.1.0-py3-none-any.whl
Size 7.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ede72182603cbd295da0bfc7f812dba04f964c8886fa99eef5e35757d5aaae84
BLAKE2b-256 checksum
How to use checksums
5a7308bae9fa404bbea02433fb4ed84f4e61f450f08b5c5d206f1d98ae435bd1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.4

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

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