From: Tim Vieira Date: Wed, 7 Aug 2013 19:43:27 +0000 (-0400) Subject: Further improvments to coverage (now at 87%). X-Git-Url: https://hydra-www.ietfng.org/gitweb/?a=commitdiff_plain;h=4ff67cda03663060e53403fcf5bfbae5b66d6368;p=dyna2 Further improvments to coverage (now at 87%). --- diff --git a/src/Dyna/Backend/Python/load/__init__.py b/src/Dyna/Backend/Python/load/__init__.py index c21d142..51685f1 100644 --- a/src/Dyna/Backend/Python/load/__init__.py +++ b/src/Dyna/Backend/Python/load/__init__.py @@ -11,7 +11,6 @@ def run(interp, line): [(name, module, args)] = re.findall('^([a-z][a-zA-Z_0-9]*) = ([a-z][a-zA-Z_0-9]*)\((.*)\)', line) except ValueError: print 'Error: failed to parse load command.' - print ' %s' % line print return diff --git a/src/Dyna/Backend/Python/load/matrix.py b/src/Dyna/Backend/Python/load/matrix.py index dad044d..d934b0c 100644 --- a/src/Dyna/Backend/Python/load/matrix.py +++ b/src/Dyna/Backend/Python/load/matrix.py @@ -78,11 +78,6 @@ class matrix(object): with file(filename) as f: for i, line in enumerate(f): line = line.rstrip() - if not line: - continue - if delim is not None: - line = re.split(delim, line) - else: - line = [line] + line = re.split(delim, line) for j, v in enumerate(line): term((i, j), astype(v)) diff --git a/src/Dyna/Backend/Python/load/tsv.py b/src/Dyna/Backend/Python/load/tsv.py index 78211f6..768b770 100644 --- a/src/Dyna/Backend/Python/load/tsv.py +++ b/src/Dyna/Backend/Python/load/tsv.py @@ -12,7 +12,7 @@ class tsv(object): """ Load tab-delimited files. - > load row = tsv("test/repl/english.gr") + > load row = tsv("test/repl/data/english.gr") > sol row/4 ===== @@ -55,10 +55,5 @@ class tsv(object): with file(filename) as f: for i, line in enumerate(f): line = line.rstrip() - if not line: - continue - if delim is not None: - line = re.split(delim, line) - else: - line = [line] + line = re.split(delim, line) term([i] + line) diff --git a/src/Dyna/Backend/Python/post/__init__.py b/src/Dyna/Backend/Python/post/__init__.py index 135c869..9f6e534 100644 --- a/src/Dyna/Backend/Python/post/__init__.py +++ b/src/Dyna/Backend/Python/post/__init__.py @@ -11,7 +11,6 @@ def run(interp, line): [(module, args)] = re.findall('([a-z][a-zA-Z_0-9]*)\((.*)\)$', line) except ValueError: print 'Error: failed to parse post command.' - print ' %s' % line print return diff --git a/src/Dyna/Backend/Python/post/draw.py b/src/Dyna/Backend/Python/post/draw.py index 6de168b..1d557d1 100644 --- a/src/Dyna/Backend/Python/post/draw.py +++ b/src/Dyna/Backend/Python/post/draw.py @@ -1,5 +1,6 @@ import pylab as pl from collections import defaultdict +from stdlib import topython class draw(object): """ @@ -46,24 +47,26 @@ class draw(object): for item in frame[t]: fn = item. fn + args = map(topython, item.args) + if fn == 'title/1': - [title] = item.args + [title] = args ax.set_title(title) elif fn == 'xlim/2': - [a,b] = item.args + [a,b] = args ax.set_xlim(a,b) elif fn == 'ylim/2': - [a,b] = item.args + [a,b] = args ax.set_ylim(a,b) elif fn == 'line/2': - [(a,b), (c,d)] = item.args + [(a,b), (c,d)] = args ax.plot([a,c], [b,d], color='b', alpha=0.5) elif fn == 'text/2': - (s,(x,y)) = item.args + (s,(x,y)) = args ax.text(x,y,s) else: diff --git a/src/Dyna/Backend/Python/prioritydict.py b/src/Dyna/Backend/Python/prioritydict.py index 454dfb0..5bbaf94 100644 --- a/src/Dyna/Backend/Python/prioritydict.py +++ b/src/Dyna/Backend/Python/prioritydict.py @@ -1,5 +1,6 @@ from heapq import heapify, heappush, heappop + class prioritydict(dict): """Dictionary that can be used as a priority queue. diff --git a/src/Dyna/Backend/Python/repl.py b/src/Dyna/Backend/Python/repl.py index cb5800d..cda363f 100644 --- a/src/Dyna/Backend/Python/repl.py +++ b/src/Dyna/Backend/Python/repl.py @@ -347,15 +347,12 @@ class REPL(cmd.Cmd, object): if len(mod) == 2: [cmd, sub] = mod if cmd in ('load', 'post'): - try: - m = get_module(cmd, sub) + m = get_module(cmd, sub) + if m: print m.__doc__ - except (ImportError, KeyError, AttributeError): - print 'No help available for "%s %s"' % (cmd, sub) - return else: - return - print 'Error: Did not understand help command.' + print 'No help available for "%s %s"' % (cmd, sub) + return def do_load(self, line): """ diff --git a/src/Dyna/Backend/Python/stdlib.py b/src/Dyna/Backend/Python/stdlib.py index 5bcbc15..807ad70 100644 --- a/src/Dyna/Backend/Python/stdlib.py +++ b/src/Dyna/Backend/Python/stdlib.py @@ -1,7 +1,7 @@ import re, os from term import Term, Cons, Nil, MapsTo from collections import Counter -from utils import pretty, pretty_print, true, false, null, isbool +from utils import true, false, null, isbool from math import log, exp, sqrt from random import random as _random from glob import glob diff --git a/src/Dyna/Backend/Python/utils.py b/src/Dyna/Backend/Python/utils.py index 3d4a28d..63bd66b 100644 --- a/src/Dyna/Backend/Python/utils.py +++ b/src/Dyna/Backend/Python/utils.py @@ -149,17 +149,17 @@ def subst(term, v): # raise KeyboardInterrupt -class ddict(dict): - """ - Default Dict where the default function gets the key as an argument, unlike - collections.defaultdict. - """ - def __init__(self, f): - self.f = f - super(ddict, self).__init__() - def __missing__(self, x): - self[x] = y = self.f(x) - return y +#class ddict(dict): +# """ +# Default Dict where the default function gets the key as an argument, unlike +# collections.defaultdict. +# """ +# def __init__(self, f): +# self.f = f +# super(ddict, self).__init__() +# def __missing__(self, x): +# self[x] = y = self.f(x) +# return y def parse_sexpr(e): @@ -192,35 +192,35 @@ def parse_sexpr(e): return es -def pretty_print(t): - print pretty(t) - -def pretty(t, initialindent=0): - "Pretty print tree as a tabbified s-expression." - f = StringIO() - out = f.write - def pp(t, indent=initialindent, indentme=True): - if indentme: - out(' '*indent) - if isinstance(t, basestring): # base case - return out('%s' % t) - if len(t) == 1: - if t[0]: - pp('%s' % t[0], indent, indentme) - return - label, children = t[0], t[1:] - label = '%s' % label - assert isinstance(label, basestring) - out('(%s ' % label) - n = len(children) - for i, child in enumerate(children): - pp(child, indent + len(label) + 2, i != 0) # first child already indented - if i != n-1: # no newline after last child - out('\n') - out(')') - pp(t) - out('\n') - return f.getvalue() +#def pretty_print(t): +# print pretty(t) +# +#def pretty(t, initialindent=0): +# "Pretty print tree as a tabbified s-expression." +# f = StringIO() +# out = f.write +# def pp(t, indent=initialindent, indentme=True): +# if indentme: +# out(' '*indent) +# if isinstance(t, basestring): # base case +# return out('%s' % t) +# if len(t) == 1: +# if t[0]: +# pp('%s' % t[0], indent, indentme) +# return +# label, children = t[0], t[1:] +# label = '%s' % label +# assert isinstance(label, basestring) +# out('(%s ' % label) +# n = len(children) +# for i, child in enumerate(children): +# pp(child, indent + len(label) + 2, i != 0) # first child already indented +# if i != n-1: # no newline after last child +# out('\n') +# out(')') +# pp(t) +# out('\n') +# return f.getvalue() class ANF(namedtuple('ANF', 'span ruleix agg head evals unifs result')): diff --git a/test/repl/load.dynadoc b/test/repl/load.dynadoc index bbdd513..e25ba41 100644 --- a/test/repl/load.dynadoc +++ b/test/repl/load.dynadoc @@ -18,31 +18,6 @@ | goal(S) += phrase(S, "ROOT", 0, sentence_length(S)). -% Let's demonstrate what happens with bad filename. -> load xxx = tsv("path/does/not/exist") -file `path/does/not/exist` does not exist. -> load xxx = matrix("path/does/not/exist", astype=str) -file `path/does/not/exist` does not exist. -> load xxx = sexpr("path/does/not/exist") -file `path/does/not/exist` does not exist. - -% what if we pass in nonsense -> load xxx = qweopr/;"" - -Error: failed to parse load command. - xxx = qweopr/;"" - -> load xxx = yyy() - -did not recognize loader 'yyy' - - -> load xxx = tsv(") - -Syntax error: EOL while scanning string literal (, line 1) - - - > load rules_tsv = tsv("test/repl/data/english.gr") *ignore* > load token = matrix("test/repl/data/english.sen", astype=str) diff --git a/test/repl/post.dynadoc b/test/repl/post.dynadoc index 14c7335..d0bc569 100644 --- a/test/repl/post.dynadoc +++ b/test/repl/post.dynadoc @@ -18,3 +18,5 @@ Solution a. b. c. + +> post dump_solution("solution.txt") \ No newline at end of file diff --git a/test/repl/repl-misc.dynadoc b/test/repl/repl-misc.dynadoc index 82af397..9670589 100644 --- a/test/repl/repl-misc.dynadoc +++ b/test/repl/repl-misc.dynadoc @@ -119,3 +119,78 @@ no items matching `xxxx`. > help vquery See query. + + +> help load tsv + +Load tab-delimited files. + > load row = tsv("test/repl/data/english.gr") + > sol + row/4 + ===== + row(0,"0","S","NP VP") = true. + row(1,"1.58","ROOT","S .") = true. + row(2,"1.58","ROOT","S !") = true. + row(3,"1.58","ROOT","VP !") = true. + row(4,"3.81","VP","V") = true. + row(5,"3.81","VP","V NP") = true. + row(6,"1.49","VP","V VP") = true. + ... + +> load row = tsv("test/repl/data/english.gr") + + *ignore* + + +> help load doesnotexist + +No help available for "load doesnotexist" + + + + + + + +% Let's demonstrate what happens with bad filename. +> load xxx = tsv("path/does/not/exist") +file `path/does/not/exist` does not exist. +> load xxx = matrix("path/does/not/exist", astype=str) +file `path/does/not/exist` does not exist. +> load xxx = sexpr("path/does/not/exist") +file `path/does/not/exist` does not exist. + +% what if we pass in nonsense +> load xxx = qweopr/;"" + +Error: failed to parse load command. + +> load xxx = yyy() + +did not recognize loader 'yyy' + + +> load xxx = tsv(") + +Syntax error: EOL while scanning string literal (, line 1) + + +%" + + + +% Let's demonstrate what happens with bad filename. +> post doesnotexist + +Error: failed to parse post command. + +> post doesnotexist() + +did not recognize post-processor 'doesnotexist' + +> post draw_circuit(") + +Syntax error: EOL while scanning string literal (, line 1) + + +%" \ No newline at end of file