From: timv Date: Wed, 19 Dec 2012 00:11:45 +0000 (-0500) Subject: exciting new aggregators and command-line options. X-Git-Url: https://hydra-www.ietfng.org/gitweb/?a=commitdiff_plain;h=78c3b18eb0b79f11a57387e1e6996a0ccb27ea94;p=dyna2 exciting new aggregators and command-line options. --- diff --git a/bin/defn.py b/bin/defn.py index aac38fc..ab16217 100644 --- a/bin/defn.py +++ b/bin/defn.py @@ -42,9 +42,8 @@ class Aggregator(object): return 'Aggregator(%r, %r)' % (self.item, self.name) -class MultisetAggregator(Counter, Aggregator): - def __init__(self, item, name, folder): - self.folder = folder +class BAggregator(Counter, Aggregator): + def __init__(self, item, name): Aggregator.__init__(self, item, name) Counter.__init__(self) def inc(self, val): @@ -52,9 +51,17 @@ class MultisetAggregator(Counter, Aggregator): def dec(self, val): self[val] -= 1 def fold(self): - return self.folder(self) + return self def fromkeys(self, *_): - raise NotImplementedError + assert False, 'bah.' + + +class MultisetAggregator(BAggregator): + def __init__(self, item, name, folder): + self.folder = folder + BAggregator.__init__(self, item, name) + def fold(self): + return self.folder(self) class LastEquals(Aggregator): @@ -69,6 +76,20 @@ class LastEquals(Aggregator): return self.list[-1] +class SetEquals(Aggregator): + def __init__(self, item, name): + self.set = set([]) + Aggregator.__init__(self, item, name) + def inc(self, val): + self.set.add(val) + def dec(self, val): + self.set.pop(val) + def fold(self): + return self.set + def clear(self): + self.set.clear() + + def agg_bind(item, agg_decl): """ Bind declarations (map functor->string) to table (storing values) and @@ -138,4 +159,11 @@ def agg_bind(item, agg_decl): if agg_decl[fn] == ':=': return LastEquals(item, agg_decl[fn]) - return MultisetAggregator(item, agg_decl[fn], defs[agg_decl[fn]]) + elif agg_decl[fn] == 'bag=': + return BAggregator(item, agg_decl[fn]) + + elif agg_decl[fn] == 'set=': + return SetEquals(item, agg_decl[fn]) + + else: + return MultisetAggregator(item, agg_decl[fn], defs[agg_decl[fn]]) diff --git a/bin/interpreter.py b/bin/interpreter.py index ef54141..cb661ad 100644 --- a/bin/interpreter.py +++ b/bin/interpreter.py @@ -29,7 +29,7 @@ #from debug import saverr; saverr.enable(editor=True) import os, sys -from collections import defaultdict, Counter +from collections import defaultdict from argparse import ArgumentParser from utils import ip, red, green, blue, magenta from defn import agg_bind @@ -306,8 +306,9 @@ def go(): pass finally: dump_charts() - with file(argv.source + '.chart', 'wb') as f: - dump_charts(f) + if argv.output is not None: + with file(argv.output, 'wb') as f: + dump_charts(f) def dynac(f): @@ -350,8 +351,15 @@ def do(filename): parser = ArgumentParser(description=__doc__) parser.add_argument('source', help='Path to Dyna source file.') +parser.add_argument('-i', dest='interactive', action='store_true', help='Fire-up an IPython shell.') +parser.add_argument('-o', dest='output', help='Output chart.') + argv = parser.parse_args() +#if argv.output is None: +# argv.output = argv.source + '.chart' + do(argv.source) -ip() +if argv.interactive: + ip()