Skip to main content

python-wax - hierarchical configuration format and context object for Python

Quick Example

>>> from wax import Wax, parse_wax
>>> w = Wax(server=Wax(host='localhost'))
>>> w.server.port = 1234

>>> print 'hostname is %(server.host)s using port %(server.port)s' % w
hostname is localhost using port 1234

>>> s = str(w)
>>> print s

[server]
host = "localhost"
port = 1234

>>> w = parse_wax(s)
print w

[server]
host = "localhost"
port = 1234

Features

  • Context objects can be serialized to / from string

  • Simple hierarchical key structure

  • Key order is maintained

  • Values can be any JSON type

  • Supports comments and annotations

Overview

Easy to construct in code:

>>> w = Wax(message='hello world', num=17)

Access values with indexes:

>>> w['num'] = 18
>>> w.message = 'hello again'
>>> w.get('missing.key', 'no problem')
'no problem'

This object translates directly to a file format. Reading the format will reconstruct the context object exactly. This allows for “round-trip” configuration that can be serialized to utf-8, edited, and read back in:

>>> w = Wax(state='New York', zip=10003)
>>> s = str(w)
>>> print s
state = "New York"
zip = 10003

>>> q = parse_wax(s)
>>> print q
state = "New York"
zip = 10003

Key order is preserved when keys are added individually. If you add them in the constructor, initial order is governed by kwargs (dict) hashing order:

>>> w = Wax(a=1, b=2, c=3)
>>> print w
a = 1
c = 3
b = 2

>>> w = Wax()
>>> w.a = 1
>>> w.b = 2
>>> w.c = 3
>>> print w
a = 1
b = 2
c = 3

Create sublevels by attaching a Wax instance to a key:

>>> w = Wax(level1=Wax(level2=Wax(property='value')))
>>> w = Wax()
>>> w.level1 = Wax()
>>> w.level1.level2 = Wax()
>>> w.level1.level2.property = 'value'

Serializing this:

>>> print w
[level1.level2]
property = "value"

Or you can set dotted keys to create nested values:

>>> w = Wax()
>>> w['foo.bar'] = 123
>>> print w.foo.bar
123

Dotted access can be used in formatting strings:

>>> w = Wax(server=Wax(host='localhost', port=1234))
>>> print 'hostname is %(server.host)s using port %(server.port)s' % w
hostname is localhost using port 1234

Download files

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

Source Distribution

python-wax-0.3.tar.gz (15.7 kB view details)

Uploaded Source

File details

Details for the file python-wax-0.3.tar.gz.

File metadata

  • Download URL: python-wax-0.3.tar.gz
  • Upload date:
  • Size: 15.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for python-wax-0.3.tar.gz
Algorithm Hash digest
SHA256 ced3fa933b9619d812067f6d765dda169cea9b2b54b9ca0bb64dfa1b7a99247a
MD5 b648cbc30733ec32fbf47723c05dcd1e
BLAKE2b-256 9e166e057f20dbb18b33d054809b0847aad11366aae19b9038e3945b137cb2a2

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.3 This release

1 file

0.2

1 file

0.1

1 file

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