parcon.pargen
index
/home/jcp/github/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:
 
>>> json.format([True,1,{"2":3,"4":None},5,None,False,"hello"]).text
'[true, 1, {"2": 3, "4": null}, 5, null, false, "hello"]'
 
You'll probably want to take a look at the Formatter class. It's the "main"
class for pargen, analogous to parcon.Parser. It contains some module
documentation that can probably help to get you started.

 
Package Contents
       

 
Classes
       
__builtin__.object
Formatter
And
First
ForEach
Forward
Is
IsExactly
Literal
Repr
String
Then
Type
Result
_ListExtremity(Formatter)
Back
Front
Head
Tail

 
class And(Formatter)
    A formatter that acts like its second formatter, except that its first
formatter must succeed in order for the second formatter to be considered.
What the first formatter consumes will be ignored; the second formatter
will be provided with the exact value that was passed into the And instance.
 
This could be used with Type, for example, to make a certain formatter only
succeed if its input is of a specific type; for example:
 
>>> int_formatter = Type(int, long) & String()
 
would be a formatter that formats ints and longs as per the String
formatter but that fails if any other type is passed to it.
 
 
Method resolution order:
And
Formatter
__builtin__.object

Methods defined here:
__init__(self, first, second)
format(self, input)

Methods inherited from Formatter:
__add__ = op_add(first, second)
__and__ = op_and(first, second)
__or__ = op_or(first, second)
__radd__ = new_function(x, y)
__rand__ = new_function(x, y)
__ror__ = new_function(x, y)

Data descriptors inherited from Formatter:
__dict__
dictionary for instance variables (if defined)
__weakref__
list of weak references to the object (if defined)

 
class Back(_ListExtremity)
    Same as Front, but this operates on the last item in the list instead of
the first item.
 
 
Method resolution order:
Back
_ListExtremity
Formatter
__builtin__.object

Methods inherited from _ListExtremity:
__init__(self, formatter)
format(self, input)

Methods inherited from Formatter:
__add__ = op_add(first, second)
__and__ = op_and(first, second)
__or__ = op_or(first, second)
__radd__ = new_function(x, y)
__rand__ = new_function(x, y)
__ror__ = new_function(x, y)

Data descriptors inherited from Formatter:
__dict__
dictionary for instance variables (if defined)
__weakref__
list of weak references to the object (if defined)

 
class First(Formatter)
    A formatter that attempts to apply all of its formatters in sequence to the
value provided to FirstFirst then acts exactly like the first of its
formatters to succeed. Each formatter is passed a fresh copy of the value
provided to First, without regard to what the formatter applied before it
may have consumed.
 
If none of the formatters match, First fails.
 
 
Method resolution order:
First
Formatter
__builtin__.object

Methods defined here:
__init__(self, *formatters)
format(self, input)

Methods inherited from Formatter:
__add__ = op_add(first, second)
__and__ = op_and(first, second)
__or__ = op_or(first, second)
__radd__ = new_function(x, y)
__rand__ = new_function(x, y)
__ror__ = new_function(x, y)

Data descriptors inherited from Formatter:
__dict__
dictionary for instance variables (if defined)
__weakref__
list of weak references to the object (if defined)

 
class ForEach(Formatter)
    A formatter that expects a sequence or dict as input. If the input is a
dict, its items() method will be called, and the resulting list used as the
input sequence. For each item in the input sequence, ForEach calls the
specified formatter, passing in that item. The results of all of these
formatters are then concatenated into a single string, separated by the
specified delimiter string (which defaults to the empty string). This
string is then returned. ForEach consumes all of the input so that the
remainder is the empty list.
 
 
Method resolution order:
ForEach
Formatter
__builtin__.object

Methods defined here:
__init__(self, formatter, delimiter='')
format(self, input)

Methods inherited from Formatter:
__add__ = op_add(first, second)
__and__ = op_and(first, second)
__or__ = op_or(first, second)
__radd__ = new_function(x, y)
__rand__ = new_function(x, y)
__ror__ = new_function(x, y)

Data descriptors inherited from Formatter:
__dict__
dictionary for instance variables (if defined)
__weakref__
list of weak references to the object (if defined)

 
class Formatter(__builtin__.object)
    The main class of this module, analogous to parcon.Parser, but for
formatters.
 
Like parcon.Parser, instances of Formatter should not be directly
constructed; instances of its various subclasses should be created and used
instead.
 
The main method that you'll want to look at is format.
 
  Methods defined here:
__add__ = op_add(first, second)
__and__ = op_and(first, second)
__or__ = op_or(first, second)
__radd__ = new_function(x, y)
__rand__ = new_function(x, y)
__ror__ = new_function(x, y)
format(self, input)
Formats a specified input object into a piece of text. Subclasses of
Formatter should override this and provide an actual implementation.
 
The return value of this method is a Result object created by calling
either the match function or the failure function. The former function
is used to indicate success; the latter is used to indicate that the
formatter failed for some reason, such as the input not being of an
appropriate type.

Data descriptors defined here:
__dict__
dictionary for instance variables (if defined)
__weakref__
list of weak references to the object (if defined)

 
class Forward(Formatter)
    A forward-declared formatter. This allows for mutually-recursive
formatters; the actual underlying formatter that a particular Forward
delegates to can be set later on after the Forward is created.
 
A formatter can be set into this Forward by doing:
 
some_forward_formatter << formatter_to_delegate_to
 
or:
 
some_forward_formatter.set(formatter_to_delegate_to)
 
It's important to remember that << is not the lowest precedence of all
operators; you'll probably want to wrap the right-hand side in parentheses
in order to avoid precedence issues that might otherwise occur.
 
 
Method resolution order:
Forward
Formatter
__builtin__.object

Methods defined here:
__init__(self, formatter=None)
__lshift__ = set(self, formatter)
format(self, input)
set(self, formatter)

Methods inherited from Formatter:
__add__ = op_add(first, second)
__and__ = op_and(first, second)
__or__ = op_or(first, second)
__radd__ = new_function(x, y)
__rand__ = new_function(x, y)
__ror__ = new_function(x, y)

Data descriptors inherited from Formatter:
__dict__
dictionary for instance variables (if defined)
__weakref__
list of weak references to the object (if defined)

 
class Front(_ListExtremity)
    Same as Head, but the remainder of this parser is exactly the value passed
to it, I.E. it doesn't consume any input. Thus the formatter:
 
>>> first_three_times = Front(String()) + Front(String()) + Front(String())
>>> first_three_times.format("12345").text
'111'
 
 
Method resolution order:
Front
_ListExtremity
Formatter
__builtin__.object

Methods inherited from _ListExtremity:
__init__(self, formatter)
format(self, input)

Methods inherited from Formatter:
__add__ = op_add(first, second)
__and__ = op_and(first, second)
__or__ = op_or(first, second)
__radd__ = new_function(x, y)
__rand__ = new_function(x, y)
__ror__ = new_function(x, y)

Data descriptors inherited from Formatter:
__dict__
dictionary for instance variables (if defined)
__weakref__
list of weak references to the object (if defined)

 
class Head(_ListExtremity)
    A formatter meant to be used on lists. It's constructed with another
formatter. When it's called, it expects some sort of sequence; it fails if
the value provided to it is not a sequence, or if it's an empty sequence.
If there's at least one item, this formatter calls its underlying formatter
with the first item in the sequence. It returns whatever this formatter
returns, with the remainder being all of the list items except for the
first. In this way, repeated invocations of Head remove items from the
front of the list, so, for example, the formatter:
 
>>> first_three = Head(String()) + Head(String()) + Head(String())
>>> first_three.format("12345").text
'123'
 
 
Method resolution order:
Head
_ListExtremity
Formatter
__builtin__.object

Methods inherited from _ListExtremity:
__init__(self, formatter)
format(self, input)

Methods inherited from Formatter:
__add__ = op_add(first, second)
__and__ = op_and(first, second)
__or__ = op_or(first, second)
__radd__ = new_function(x, y)
__rand__ = new_function(x, y)
__ror__ = new_function(x, y)

Data descriptors inherited from Formatter:
__dict__
dictionary for instance variables (if defined)
__weakref__
list of weak references to the object (if defined)

 
class Is(Formatter)
    A formatter that consumes no input and returns the empty string. However,
it only succeeds if its input is equal, as per the == operator, to a value
provided to the Is instance when it's constructed.
 
 
Method resolution order:
Is
Formatter
__builtin__.object

Methods defined here:
__init__(self, value)
format(self, input)

Methods inherited from Formatter:
__add__ = op_add(first, second)
__and__ = op_and(first, second)
__or__ = op_or(first, second)
__radd__ = new_function(x, y)
__rand__ = new_function(x, y)
__ror__ = new_function(x, y)

Data descriptors inherited from Formatter:
__dict__
dictionary for instance variables (if defined)
__weakref__
list of weak references to the object (if defined)

 
class IsExactly(Formatter)
    Same as Is, but IsExactly uses Python's is operator instead of Python's ==
operator to perform the equality check. This should be used for True,
False, None, and other such values.
 
 
Method resolution order:
IsExactly
Formatter
__builtin__.object

Methods defined here:
__init__(self, value)
format(self, input)

Methods inherited from Formatter:
__add__ = op_add(first, second)
__and__ = op_and(first, second)
__or__ = op_or(first, second)
__radd__ = new_function(x, y)
__rand__ = new_function(x, y)
__ror__ = new_function(x, y)

Data descriptors inherited from Formatter:
__dict__
dictionary for instance variables (if defined)
__weakref__
list of weak references to the object (if defined)

 
class Literal(Formatter)
    A formatter that outputs a specified piece of literal text. It doesn't
consume any of the input.
 
 
Method resolution order:
Literal
Formatter
__builtin__.object

Methods defined here:
__init__(self, text)
format(self, input)

Methods inherited from Formatter:
__add__ = op_add(first, second)
__and__ = op_and(first, second)
__or__ = op_or(first, second)
__radd__ = new_function(x, y)
__rand__ = new_function(x, y)
__ror__ = new_function(x, y)

Data descriptors inherited from Formatter:
__dict__
dictionary for instance variables (if defined)
__weakref__
list of weak references to the object (if defined)

 
class Repr(Formatter)
    Same to String(), but this formatter uses repr() instead of str() to do the
actual formatting.
 
 
Method resolution order:
Repr
Formatter
__builtin__.object

Methods defined here:
format(self, input)

Methods inherited from Formatter:
__add__ = op_add(first, second)
__and__ = op_and(first, second)
__or__ = op_or(first, second)
__radd__ = new_function(x, y)
__rand__ = new_function(x, y)
__ror__ = new_function(x, y)

Data descriptors inherited from Formatter:
__dict__
dictionary for instance variables (if defined)
__weakref__
list of weak references to the object (if defined)

 
class Result(__builtin__.object)
    A formatter result. Instances of this class are returned from
Formatter.format.
 
Two fields are present: text and remainder. If this result represents
failure of a formatter, text will be None and remainder will be unspecified.
If this result represents success, text will be the text produced by the
formatter, and remainder will be the portion of the input object that the
parser did not consume.
 
Result objects have a boolean truth value corresponding to whether or not
they succeeded. For example, this could be used to print whether some
particular result succeeded:
 
if some_result:
    print "Result succeeded"
else:
    print "Result failed"
 
  Methods defined here:
__init__(self, text, remainder)
__nonzero__(self)
__repr__ = __str__(self)
__str__(self)

Data descriptors defined here:
__dict__
dictionary for instance variables (if defined)
__weakref__
list of weak references to the object (if defined)

 
class String(Formatter)
    A formatter that formats whatever data it's provided as input using
Python's str() function. This is typically the formatter that you'll use
to format numbers and other things like that. The remainder is always None.
 
 
Method resolution order:
String
Formatter
__builtin__.object

Methods defined here:
format(self, input)

Methods inherited from Formatter:
__add__ = op_add(first, second)
__and__ = op_and(first, second)
__or__ = op_or(first, second)
__radd__ = new_function(x, y)
__rand__ = new_function(x, y)
__ror__ = new_function(x, y)

Data descriptors inherited from Formatter:
__dict__
dictionary for instance variables (if defined)
__weakref__
list of weak references to the object (if defined)

 
class Tail(_ListExtremity)
    Same as Head, but this operates on and removes the last item in the list
instead of the first item.
 
 
Method resolution order:
Tail
_ListExtremity
Formatter
__builtin__.object

Methods inherited from _ListExtremity:
__init__(self, formatter)
format(self, input)

Methods inherited from Formatter:
__add__ = op_add(first, second)
__and__ = op_and(first, second)
__or__ = op_or(first, second)
__radd__ = new_function(x, y)
__rand__ = new_function(x, y)
__ror__ = new_function(x, y)

Data descriptors inherited from Formatter:
__dict__
dictionary for instance variables (if defined)
__weakref__
list of weak references to the object (if defined)

 
class Then(Formatter)
    A formatter that applies two formatters one after the other, concatenating
their results and returning them. The remainder of the first formatter will
be passed to the second formatter as its value, and the remainder of Then
will be the remainder of the second formatter.
 
If either formatter fails, Then also fails.
 
 
Method resolution order:
Then
Formatter
__builtin__.object

Methods defined here:
__init__(self, first, second)
format(self, input)

Methods inherited from Formatter:
__add__ = op_add(first, second)
__and__ = op_and(first, second)
__or__ = op_or(first, second)
__radd__ = new_function(x, y)
__rand__ = new_function(x, y)
__ror__ = new_function(x, y)

Data descriptors inherited from Formatter:
__dict__
dictionary for instance variables (if defined)
__weakref__
list of weak references to the object (if defined)

 
class Type(Formatter)
    A formatter that produces the empty string and consumes no input. However,
it only succeeds if the value passed to it matches at least one of the
specified static types. Each of those types can be a Python class or a
static type as defined by parcon.static.
 
 
Method resolution order:
Type
Formatter
__builtin__.object

Methods defined here:
__init__(self, *static_types)
format(self, input)

Methods inherited from Formatter:
__add__ = op_add(first, second)
__and__ = op_and(first, second)
__or__ = op_or(first, second)
__radd__ = new_function(x, y)
__rand__ = new_function(x, y)
__ror__ = new_function(x, y)

Data descriptors inherited from Formatter:
__dict__
dictionary for instance variables (if defined)
__weakref__
list of weak references to the object (if defined)

 
Functions
       
failure()
Method called by formatters to create a new Result object indicating
failure. Formatters typically fail when their input was not in the format
that they expected it to be, or for other reasons. Head, for example, fails
if the provided value is not a sequence, or if the sequence provided is
empty.
match(text, remainder)
Method called by formatters to create a new Result object indicating
success. text is the text that the formatter produced; this can be the
empty string, but it must be a string of some sort. remainder is the
portion of the input value that the formatter did not consume; parser such
as Then, for example, pass the remainder of their first parser as the value
to their second parser.
op_add(first, second)
op_and(first, second)
op_or(first, second)
promote(value)
reversed(function)

 
Data
        sequence_or_dict_type = Or(Sequence(), Type(<type 'dict'>))
sequence_type = Sequence()