[Checkins] SVN: manuel/trunk/src/manuel/table-example.txt move some parsing code from the evaluate step into the parse step
Benji York
benji at zope.com
Sat Jun 6 16:17:16 EDT 2009
Log message for revision 100666:
move some parsing code from the evaluate step into the parse step
Changed:
U manuel/trunk/src/manuel/table-example.txt
-=-
Modified: manuel/trunk/src/manuel/table-example.txt
===================================================================
--- manuel/trunk/src/manuel/table-example.txt 2009-06-06 18:01:53 UTC (rev 100665)
+++ manuel/trunk/src/manuel/table-example.txt 2009-06-06 20:17:15 UTC (rev 100666)
@@ -70,22 +70,48 @@
table_end = re.compile(r'\n=[= ]+\n(?=\Z|\n)', re.DOTALL)
class Table(object):
- def __init__(self, source):
- self.source = source
+ def __init__(self, expression, variables, examples):
+ self.expression = expression
+ self.variables = variables
+ self.examples = examples
- def find_tables(document):
+ def parse_tables(document):
for region in document.find_regions(table_start, table_end):
- table = Table(region.source)
+ lines = enumerate(iter(region.source.splitlines()))
+ lines.next() # skip the first line
+
+ # grab the expression to be evaluated
+ expression = lines.next()[1]
+ if expression.startswith('\\'):
+ expression = expression[1:]
+
+ lines.next() # skip the divider line
+ variables = [v.strip() for v in lines.next()[1].split()][:-1]
+
+ lines.next() # skip the divider line
+
+ examples = []
+ for lineno_offset, line in lines:
+ if line.startswith('='):
+ break # we ran into the final divider, so stop
+
+ values = [eval(v.strip(), {}) for v in line.split()]
+ inputs = values[:-1]
+ output = values[-1]
+
+ examples.append((inputs, output, lineno_offset))
+
+ table = Table(expression, variables, examples)
document.replace_region(region, table)
Instances of the class "Table" will represent the tables.
-Now we can create a Manuel Document from the source and use the "find_tables"
+Now we can create a Manuel Document from the source and use the "parse_tables"
function on it.
>>> import manuel
>>> document = manuel.Document(source, location='fake.txt')
- >>> find_tables(document)
+ >>> parse_tables(document)
If we examine the Docuement object we can see that the table was recognized.
@@ -134,26 +160,10 @@
if not isinstance(region.parsed, Table):
return
- lines = enumerate(iter(region.source.splitlines()))
- lines.next() # skip the first line
- expression = lines.next()[1]
- lines.next() # skip the divider line
- variables = [v.strip() for v in lines.next()[1].split()][:-1]
-
+ table = region.parsed
errors = TableErrors()
-
- lines.next() # skip the divider line
- for lineno_offset, line in lines:
- if line.startswith('='):
- break # we ran into the final divider, so stop
-
- values = [eval(v.strip(), {}) for v in line.split()]
- inputs = values[:-1]
- output = values[-1]
-
- if expression.startswith('\\'):
- expression = expression[1:]
- result = eval(expression, dict(zip(variables, inputs)))
+ for inputs, output, lineno_offset in table.examples:
+ result = eval(table.expression, dict(zip(table.variables, inputs)))
if result != output:
lineno = region.lineno + lineno_offset
errors.append(
@@ -191,7 +201,7 @@
... """
>>> document = manuel.Document(source_with_errors, location='fake.txt')
- >>> find_tables(document)
+ >>> parse_tables(document)
>>> region = list(document)[1]
This time the evaluator records errors:
@@ -214,7 +224,7 @@
if not isinstance(region.evaluated, TableErrors):
continue
- # if there were no errors, skip this table
+ # if there were no errors, there is nothing to report
if not region.evaluated:
continue
@@ -244,7 +254,7 @@
All the pieces (parsing, evaluating, and formatting) are available now, so we
just have to put them together into a single "Manuel" object.
- >>> m = manuel.Manuel(parsers=[find_tables], evaluaters=[evaluate_table],
+ >>> m = manuel.Manuel(parsers=[parse_tables], evaluaters=[evaluate_table],
... formatters=[format_table_errors])
Now we can create a fresh document and tell it to do all the above steps with
More information about the Checkins
mailing list