示例#1
0
def test_with_items():
    assert AttrDict({"foo": "bar"}) == {"foo": "bar"}

    assert AttrDict((("foo", "bar"), )) == {"foo": "bar"}

    assert AttrDict(foo="bar") == {"foo": "bar"}

    assert AttrDict({"alpha": "bravo"}, foo="bar") == {
        "foo": "bar",
        "alpha": "bravo"
    }
示例#2
0
def test_attribute_access():
    """
    Items can be accessed as attributes
    """
    ad = AttrDict()
    ad["x"] = 1

    assert ad.x == 1

    assert "x" in ad

    ad._y = 2

    assert ad["_y"] == 2
示例#3
0
def test_copy():
    """
    Make a dict copy of an AttrDict.
    """
    dict_a = AttrDict({"foo": {"bar": "baz"}})

    dict_b = dict_a.copy()

    assert dict_a == dict_b

    dict_c = dict_b

    dict_b.foo.lorem = "ipsum"

    assert dict_b == dict_c
示例#4
0
    def results_iter(self, response, **kwargs):
        results = response.json()

        if type(results) is dict:
            if 'result' in results:
                results = results.get('result')

        for result in results:
            yield AttrDict(result)
示例#5
0
    def results_iter(self, response, **kwargs):
        results = response.json()

        if type(results) is dict:
            if 'result' in results:
                results = results.get('result')
                # if type(results) is dict:
                #     results = [dict({'_key': k}, **v) for (k, v) in results.items()]
            # else:
            #     results = list(results.items())

        for result in results:

            if type(result) is tuple:
                result[1].update({'_key': result[0]})
                yield AttrDict(result[1])
            else:
                yield AttrDict(result)
示例#6
0
    def results_iter(self, response, **kwargs):
        results = response.json()
        result_data = []

        if type(results) is dict:
            if 'result' in results:
                results = results.get('result')
                if type(results) is dict:
                    yield AttrDict(results)
                else:
                    result_data = results
            else:
                result_data = list(results.items())

        for result in result_data:

            if type(result) is tuple:
                result[1].update({'_key': result[0]})
                yield AttrDict(result[1])
            else:
                yield AttrDict(result)
示例#7
0
def test_fromkeys():
    """
    make a new sequence from a set of keys.
    """

    # default value
    assert AttrDict.fromkeys(()) == {}

    assert AttrDict.fromkeys({
        "foo": "bar",
        "baz": "qux"
    }) == {
        "foo": None,
        "baz": None
    }

    assert AttrDict.fromkeys(("foo", "baz")) == {"foo": None, "baz": None}

    # custom value
    assert AttrDict.fromkeys((), 0) == {}

    assert AttrDict.fromkeys({
        "foo": "bar",
        "baz": "qux"
    }, 0) == {
        "foo": 0,
        "baz": 0
    }

    assert AttrDict.fromkeys(("foo", "baz"), 0) == {"foo": 0, "baz": 0}
示例#8
0
def test_tree():
    tree = AttrDict({"x": {"y": {"z": 1}}})

    assert tree.x.y.z == 1
示例#9
0
def test_empty():
    assert AttrDict() == {}

    assert AttrDict(()) == {}

    assert AttrDict({}) == {}