def test_dicts(): """ Ensure that we can tokenize a dict. """ objs = tokenize("{foo bar bar baz}") assert objs == [HyDict({ "foo": "bar", "bar": "baz" })] objs = tokenize("(bar {foo bar bar baz})") assert objs == [HyExpression([HySymbol("bar"), HyDict({"foo": "bar", "bar": "baz"})])]
def test_dicts(): """ Ensure that we can tokenize a dict. """ objs = tokenize("{foo bar bar baz}") assert objs == [HyDict(["foo", "bar", "bar", "baz"])] objs = tokenize("(bar {foo bar bar baz})") assert objs == [HyExpression([HySymbol("bar"), HyDict(["foo", "bar", "bar", "baz"])])] objs = tokenize("{(foo bar) (baz quux)}") assert objs == [HyDict([ HyExpression([HySymbol("foo"), HySymbol("bar")]), HyExpression([HySymbol("baz"), HySymbol("quux")]) ])]
def process(tree): if isinstance(tree, HyExpression): fn = tree[0] ntree = HyExpression([fn] + [process(x) for x in tree[1:]]) ntree.replace(tree) if isinstance(fn, HyString): if fn in _hy_macros: m = _hy_macros[fn] obj = m(ntree) obj.replace(tree) return obj ntree.replace(tree) return ntree if isinstance(tree, HyDict): obj = HyDict(dict((process(x), process(tree[x])) for x in tree)) obj.replace(tree) return obj if isinstance(tree, HyList): obj = HyList([process(x) for x in tree]) # NOQA # flake8 thinks we're redefining from 52. obj.replace(tree) return obj if isinstance(tree, list): return [process(x) for x in tree] return tree
def test_cons_list(): """Check that cons of something and a list gets tokenized as a list""" entry = tokenize("(a . [])")[0] assert entry == HyList([HySymbol("a")]) assert type(entry) == HyList entry = tokenize("(a . ())")[0] assert entry == HyExpression([HySymbol("a")]) assert type(entry) == HyExpression entry = tokenize("(a b . {})")[0] assert entry == HyDict([HySymbol("a"), HySymbol("b")]) assert type(entry) == HyDict
def router(tree, rkwargs=None): tree = HyExpression(tree) name = tree.pop(0) path = tree.pop(0) tree.insert(0, HySymbol("fn")) tree = HyExpression([HySymbol("def"), name, tree]) route = HyExpression([HySymbol(".route"), HySymbol("app"), path]) if rkwargs: route = HyExpression([ HySymbol("kwapply"), route, HyDict({HyString("methods"): rkwargs}) ]) return HyExpression([HySymbol("with_decorator"), route, tree])
def exit(self): self.commit() self.result = HyDict(self.nodes)
def empty_dict(p): return HyDict([])
def t_dict(p): return HyDict(p[1])
refs[name] = macro readers = _hy_reader[source_module] reader_refs = _hy_reader[target_module] for name, reader in readers.items(): reader_refs[name] = reader # type -> wrapping function mapping for _wrap_value _wrappers = { int: HyInteger, bool: lambda x: HySymbol("True") if x else HySymbol("False"), float: HyFloat, complex: HyComplex, str_type: HyString, dict: lambda d: HyDict(_wrap_value(x) for x in sum(d.items(), ())), list: lambda l: HyList(_wrap_value(x) for x in l), tuple: lambda t: HyList(_wrap_value(x) for x in t), type(None): lambda foo: HySymbol("None"), HyExpression: lambda e: HyExpression(_wrap_value(x) for x in e), } if sys.version_info[0] < 3: # do not add long on python3 _wrappers[long_type] = HyInteger def _wrap_value(x): """Wrap `x` into the corresponding Hy type. This allows a macro to return an unquoted expression transparently.
def exit(self): self.commit() it = iter(self.nodes) result = dict(zip(it, it)) self.result = HyDict(result)
from hy.models.dict import HyDict hydict = HyDict(["a", 1, "b", 2, "c", 3]) def test_dict_items(): assert hydict.items() == [("a", 1), ("b", 2), ("c", 3)] def test_dict_keys(): assert hydict.keys() == ["a", "b", "c"] def test_dict_values(): assert hydict.values() == [1, 2, 3]