Skip to main content

A hierarchical configuration format and context object for Python

Project description

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

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

python-wax-0.1.tar.gz (14.4 kB view details)

Uploaded Source

File details

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

File metadata

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

File hashes

Hashes for python-wax-0.1.tar.gz
Algorithm Hash digest
SHA256 fcaa90ee1240b4399084ffae6a1e7fb6c8d4425fc66982cda5a517a08b1aaeeb
MD5 c547cb48f7d1480a2482719fda0ed555
BLAKE2b-256 9cdb789a96452dd596797a735471ab17190d28b2bbc91b056390cf5e7c514ea8

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