A pre-processor for TOML files.
Project description
This package defines a TOMLParser class which allows you to employ an extended syntax in your TOML files.
Defaults
Consider this scrap of TOML:
[A] [B] flavour = "strawberry"
Confusion lets you add a DEFAULT section just like the traditional Python configparser module:
[DEFAULT] flavour = "vanilla" [A] [B] flavour = "strawberry"
Substitution
By applying the extended substitution syntax, you can define a value in only one location and reference it elsewhere (DRY principle):
[A] flavour = "strawberry" [B] flavour = ${A:flavour}
Example
Let’s use this scrap of TOML for demonstration:
text = """ [DEFAULT] flavour = "vanilla" flake = false [A] flavour = "strawberry" [B] flavour = ${A:flavour} flake = true """
The simplest way to construct a parser is using the from_string class method:
parser = TOMLParser.from_string(text)
To create a TOMLParser object from a TOML file, just use the pathlib module:
import pathlib from confusion import TOMLParser text = pathlib.Path("my.toml").read_text() parser = TOMLParser.from_string(text)
A TOMLParser object has all the methods of Python’s standard ConfigParser. There is one difference; the sections attribute is a property which returns a dictionary:
print(parser.sections) >>> {'A': <Section: A>, 'B': <Section: B>}
The hierarchical TOML data is available via the object’s tables property:
print(parser.tables) >>> {'A': {'flavour': 'strawberry', 'flake': False}, 'B': {'flavour': 'strawberry', 'flake': True}}
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
Built Distribution
Hashes for confusion-0.2.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 12c0a145003d747798bc08ae62a93bc94999462cfb094157f800d00d7dca115d |
|
MD5 | 6825451e469dabee84d6fb6c92b290ff |
|
BLAKE2b-256 | a90e1592a300bf23af6b297ab18940f7babcac2a94c2b0a7f4c15247f2b45094 |