parcon.pargen | index /home/boydam/workspace/Parcon-python/parcon/parcon/pargen/__init__.py |
Pargen is a formatter combinator library. It's much the opposite of parcon:
while parcon parses text into objects, pargen formats objects into text.
I'll get more documentation up on here soon, but for now, here's a JSON
formatter (essentially a simplified reimplementation of Python's json.dumps):
from parcon.pargen import *
from decimal import Decimal
json = Forward()
number = Type(float, int, long, Decimal) & String()
boolean = Type(bool) & ((Is(True) & "true") | (Is(False) & "false"))
null = Is(None) & "null"
string = Type(basestring) & '"' + String() + '"'
json_list = Type(list, tuple) & ("[" + ForEach(json, ", ") + "]")
json_map =Type(dict) & ("{" + ForEach(Head(json) + ": " + Tail(json), ", ") + "}")
json << (boolean | number | null | string | json_list | json_map)
You can then do things like:
>>> print json.format([True,1,{"2":3,"4":None},5,None,False,"hello"]).text
[true, 1, {"2": 3, "4": null}, 5, null, false, "hello"]
Package Contents | ||||||
Classes | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Functions | ||
|
Data | ||
sequence_or_dict_type = Or(Sequence(), Type(<type 'dict'>)) sequence_type = Sequence() |