Skip to main content

Totally Awesome Type-aware TOML Loader

Project description

TATTL: a Totally Awesome Type-aware TOML Loader

Usage example

import tattl
import tomllib

from dataclasses import dataclass, field
from pprint import pp

my_toml = """
title = "TOML Example"

[points]
alice = 17
bob = 12
charlie = 7

[garden]
sunny = true
elevation-map = [
    [1, 1, 1, 2, 1],
    [1, 2, 2, 2, 1],
    [1, 2, 3, 3, 2],
    [1, 2, 2, 3, 1],
    [1, 1, 1, 2, 1],
]

[garden.flowers]
roses = { amount = 7, growth = 0.90 }
daffodils = { amount = 3, growth = 0.54 }
daisies = { amount = 12, growth = 0.21 }

[fruits.apples]
color = "red"
tastes = ["sweet", "sour"]

[fruits.mangoes]
color = "orange"
tastes = ["sweet", "citrus"]
"""


@dataclass
class Structure:
    title: str
    points: dict[str, int]

    @dataclass
    class Garden:
        sunny: bool
        elevation_map: list[list[int]] = field(metadata={"name": "elevation-map"})

        @dataclass
        class Flower:
            amount: int
            growth: float

        flowers: dict[str, Flower]

    garden: Garden

    @dataclass
    class Fruit:
        color: str
        tastes: list[str]

    fruits: dict[str, Fruit]


data = tattl.unpack_dict(tomllib.loads(my_toml), Structure)

pp(data)

# Structure(title='TOML Example',
#           points={'alice': 17, 'bob': 12, 'charlie': 7},
#           garden=Garden(sunny=True,
#                         elevation_map=[[1, 1, 1, 2, 1],
#                                        [1, 2, 2, 2, 1],
#                                        [1, 2, 3, 3, 2],
#                                        [1, 2, 2, 3, 1],
#                                        [1, 1, 1, 2, 1]],
#                         flowers={'roses': Flower(amount=7,
#                                                  growth=0.9),
#                                  'daffodils': Flower(amount=3,
#                                                      growth=0.54),
#                                  'daisies': Flower(amount=12,
#                                                    growth=0.21)}),
#           fruits={'apples': Fruit(color='red',
#                                   tastes=['sweet', 'sour']),
#                   'mangoes': Fruit(color='orange',
#                                    tastes=['sweet', 'citrus'])})

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tattl-0.2.1.tar.gz (11.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

tattl-0.2.1-py3-none-any.whl (6.0 kB view details)

Uploaded Python 3

File details

Details for the file tattl-0.2.1.tar.gz.

File metadata

  • Download URL: tattl-0.2.1.tar.gz
  • Upload date:
  • Size: 11.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.5.6

File hashes

Hashes for tattl-0.2.1.tar.gz
Algorithm Hash digest
SHA256 37acc1126a80396773f2ea1319c607cca1939f9150fb62ab04cda5ebf64ee0ee
MD5 90877b616d9697724b83de22ef575fca
BLAKE2b-256 d5869a6ebe195acaad1825dba5ece2ea13a650ddcb583f8475f576376842f4b6

See more details on using hashes here.

File details

Details for the file tattl-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: tattl-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 6.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.5.6

File hashes

Hashes for tattl-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 d50fafa497cf859bde78f2786065b9aaced603c1f6274a2d600e04ffaa783fa8
MD5 83a8e5072a5291764fc50cf7dcd20c7d
BLAKE2b-256 1958c176595f1725500de08f014424a7dbf7107fe876425b4ce63673381d7337

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page