def test_eval2(self): o = parse(''' (account (usename "raz") (started (date August 17 2005)) (renewed (date October 5 2009)) (score (+ 2 (* 3 7))) (score2 (quote (+ 2 (* 3 7)))) ) ''', {"account": sexp2dict, "date": lambda x, e: tuple(x[1:]), "+": lambda x, e: sum(map(lambda i: process(i, e), x[1:])), "*": lambda x, e: reduce(lambda a, b: a*b, map(lambda i: process(i, e), x[1:])), "quote": lambda x, e: x[1]}) expected = {Sym('score2'): [Sym('+'), 2, [Sym('*'), 3, 7]], Sym('usename'): 'raz', Sym('score'): 23, Sym('renewed'): (Sym('October'), 5, 2009), Sym('started'): (Sym('August'), 17, 2005)} # nopep8 self.assertEqual(o, expected) write_pretty( prepare(o, [[dict, lambda x, e: dict2sexp(x, e, "account")], [tuple, lambda x, e: [Sym("date")] + list(x)]])) print print #===================================================================== # tree def parse_node(sexp, env): name = sexp[1] data = {} children = [] for x in sexp[2:]: if x[0] == "node": children.append(parse_node(x, env)) else: data[x[0]] = x[1] return (name, data, children) o = parse(''' ;tree comments (node "C" (dist .2) (boot 70) (species "root") (node "A" (dist .1) (boot 100)) ; branch A (node "B" (dist .11) (boot 98) (node "Human1" (dist .01) (species "Human")) (node "Chimp2" (dist .03) (species "Chimp")))) ''') t = process(o, {"node": parse_node}) from pprint import pprint pprint(o) print t write_pretty(o) print
def test_dict2sexp(self): exp = dict2sexp({"aaa": 111, True: (((22, "abc", "adcd"), 9999), "www", (Sym("hello"), [], ("hi", 5555, "---") )), 78: False}) expected = [ Sym('dict'), [True, (((22, 'abc', 'adcd'), 9999), 'www', (Sym('hello'), [], ('hi', 5555, '---')))], ['aaa', 111], [78, False] ] self.assertEqual(exp, expected)
def test_dict2sexp(self): exp = dict2sexp({ "aaa": 111, True: (((22, "abc", "adcd"), 9999), "www", (Sym("hello"), [], ("hi", 5555, "---"))), 78: False }) expected = [ Sym('dict'), [ True, (((22, 'abc', 'adcd'), 9999), 'www', (Sym('hello'), [], ('hi', 5555, '---'))) ], ['aaa', 111], [78, False] ] self.assertEqual(exp, expected)
def test_eval2(self): o = parse( ''' (account (usename "raz") (started (date August 17 2005)) (renewed (date October 5 2009)) (score (+ 2 (* 3 7))) (score2 (quote (+ 2 (* 3 7)))) ) ''', { "account": sexp2dict, "date": lambda x, e: tuple(x[1:]), "+": lambda x, e: sum(map(lambda i: process(i, e), x[1:])), "*": lambda x, e: reduce(lambda a, b: a * b, map(lambda i: process(i, e), x[1:])), "quote": lambda x, e: x[1] }) expected = { Sym('score2'): [Sym('+'), 2, [Sym('*'), 3, 7]], Sym('usename'): 'raz', Sym('score'): 23, Sym('renewed'): (Sym('October'), 5, 2009), Sym('started'): (Sym('August'), 17, 2005) } # nopep8 self.assertEqual(o, expected) write_pretty( prepare(o, [[dict, lambda x, e: dict2sexp(x, e, "account")], [tuple, lambda x, e: [Sym("date")] + list(x)]])) print print #===================================================================== # tree def parse_node(sexp, env): name = sexp[1] data = {} children = [] for x in sexp[2:]: if x[0] == "node": children.append(parse_node(x, env)) else: data[x[0]] = x[1] return (name, data, children) o = parse(''' ;tree comments (node "C" (dist .2) (boot 70) (species "root") (node "A" (dist .1) (boot 100)) ; branch A (node "B" (dist .11) (boot 98) (node "Human1" (dist .01) (species "Human")) (node "Chimp2" (dist .03) (species "Chimp")))) ''') t = process(o, {"node": parse_node}) from pprint import pprint pprint(o) print t write_pretty(o) print