Here lies the embedded docblock documentation for the various parts of Reparse.
expression¶
-
reparse.expression.
AlternatesGroup
(expressions, final_function, name=u'')¶ Group expressions using the OR character
|
>>> from collections import namedtuple >>> expr = namedtuple(‘expr’, ‘regex group_lengths run’)(‘(1)’, [1], None) >>> grouping = AlternatesGroup([expr, expr], lambda f: None, ‘yeah’) >>> grouping.regex # doctest: +IGNORE_UNICODE ‘(?:(1))|(?:(1))’ >>> grouping.group_lengths [1, 1]
-
class
reparse.expression.
Expression
(regex, functions, group_lengths, final_function, name=u'')¶ Expressions are the building blocks of parsers.
Each contains:
- A regex pattern (lazily compiled on first usage)
- Group lengths, functions and names
- a
final_function
When an expression runs with
findall
orscan
, it matches a string using its regex, and returns the results from the parsing functions.-
findall
(string)¶ Parse string, returning all outputs as parsed by functions
-
run
(matches)¶ Run group functions over matches
-
scan
(string)¶ Like findall, but also returning matching start and end string locations
-
reparse.expression.
Group
(expressions, final_function, inbetweens, name=u'')¶ Group expressions together with
inbetweens
and with the output of afinal_functions
.
builders¶
-
class
reparse.builders.
Expression_Builder
(expressions_dict, function_builder)¶ Expression builder is useful for building regex bits with Groups that cascade down:
from GROUP (group).?,?(group)|(group) (group) to EXPRESSION (group) | (group) to TYPE (group)
>>> dummy = lambda input: input >>> get_function = lambda *_, **__: dummy >>> function_builder = lambda: None >>> function_builder.get_function = get_function >>> expression = {'greeting':{'greeting':{'Expression': '(hey)|(cool)', 'Groups' : ['greeting', 'cooly']}}} >>> eb = Expression_Builder(expression, function_builder) >>> eb.get_type("greeting").findall("hey, cool!") [[('hey',), ('',)], [('',), ('cool',)]]
-
class
reparse.builders.
Function_Builder
(functions_dict)¶ Function Builder is an on-the-fly builder of functions for expressions
>>> def t(_): ... return _ >>> fb = Function_Builder({"hey":t}) >>> fb.get_function("hey", "") is t True
tools¶
expression_checker¶
This module contains a useful function that aids in validating expression files by making sure each expression in a group are valid against the ‘Expression’, ‘Matches’, & ‘Non-Matches’ fields.
It is intended to be used against a testing_framework
that
contains assertIn, assertTrue, assertFalse (such as unittest).
Example Usage:
from reparse.tools.expression_checker import check_expression
import unittest
class cool_test(unittest.TestCase):
def test_coolness(self):
check_expression(self, load_yaml("parse/cool/expressions.yaml"))
-
reparse.tools.expression_checker.
check_expression
(testing_framework, expression_dict)¶ >>> class mock_framework: ... def assertIn(self, item, list, msg="Failed asserting item is in list"): ... if item not in list: raise Exception(msg) ... def assertTrue(self, value, msg="Failed asserting true"): ... if not value: raise Exception(msg) ... def assertFalse(self, value, msg): self.assertTrue(not value, msg) >>> check_expression(mock_framework(), ... {'class': {'group' :{'Matches': " 0 | 1", 'Non-Matches': "2 | 0 2", 'Expression': "[0-1]"}}})