def test_serialize_basics(self):
        basic_list = ['a', 'b', 'c']
        basic_dict = {'a': 1, 'b': 2, 'c': 3}
        nested_list = [[0, 1, 2], [3, 4, 5]]
        nested_dict = {'a': {'a': 1, 'b': 2}, 'b': {'c': 3, 'd': 4}}
        list_of_nested_dicts = [{'a': {'a': 1, 'b': 2}, 'b': {'c': 3, 'd': 4}}]

        s_basic_list = serialize_item(basic_list)
        s_basic_args_list = serialize_args(basic_list)
        s_basic_dict = serialize_item(basic_dict)
        s_nested_list = serialize_item(nested_list)
        s_nested_args_list = serialize_args(nested_list)
        s_nested_dict = serialize_item(nested_dict)
        s_list_of_nested_dicts = serialize_item(list_of_nested_dicts)
        s_args_list_of_nested_dicts = serialize_args(list_of_nested_dicts)

        assert s_basic_list == str(['a', 'b', 'c'])
        assert s_basic_args_list == str(['a', 'b', 'c'])
        assert s_basic_dict == str(["['1', 'a']", "['2', 'b']", "['3', 'c']"])
        assert s_nested_list == str(["['0', '1', '2']", "['3', '4', '5']"])
        assert s_nested_args_list == str(
            ["['0', '1', '2']", "['3', '4', '5']"])
        assert s_nested_dict == str([
            '[\'["[\\\'1\\\', \\\'a\\\']", "[\\\'2\\\', \\\'b\\\']"]\', \'a\']',
            '[\'["[\\\'3\\\', \\\'c\\\']", "[\\\'4\\\', \\\'d\\\']"]\', \'b\']'
        ])
        assert s_list_of_nested_dicts == str([
            '[\'[\\\'["[\\\\\\\'1\\\\\\\', \\\\\\\'a\\\\\\\']", "[\\\\\\\'2\\\\\\\', \\\\\\\'b\\\\\\\']"]\\\', \\\'a\\\']\', \'[\\\'["[\\\\\\\'3\\\\\\\', \\\\\\\'c\\\\\\\']", "[\\\\\\\'4\\\\\\\', \\\\\\\'d\\\\\\\']"]\\\', \\\'b\\\']\']'
        ])
        assert s_args_list_of_nested_dicts == str([
            '[\'[\\\'["[\\\\\\\'1\\\\\\\', \\\\\\\'a\\\\\\\']", "[\\\\\\\'2\\\\\\\', \\\\\\\'b\\\\\\\']"]\\\', \\\'a\\\']\', \'[\\\'["[\\\\\\\'3\\\\\\\', \\\\\\\'c\\\\\\\']", "[\\\\\\\'4\\\\\\\', \\\\\\\'d\\\\\\\']"]\\\', \\\'b\\\']\']'
        ])
    def test_serialize_nested_lists_of_nested_lists(self):
        a = [[[1, 2, 3], [4, 5, 6]], [7, 8, 9]]
        b = [[7, 8, 9], [[4, 5, 6], [1, 2, 3]]]
        c = [[[6, 4, 5], [1, 3, 2]], [9, 8, 7]]

        s_a__item = serialize_item(a)
        s_b__item = serialize_item(b)
        s_c__item = serialize_item(c)

        assert s_a__item == s_b__item
        assert s_b__item == s_c__item
        assert s_c__item == str([
            '["[\'1\', \'2\', \'3\']", "[\'4\', \'5\', \'6\']"]',
            "['7', '8', '9']"
        ])

        s_a__args = serialize_args(a)
        s_b__args = serialize_args(b)
        s_c__args = serialize_args(c)

        assert s_a__args != s_b__args
        assert s_a__args == s_c__args
        assert s_b__args != s_c__args
        assert s_a__args == str([
            '["[\'1\', \'2\', \'3\']", "[\'4\', \'5\', \'6\']"]',
            "['7', '8', '9']"
        ])
        assert s_b__args == str([
            "['7', '8', '9']",
            '["[\'1\', \'2\', \'3\']", "[\'4\', \'5\', \'6\']"]'
        ])
        assert s_c__args == str([
            '["[\'1\', \'2\', \'3\']", "[\'4\', \'5\', \'6\']"]',
            "['7', '8', '9']"
        ])
    def test_serialize_nested_lists(self):
        a = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
        b = [[7, 8, 9], [4, 5, 6], [1, 2, 3]]
        c = [[6, 4, 5], [1, 3, 2], [9, 8, 7]]

        s_a__item = serialize_item(a)
        s_b__item = serialize_item(b)
        s_c__item = serialize_item(c)

        assert s_a__item == s_b__item
        assert s_b__item == s_c__item
        assert s_c__item == str(
            ["['1', '2', '3']", "['4', '5', '6']", "['7', '8', '9']"])

        s_a__args = serialize_args(a)
        s_b__args = serialize_args(b)
        s_c__args = serialize_args(c)

        assert s_a__args != s_b__args
        assert s_a__args != s_c__args
        assert s_b__args != s_c__args
        assert s_a__args == str(
            ["['1', '2', '3']", "['4', '5', '6']", "['7', '8', '9']"])
        assert s_b__args == str(
            ["['7', '8', '9']", "['4', '5', '6']", "['1', '2', '3']"])
        assert s_c__args == str(
            ["['4', '5', '6']", "['1', '2', '3']", "['7', '8', '9']"])
Example #4
0
    def test_serialize_methods(self):
        a = lambda *args, **kwargs: 'foo'
        def b():
            return 'bar'
        class C:
            def c(self):
                return 'biz'

        assert serialize_item(a) == '<lambda>'
        assert serialize_item(b) == 'b'
        assert serialize_item(C().c) == 'c'
        assert serialize_args([a]) == str(['<lambda>'])
        assert serialize_args([b]) == str(['b'])
        assert serialize_args([C().c, 1, '2', [3], {'four': 4}]) == str(['c', '1', '2', "['3']", '["[\'4\', \'four\']"]'])
Example #5
0
    def test_serialize_iterables(self):
        target_set = set([5, 3, 4, 2, 7, 6, 1, 8, 9, 0])
        def gen():
            for i in range(10):
                yield i
        target_generator = gen()
        target_frozenset = frozenset([5, 3, 4, 2, 7, 6, 1, 8, 9, 0])

        s_set = serialize_args(target_set)
        s_gen = serialize_args(target_generator)
        s_frozenset = serialize_args(target_frozenset)

        assert s_set == s_gen
        assert s_gen == s_frozenset
        assert s_frozenset == str(['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'])
Example #6
0
    def test_serialize_models(self):
        a = TestModel('a', 'b')
        b = [TestModel('a', 'b'), TestModel('b', 'c'), TestModel('c', 'd')]
        c = {'c': TestModel('a', 'b'), 'b': TestModel('b', 'c'), 'a': TestModel('c', 'd')}
        d = set([TestModel('a', 'b'), TestModel('b', 'c'), TestModel('c', 'd')])

        s_a = serialize_item(a)
        s_b = serialize_args(b)
        s_c = serialize_item(c)
        s_d = serialize_args(d)

        assert s_a == 'TestModel'
        assert s_b == str(['TestModel', 'TestModel', 'TestModel'])
        assert s_c == str(["['TestModel', 'a']", "['TestModel', 'b']", "['TestModel', 'c']"])
        assert s_d == str(['TestModel', 'TestModel', 'TestModel'])
Example #7
0
    def test_serialize_models(self):
        a = TestModel("a", "b")
        b = [TestModel("a", "b"), TestModel("b", "c"), TestModel("c", "d")]
        c = {"c": TestModel("a", "b"), "b": TestModel("b", "c"), "a": TestModel("c", "d")}
        d = set([TestModel("a", "b"), TestModel("b", "c"), TestModel("c", "d")])

        s_a = serialize_item(a)
        s_b = serialize_args(b)
        s_c = serialize_item(c)
        s_d = serialize_args(d)

        assert s_a == "TestModel"
        assert s_b == str(["TestModel", "TestModel", "TestModel"])
        assert s_c == str(["['TestModel', 'a']", "['TestModel', 'b']", "['TestModel', 'c']"])
        assert s_d == str(["TestModel", "TestModel", "TestModel"])
Example #8
0
    def test_serialize_methods(self):
        a = lambda *args, **kwargs: "foo"

        def b():
            return "bar"

        class C:
            def c(self):
                return "biz"

        assert serialize_item(a) == "<lambda>"
        assert serialize_item(b) == "b"
        assert serialize_item(C().c) == "c"
        assert serialize_args([a]) == str(["<lambda>"])
        assert serialize_args([b]) == str(["b"])
        assert serialize_args([C().c, 1, "2", [3], {"four": 4}]) == str(["c", "1", "2", "['3']", "[\"['4', 'four']\"]"])
Example #9
0
    def test_serialize_iterables(self):
        target_set = set([5, 3, 4, 2, 7, 6, 1, 8, 9, 0])

        def gen():
            for i in range(10):
                yield i

        target_generator = gen()
        target_frozenset = frozenset([5, 3, 4, 2, 7, 6, 1, 8, 9, 0])

        s_set = serialize_args(target_set)
        s_gen = serialize_args(target_generator)
        s_frozenset = serialize_args(target_frozenset)

        assert s_set == s_gen
        assert s_gen == s_frozenset
        assert s_frozenset == str(["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"])
    def test_serialize_methods(self):
        a = lambda *args, **kwargs: 'foo'

        def b():
            return 'bar'

        class C:
            def c(self):
                return 'biz'

        assert serialize_item(a) == '<lambda>'
        assert serialize_item(b) == 'b'
        assert serialize_item(C().c) == 'c'
        assert serialize_args([a]) == str(['<lambda>'])
        assert serialize_args([b]) == str(['b'])
        assert serialize_args([C().c, 1, '2', [3], {
            'four': 4
        }]) == str(['c', '1', '2', "['3']", '["[\'4\', \'four\']"]'])
    def test_serialize_iterables(self):
        target_set = set([5, 3, 4, 2, 7, 6, 1, 8, 9, 0])

        def gen():
            for i in range(10):
                yield i

        target_generator = gen()
        target_frozenset = frozenset([5, 3, 4, 2, 7, 6, 1, 8, 9, 0])

        s_set = serialize_args(target_set)
        s_gen = serialize_args(target_generator)
        s_frozenset = serialize_args(target_frozenset)

        assert s_set == s_gen
        assert s_gen == s_frozenset
        assert s_frozenset == str(
            ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'])
Example #12
0
    def test_serialize_nested_lists_of_nested_lists(self):
        a = [[[1, 2, 3], [4, 5, 6]], [7, 8, 9]]
        b = [[7, 8, 9], [[4, 5, 6], [1, 2, 3]]]
        c = [[[6, 4, 5], [1, 3, 2]], [9, 8, 7]]

        s_a__item = serialize_item(a)
        s_b__item = serialize_item(b)
        s_c__item = serialize_item(c)

        assert s_a__item == s_b__item
        assert s_b__item == s_c__item
        assert s_c__item == str(['["[\'1\', \'2\', \'3\']", "[\'4\', \'5\', \'6\']"]', "['7', '8', '9']"])

        s_a__args = serialize_args(a)
        s_b__args = serialize_args(b)
        s_c__args = serialize_args(c)

        assert s_a__args != s_b__args
        assert s_a__args == s_c__args
        assert s_b__args != s_c__args
        assert s_a__args == str(['["[\'1\', \'2\', \'3\']", "[\'4\', \'5\', \'6\']"]', "['7', '8', '9']"])
        assert s_b__args == str(["['7', '8', '9']", '["[\'1\', \'2\', \'3\']", "[\'4\', \'5\', \'6\']"]'])
        assert s_c__args == str(['["[\'1\', \'2\', \'3\']", "[\'4\', \'5\', \'6\']"]', "['7', '8', '9']"])
Example #13
0
    def test_serialize_nested_lists(self):
        a = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
        b = [[7, 8, 9], [4, 5, 6], [1, 2, 3]]
        c = [[6, 4, 5], [1, 3, 2], [9, 8, 7]]

        s_a__item = serialize_item(a)
        s_b__item = serialize_item(b)
        s_c__item = serialize_item(c)

        assert s_a__item == s_b__item
        assert s_b__item == s_c__item
        assert s_c__item == str(["['1', '2', '3']", "['4', '5', '6']", "['7', '8', '9']"])

        s_a__args = serialize_args(a)
        s_b__args = serialize_args(b)
        s_c__args = serialize_args(c)

        assert s_a__args != s_b__args
        assert s_a__args != s_c__args
        assert s_b__args != s_c__args
        assert s_a__args == str(["['1', '2', '3']", "['4', '5', '6']", "['7', '8', '9']"])
        assert s_b__args == str(["['7', '8', '9']", "['4', '5', '6']", "['1', '2', '3']"])
        assert s_c__args == str(["['4', '5', '6']", "['1', '2', '3']", "['7', '8', '9']"])
    def test_serialize_models(self):
        a = TestModel('a', 'b')
        b = [TestModel('a', 'b'), TestModel('b', 'c'), TestModel('c', 'd')]
        c = {
            'c': TestModel('a', 'b'),
            'b': TestModel('b', 'c'),
            'a': TestModel('c', 'd')
        }
        d = set(
            [TestModel('a', 'b'),
             TestModel('b', 'c'),
             TestModel('c', 'd')])

        s_a = serialize_item(a)
        s_b = serialize_args(b)
        s_c = serialize_item(c)
        s_d = serialize_args(d)

        assert s_a == 'TestModel'
        assert s_b == str(['TestModel', 'TestModel', 'TestModel'])
        assert s_c == str(
            ["['TestModel', 'a']", "['TestModel', 'b']", "['TestModel', 'c']"])
        assert s_d == str(['TestModel', 'TestModel', 'TestModel'])
Example #15
0
    def test_serialize_basics(self):
        basic_list = ["a", "b", "c"]
        basic_dict = {"a": 1, "b": 2, "c": 3}
        nested_list = [[0, 1, 2], [3, 4, 5]]
        nested_dict = {"a": {"a": 1, "b": 2}, "b": {"c": 3, "d": 4}}
        list_of_nested_dicts = [{"a": {"a": 1, "b": 2}, "b": {"c": 3, "d": 4}}]

        s_basic_list = serialize_item(basic_list)
        s_basic_args_list = serialize_args(basic_list)
        s_basic_dict = serialize_item(basic_dict)
        s_nested_list = serialize_item(nested_list)
        s_nested_args_list = serialize_args(nested_list)
        s_nested_dict = serialize_item(nested_dict)
        s_list_of_nested_dicts = serialize_item(list_of_nested_dicts)
        s_args_list_of_nested_dicts = serialize_args(list_of_nested_dicts)

        assert s_basic_list == str(["a", "b", "c"])
        assert s_basic_args_list == str(["a", "b", "c"])
        assert s_basic_dict == str(["['1', 'a']", "['2', 'b']", "['3', 'c']"])
        assert s_nested_list == str(["['0', '1', '2']", "['3', '4', '5']"])
        assert s_nested_args_list == str(["['0', '1', '2']", "['3', '4', '5']"])
        assert s_nested_dict == str(
            [
                "['[\"[\\'1\\', \\'a\\']\", \"[\\'2\\', \\'b\\']\"]', 'a']",
                "['[\"[\\'3\\', \\'c\\']\", \"[\\'4\\', \\'d\\']\"]', 'b']",
            ]
        )
        assert s_list_of_nested_dicts == str(
            [
                "['[\\'[\"[\\\\\\'1\\\\\\', \\\\\\'a\\\\\\']\", \"[\\\\\\'2\\\\\\', \\\\\\'b\\\\\\']\"]\\', \\'a\\']', '[\\'[\"[\\\\\\'3\\\\\\', \\\\\\'c\\\\\\']\", \"[\\\\\\'4\\\\\\', \\\\\\'d\\\\\\']\"]\\', \\'b\\']']"
            ]
        )
        assert s_args_list_of_nested_dicts == str(
            [
                "['[\\'[\"[\\\\\\'1\\\\\\', \\\\\\'a\\\\\\']\", \"[\\\\\\'2\\\\\\', \\\\\\'b\\\\\\']\"]\\', \\'a\\']', '[\\'[\"[\\\\\\'3\\\\\\', \\\\\\'c\\\\\\']\", \"[\\\\\\'4\\\\\\', \\\\\\'d\\\\\\']\"]\\', \\'b\\']']"
            ]
        )
Example #16
0
    def test_serialize_basics(self):
        basic_list = [ 'a', 'b', 'c' ]
        basic_dict = { 'a': 1, 'b': 2, 'c': 3 }
        nested_list = [ [ 0, 1, 2 ], [ 3, 4, 5 ] ]
        nested_dict = { 'a': { 'a': 1, 'b': 2 }, 'b': { 'c': 3, 'd': 4 } }
        list_of_nested_dicts = [ { 'a': { 'a': 1, 'b': 2 }, 'b': { 'c': 3, 'd': 4 } } ]

        s_basic_list = serialize_item(basic_list)
        s_basic_args_list = serialize_args(basic_list)
        s_basic_dict = serialize_item(basic_dict)
        s_nested_list = serialize_item(nested_list)
        s_nested_args_list = serialize_args(nested_list)
        s_nested_dict = serialize_item(nested_dict)
        s_list_of_nested_dicts = serialize_item(list_of_nested_dicts)
        s_args_list_of_nested_dicts = serialize_args(list_of_nested_dicts)

        assert s_basic_list == str(['a', 'b', 'c'])
        assert s_basic_args_list == str(['a', 'b', 'c'])
        assert s_basic_dict == str(["['1', 'a']", "['2', 'b']", "['3', 'c']"])
        assert s_nested_list == str(["['0', '1', '2']", "['3', '4', '5']"])
        assert s_nested_args_list == str(["['0', '1', '2']", "['3', '4', '5']"])
        assert s_nested_dict == str(['[\'["[\\\'1\\\', \\\'a\\\']", "[\\\'2\\\', \\\'b\\\']"]\', \'a\']', '[\'["[\\\'3\\\', \\\'c\\\']", "[\\\'4\\\', \\\'d\\\']"]\', \'b\']'])
        assert s_list_of_nested_dicts == str(['[\'[\\\'["[\\\\\\\'1\\\\\\\', \\\\\\\'a\\\\\\\']", "[\\\\\\\'2\\\\\\\', \\\\\\\'b\\\\\\\']"]\\\', \\\'a\\\']\', \'[\\\'["[\\\\\\\'3\\\\\\\', \\\\\\\'c\\\\\\\']", "[\\\\\\\'4\\\\\\\', \\\\\\\'d\\\\\\\']"]\\\', \\\'b\\\']\']'])
        assert s_args_list_of_nested_dicts == str(['[\'[\\\'["[\\\\\\\'1\\\\\\\', \\\\\\\'a\\\\\\\']", "[\\\\\\\'2\\\\\\\', \\\\\\\'b\\\\\\\']"]\\\', \\\'a\\\']\', \'[\\\'["[\\\\\\\'3\\\\\\\', \\\\\\\'c\\\\\\\']", "[\\\\\\\'4\\\\\\\', \\\\\\\'d\\\\\\\']"]\\\', \\\'b\\\']\']'])
Example #17
0
def get_hash(args, trace_string, kwargs, ignore=UNDEFINED):
    counter_value = counter.get_from_trace_for_cache(trace_string)

    if ignore != UNDEFINED:
        args = list(args)
        for i in ignore.args:
            args[i] = None
        for k in ignore.kwargs:
            kwargs[k] = None
        args = tuple(args)

    return sha1((str(util.serialize_args(args)) + "\n" +
                              str(counter_value) + "\n" +
                              str(util.serialize_item(kwargs)) + "\n" +
                              trace_string + "\n" )).hexdigest()
Example #18
0
def get_hash(args, trace_string, kwargs, ignore=UNDEFINED):
    counter_value = counter.get_from_trace_for_cache(trace_string)

    if ignore != UNDEFINED:
        args = list(args)
        for i in ignore.args:
            args[i] = None
        for k in ignore.kwargs:
            kwargs[k] = None
        args = tuple(args)

    return sha1((str(util.serialize_args(args)) + "\n" +
                              str(counter_value) + "\n" +
                              str(util.serialize_item(kwargs)) + "\n" +
                              trace_string + "\n" )).hexdigest()
Example #19
0
def get_hash(args, trace_string, kwargs, ignore=UNDEFINED):
    counter_value = counter.get_from_trace_for_cache(trace_string)

    args_with_ignores = [] 
    kwargs_with_ignores = {}
    if ignore != UNDEFINED:
        for i, arg in enumerate(args):
            args_with_ignores.append(None if i in ignore.args else args[i])
        for k, v in kwargs.iteritems():
            kwargs_with_ignores[k] = None if k in ignore.kwargs else v

        args_with_ignores = tuple(args_with_ignores)
    else:
        args_with_ignores = args
        kwargs_with_ignores = kwargs

    return sha1((str(util.serialize_args(args_with_ignores)) + "\n" +
                              str(counter_value) + "\n" +
                              str(util.serialize_item(kwargs_with_ignores)) + "\n" +
                              trace_string + "\n" )).hexdigest()
Example #20
0
def get_hash(args, trace_string, kwargs, ignore=UNDEFINED):
    counter_value = counter.get_from_trace_for_cache(trace_string)

    args_with_ignores = []
    kwargs_with_ignores = {}
    if ignore != UNDEFINED:
        for i, arg in enumerate(args):
            args_with_ignores.append(None if i in ignore.args else args[i])
        for k, v in kwargs.iteritems():
            kwargs_with_ignores[k] = None if k in ignore.kwargs else v

        args_with_ignores = tuple(args_with_ignores)
    else:
        args_with_ignores = args
        kwargs_with_ignores = kwargs

    return sha1((str(util.serialize_args(args_with_ignores)) + "\n" +
                 str(counter_value) + "\n" +
                 str(util.serialize_item(kwargs_with_ignores)) + "\n" +
                 trace_string + "\n")).hexdigest()