Exemple #1
0
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"})])]
Exemple #2
0
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")])
    ])]
Exemple #3
0
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
Exemple #4
0
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
Exemple #5
0
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
Exemple #6
0
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])
Exemple #7
0
 def exit(self):
     self.commit()
     self.result = HyDict(self.nodes)
Exemple #8
0
def empty_dict(p):
    return HyDict([])
Exemple #9
0
def t_dict(p):
    return HyDict(p[1])
Exemple #10
0
        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.
Exemple #11
0
 def exit(self):
     self.commit()
     it = iter(self.nodes)
     result = dict(zip(it, it))
     self.result = HyDict(result)
Exemple #12
0
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]