-
Notifications
You must be signed in to change notification settings - Fork 8
Quickstart
The most important functions and classes may be imported via the pyradox
package directly. You can use pyradox.parse(s)
to parse a string into a pyradox.struct.Tree
. For example:
>>> import pyradox
>>> tree = pyradox.parse('lorem = ipsum dolor = {sit = arnet} consectetur = {1 2 3 4}')
>>> print(tree)
produces:
lorem = ipsum
dolor = {
sit = arnet
}
consectetur = {
1 2 3 4
}
You can parse from a .txt file using pyradox.parseFile(path)
, or iterate over the tree corresponding to the files in a directory using pyradox.parseDir(path)
.
Trees can be used much like dictionaries:
>>> tree['lorem']
'ipsum'
>>> tree['lorem'] = 10.0; print(tree)
lorem = 10.0
dolor = {
sit = arnet
}
consectetur = {
1 2 3 4
}
>>> list(tree.keys())
['lorem', 'dolor', 'consectetur']
However, unlike dictionaries they are ordered and may include duplicate keys:
>>> tree.append('lorem', 20.0); print(tree)
lorem = 10.0
dolor = {
sit = arnet
}
consectetur = {
1 2 3 4
}
lorem = 20.0
Keys are printed with case, but are matched case insensitive:
>>> list(tree.findAll('LOREM'))
[10.0, 20.0]
pyradox determines the type of primitives when parsing automatically.
-
bool
: "yes" and "no" are parsed as booleans. -
int
: Numbers without a decimal point are parsed as integers. -
float
: Numbers with a decimal point are parsed as floats. -
str
: Anything in double quotes is parsed as a string. This is also the default type.
There is also one special primitive, the pyradox.primitive.Date
, which results from tokens with two periods separating numbers. You may construct a date using any of the following:
pyradox.Date('1444.1.1')
pyradox.Date((1444, 1, 1))