]> hydra-www.ietfng.org Git - dyna2/commitdiff
Reorganize imports so that loader/post-processor deps only matter when you ask
authorTim Vieira <tim.f.vieira@gmail.com>
Fri, 28 Jun 2013 19:05:26 +0000 (15:05 -0400)
committerTim Vieira <tim.f.vieira@gmail.com>
Fri, 28 Jun 2013 19:05:26 +0000 (15:05 -0400)
for them (e.g. matplotlib) and that ./dyna starts-up quicker.

.gitignore
src/Dyna/Backend/Python/__init__.py [new file with mode: 0644]
src/Dyna/Backend/Python/defn.py
src/Dyna/Backend/Python/interpreter.py
src/Dyna/Backend/Python/load/__init__.py
src/Dyna/Backend/Python/post/__init__.py
src/Dyna/Backend/Python/post/trace.py
src/Dyna/Backend/Python/repl.py
src/Dyna/Backend/Python/utils.py
test/repl/load.dyna

index ec9fd4703dca8157fcadd57630ef7f78328fe42d..49f4a3ef94fa75f2afbde882b05831f4004d8bad 100644 (file)
@@ -4,6 +4,8 @@
 *.swp
 *.pyc
 
+*.d   # debugger's output directory.
+
 dist/
 examples/*.d
 examples/*.hist
diff --git a/src/Dyna/Backend/Python/__init__.py b/src/Dyna/Backend/Python/__init__.py
new file mode 100644 (file)
index 0000000..e69de29
index 1859ee34bfba5c3e9df3db209548d56aec26529d..a45b35e5fb08b43e731dd994746e7b8a28b5228b 100644 (file)
@@ -5,7 +5,7 @@ from __future__ import division
 
 import operator
 from collections import Counter
-
+from utils import drepr, _repr
 
 class Aggregator(object):
     def fold(self):
@@ -61,10 +61,6 @@ def user_vars(variables):
                  if name.startswith('u') and not name.startswith('u_'))
 
 
-from term import _repr
-def drepr(vs):
-    return '{%s}' %  ', '.join('%s=%s' % (k, _repr(v)) for k,v in vs.iteritems())
-
 from collections import namedtuple
 class Result(namedtuple('Result', 'value variables')):
     def __repr__(self):
index 2bf13b9b426c5259e97010584129e00c282afadd..f55ddbbe4c7e0f4e21cdb32cb2d95a43284fb49c 100644 (file)
@@ -552,7 +552,7 @@ class Interpreter(object):
         for k, v in env.agg_decl.items():
             self.new_fn(k, v)
 
-        new_rules = set()    
+        new_rules = set()
         for _, r, _ in env.queries:
             new_rules.add(r)
         for r, _ in env.initializers:
index 79da4baedb7a7a091e7d985eb61a0c403b3714c4..462a3b41b83e87eb091e04ebb7bdbeb729eb68f9 100644 (file)
@@ -1,9 +1,12 @@
-from sexpr import sexpr
-from tsv import tsv
-from matrix import matrix
-from pickled import pickled
+#from sexpr import sexpr
+#from tsv import tsv
+#from matrix import matrix
+#from pickled import pickled
 
 import re as _re
+from utils import get_module
+
+available = 'sexpr', 'tsv', 'matrix'
 
 def run(interp, line):
     try:
@@ -14,11 +17,10 @@ def run(interp, line):
         print
         return
 
-    try:
-        m = getattr(__import__('load'), module)(interp, name)
-    except KeyError:
-        print 'did not recognize post-processor %r' % name
+    if module not in available:
+        print 'did not recognize loader %r' % name
         return
 
+    m = get_module('load', module)(interp, name)
     exec 'm.main(%s)' % args
     interp.go()
index 1484307022f41735660b43e441d1a0ab5e6d6f61..f4f3e7b4cf5318bb2ece54a36e5dcdec9985ca39 100644 (file)
@@ -1,24 +1,21 @@
 import re as _re
-from save import save
-from graph import graph
-from draw_circuit import draw_circuit
-from dump_solution import dump_solution
-from trace import trace
+from utils import get_module
+
+available = 'trace', 'dump_solution', 'draw_circuit', 'graph', 'save'
 
 def run(interp, line):
 
     try:
-        [(name, args)] = _re.findall('([a-z][a-zA-Z_0-9]*)\((.*)\)$', line.strip())
+        [(module, args)] = _re.findall('([a-z][a-zA-Z_0-9]*)\((.*)\)$', line.strip())
     except ValueError:
         print 'Error: failed to parse post command.'
         print '    %s' % line
         print
         return
 
-    try:
-        m = globals()[name](interp)
-    except KeyError:
-        print 'did not recognize post-processor %r' % name
+    if module not in available:
+        print 'did not recognize post-processor %r' % module
         return
 
+    m = get_module('post', module)(interp)
     eval('m.main(%s)' % args)
index 10b3cd14a44e6dc3b9a8fe457b90390683caa917..fe7a590e0b13c444567e4c542e8e28417e868126 100644 (file)
@@ -6,9 +6,7 @@ TODO: backchained stuff
 """
 
 import re
-from utils import yellow, green, red
-from defn import drepr
-from term import _repr
+from utils import yellow, green, red, _repr, drepr
 import debug, defn
 from cStringIO import StringIO
 from utils import lexer, subst
index afe02900876d49b89783bc9c94102368ac9f1d54..1ebc77b71d11862b9acee23ecf1285bbbb507a3a 100644 (file)
@@ -16,21 +16,13 @@ to help.
 TODO: $include load rules from a file.
 """
 
-import re, os, cmd, readline
-
-import debug, interpreter
-from utils import dynac, ip, lexer, subst
+import os, cmd, readline
+from utils import dynac, ip, lexer, subst, drepr, _repr
 from errors import DynaCompilerError, DynaInitializerException
-from chart import _repr
 from config import dotdynadir
-
 from errors import show_traceback
-import load, post
-
 from interpreter import Interpreter, foo, none
-
-from term import _repr
-from defn import drepr
+import load, post
 
 
 class REPL(cmd.Cmd, object):
@@ -134,6 +126,7 @@ class REPL(cmd.Cmd, object):
         """
         Development tool. Used for view Dyna's intermediate representations.
         """
+        import debug
         with file(dotdynadir / 'repl-debug-line.dyna', 'wb') as f:
             f.write(line)
         debug.main(f.name)
@@ -211,8 +204,8 @@ class REPL(cmd.Cmd, object):
          - `query` shows variable bindings applied to query
 
             :- query f(X)
-                1  f(1)
-                4  f(2)
+                1 =* f(1)
+                4 =* f(2)
 
         """
         results = self._query(q)
@@ -327,8 +320,9 @@ class REPL(cmd.Cmd, object):
                 [cmd, sub] = mod
                 if cmd in ('load', 'post'):
                     try:
-                        print getattr(globals()[cmd], sub).__doc__
-                    except (KeyError, AttributeError):
+                        exec 'from %s.%s import %s as m' % (cmd, sub, sub)
+                        print m.__doc__
+                    except (ImportError, KeyError, AttributeError):
                         print 'No help available for "%s %s"' % (cmd, sub)
                         return
                     else:
@@ -341,7 +335,7 @@ class REPL(cmd.Cmd, object):
 
         Available loaders:
 
-            {loaders}
+            {load}
 
         For more information about a particular loader type the following (in
         this case we get help for the `tsv` loader):
@@ -370,8 +364,6 @@ class REPL(cmd.Cmd, object):
             show_traceback()
             readline.write_history_file(self.hist)
 
-    do_load.__doc__ = do_load.__doc__.format(loaders=', '.join(x for x in dir(load) if not x.startswith('_')))
-
     def do_post(self, line):
         """
         Execute post-processor.
@@ -392,4 +384,6 @@ class REPL(cmd.Cmd, object):
             show_traceback()
             readline.write_history_file(self.hist)
 
-    do_post.__doc__ = do_post.__doc__.format(post=', '.join(x for x in dir(post) if not x.startswith('_')))
+
+    do_load.__doc__ = do_load.__doc__.format(load=', '.join(load.available))
+    do_post.__doc__ = do_post.__doc__.format(post=', '.join(post.available))
index 92162e405c9c184d469efc1f0f27af458b21a7ea..548e2c53ef2dc1feb90e9d036b2c263466c83b7e 100644 (file)
@@ -7,6 +7,7 @@ import signal
 from contextlib import contextmanager
 from collections import namedtuple
 
+
 def _repr(x):
     if x is True:
         return 'true'
@@ -21,10 +22,23 @@ def _repr(x):
         return repr(x)
 
 
+def drepr(vs):
+    return '{%s}' %  ', '.join('%s=%s' % (k, _repr(v)) for k,v in vs.iteritems())
+
+
 # interactive IPython shell
 ip = InteractiveShellEmbed(banner1 = 'Dropping into IPython\n')
 
 
+def get_module(cmd, sub):
+    try:
+        exec 'from %s.%s import %s as m' % (cmd, sub, sub)
+    except (ImportError, SyntaxError):
+        return
+    else:
+        return m
+
+
 black, red, green, yellow, blue, magenta, cyan, white = \
     map('\033[3%sm%%s\033[0m'.__mod__, range(8))
 
@@ -149,9 +163,9 @@ def parse_sexpr(e):
 class ANF(namedtuple('ANF', 'lines ruleix agg head evals unifs result')):
     pass
 
+
 def read_anf(e):
     def _g(x):
-#        return [(var, val[0], val[1:]) for var, val in x]
         for var, val in x:
             if isinstance(val, list):
                 yield (var, val[0], val[1:])
index 240278a8114879dad23973c305d7069382d9cb29..42bd9e6ee815fb912a4aeec9dc620dbc109b85f7 100644 (file)
@@ -3,13 +3,13 @@
 % binary rules
 rewrite(X, Y, Z) :=
     rules_tsv(Linenum, Cost, X, R),
-    cons(Y, &cons(Z, &nil)) is pycall("split", R, "\\s+"),
+    [Y, Z] is pycall("split", R, "\\s+"),
     pycall("float", Cost).
 
 % load unary rules
 rewrite(X, Y) :=
     rules_tsv(Linenum, Cost, X, R),
-    cons(Y, &nil) is pycall("split", R, "\\s+"),
+    [Y] is pycall("split", R, "\\s+"),
     pycall("float", Cost).
 
 phrase(S,X,I,K) += phrase(S,Y,I,K) + rewrite(X,Y).