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_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_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_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_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"])
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'])
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_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_dicts(self): a = {'a': 1, 'b': 2, 'c': 3} b = {'c': 3, 'a': 1, 'b': 2} c = {'c': 3, 'b': 2, 'a': 1} d = {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5, 'f': 6, 'g': 7, 'h': 8} e = {'b': 2, 'a': 1, 'h': 8, 'd': 4, 'e': 5, 'f': 6, 'g': 7, 'c': 3} f = {'e': 5, 'a': 1, 'h': 8, 'd': 4, 'b': 2, 'f': 6, 'g': 7, 'c': 3} s_a = serialize_item(a) s_b = serialize_item(b) s_c = serialize_item(c) s_d = serialize_item(d) s_e = serialize_item(e) s_f = serialize_item(f) assert s_a == s_b assert s_b == s_c assert s_c == str(["['1', 'a']", "['2', 'b']", "['3', 'c']"]) assert s_d == s_e assert s_e == s_f assert s_f == str([ "['1', 'a']", "['2', 'b']", "['3', 'c']", "['4', 'd']", "['5', 'e']", "['6', 'f']", "['7', 'g']", "['8', 'h']" ])
def test_serialize_dicts(self): a = {"a": 1, "b": 2, "c": 3} b = {"c": 3, "a": 1, "b": 2} c = {"c": 3, "b": 2, "a": 1} d = {"a": 1, "b": 2, "c": 3, "d": 4, "e": 5, "f": 6, "g": 7, "h": 8} e = {"b": 2, "a": 1, "h": 8, "d": 4, "e": 5, "f": 6, "g": 7, "c": 3} f = {"e": 5, "a": 1, "h": 8, "d": 4, "b": 2, "f": 6, "g": 7, "c": 3} s_a = serialize_item(a) s_b = serialize_item(b) s_c = serialize_item(c) s_d = serialize_item(d) s_e = serialize_item(e) s_f = serialize_item(f) assert s_a == s_b assert s_b == s_c assert s_c == str(["['1', 'a']", "['2', 'b']", "['3', 'c']"]) assert s_d == s_e assert s_e == s_f assert s_f == str( [ "['1', 'a']", "['2', 'b']", "['3', 'c']", "['4', 'd']", "['5', 'e']", "['6', 'f']", "['7', 'g']", "['8', 'h']", ] )
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_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_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'])
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 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()
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 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()
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()
def test_serialize_dicts(self): a = {'a': 1, 'b': 2, 'c': 3} b = {'c': 3, 'a': 1, 'b': 2} c = {'c': 3, 'b': 2, 'a': 1} d = {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5, 'f': 6, 'g': 7, 'h': 8} e = {'b': 2, 'a': 1, 'h': 8, 'd': 4, 'e': 5, 'f': 6, 'g': 7, 'c': 3} f = {'e': 5, 'a': 1, 'h': 8, 'd': 4, 'b': 2, 'f': 6, 'g': 7, 'c': 3} s_a = serialize_item(a) s_b = serialize_item(b) s_c = serialize_item(c) s_d = serialize_item(d) s_e = serialize_item(e) s_f = serialize_item(f) assert s_a == s_b assert s_b == s_c assert s_c == str(["['1', 'a']", "['2', 'b']", "['3', 'c']"]) assert s_d == s_e assert s_e == s_f assert s_f == str(["['1', 'a']", "['2', 'b']", "['3', 'c']", "['4', 'd']", "['5', 'e']", "['6', 'f']", "['7', 'g']", "['8', 'h']"])
def cache(handle=lambda *args, **kwargs: None, args=UNDEFINED, kwargs=UNDEFINED, ignore=UNDEFINED, call_stack=UNDEFINED, callback=UNDEFINED, subsequent_rvalue=UNDEFINED): """ Store a call descriptor :param lambda handle: Any callable will work here. The method to cache. :param tuple args: The arguments to the method. :param dict kwargs: The keyword arguments to the method. :param tuple(list(int), list(str)) ignore: A tuple of arguments to ignore. The first element should be a list of positional arguments. The second should be a list of keys for keyword arguments. :param caliendo.hooks.CallStack call_stack: The stack of calls thus far for this patch. :param function callback: The callback function to execute each time there is a cache hit for 'handle' (actually mechanism is more complicated, but this is what it boils down to) :param mixed subsequent_rvalue: If passed; this will be the return value each time this method is run regardless of what is returned when it is initially cached. Caching for this method will be skipped. This is useful when the method returns something unpickleable but we still need to stub it out. :returns: The value of handle(*args, **kwargs) """ if args == UNDEFINED: args = tuple() if kwargs == UNDEFINED: kwargs = {} if not USE_CALIENDO: return handle(*args, **kwargs) trace_string = util.get_stack(handle.__name__) call_hash = get_hash(args, trace_string, kwargs, ignore) cd = call_descriptor.fetch(call_hash) modify_or_replace = 'no' util.set_current_hash(call_hash) if config.CALIENDO_PROMPT: display_name = ("(test %s): " % caliendo.util.current_test) if caliendo.util.current_test else '' if hasattr(handle, '__module__') and hasattr(handle, '__name__'): display_name += "%s.%s" % (handle.__module__, handle.__name__) else: display_name += handle if cd: modify_or_replace = prompt.should_modify_or_replace_cached(display_name) if not cd or modify_or_replace == 'replace': returnval = handle(*args, **kwargs) elif cd and modify_or_replace == 'modify': returnval = prompt.modify_cached_value(cd.returnval, calling_method=display_name, calling_test='') if cd and subsequent_rvalue != UNDEFINED: return subsequent_rvalue elif subsequent_rvalue != UNDEFINED: original_rvalue = returnval returnval = subsequent_rvalue if not cd or modify_or_replace != 'no': if isinstance(handle, types.MethodType): args = list(args) args[0] = util.serialize_item(args[0]) args = tuple(args) cd = call_descriptor.CallDescriptor( hash = call_hash, stack = trace_string, method = handle.__name__, returnval = returnval, args = args, kwargs = kwargs ) cd.save() util.set_last_hash(cd.hash) if call_stack != UNDEFINED: call_stack.add(cd) if callback != UNDEFINED: call_stack.add_hook(Hook(call_descriptor_hash=cd.hash, callback=callback)) if subsequent_rvalue == UNDEFINED: return cd.returnval else: return original_rvalue
def cache(handle=lambda *args, **kwargs: None, args=UNDEFINED, kwargs=UNDEFINED, ignore=UNDEFINED, call_stack=UNDEFINED, callback=UNDEFINED, subsequent_rvalue=UNDEFINED): """ Store a call descriptor :param lambda handle: Any callable will work here. The method to cache. :param tuple args: The arguments to the method. :param dict kwargs: The keyword arguments to the method. :param tuple(list(int), list(str)) ignore: A tuple of arguments to ignore. The first element should be a list of positional arguments. The second should be a list of keys for keyword arguments. :param caliendo.hooks.CallStack call_stack: The stack of calls thus far for this patch. :param function callback: The callback function to execute each time there is a cache hit for 'handle' (actually mechanism is more complicated, but this is what it boils down to) :param mixed subsequent_rvalue: If passed; this will be the return value each time this method is run regardless of what is returned when it is initially cached. Caching for this method will be skipped. This is useful when the method returns something unpickleable but we still need to stub it out. :returns: The value of handle(*args, **kwargs) """ if args == UNDEFINED: args = tuple() if kwargs == UNDEFINED: kwargs = {} if not USE_CALIENDO: return handle(*args, **kwargs) filtered_args = ignore.filter_args(args) if ignore is not UNDEFINED else args filtered_kwargs = ignore.filter_kwargs(kwargs) if ignore is not UNDEFINED else args trace_string = util.get_stack(handle.__name__) call_hash = get_hash(filtered_args, trace_string, filtered_kwargs, ignore) cd = call_descriptor.fetch(call_hash) modify_or_replace = 'no' util.set_current_hash(call_hash) if config.CALIENDO_PROMPT: display_name = ("(test %s): " % caliendo.util.current_test) if caliendo.util.current_test else '' if hasattr(handle, '__module__') and hasattr(handle, '__name__'): display_name += "%s.%s" % (handle.__module__, handle.__name__) else: display_name += handle if cd: modify_or_replace = prompt.should_modify_or_replace_cached(display_name) if not cd or modify_or_replace == 'replace': returnval = handle(*args, **kwargs) elif cd and modify_or_replace == 'modify': returnval = prompt.modify_cached_value(cd.returnval, calling_method=display_name, calling_test='') if cd and subsequent_rvalue != UNDEFINED: return subsequent_rvalue elif subsequent_rvalue != UNDEFINED: original_rvalue = returnval returnval = subsequent_rvalue if not cd or modify_or_replace != 'no': if isinstance(handle, types.MethodType): filtered_args = list(filtered_args) filtered_args[0] = util.serialize_item(filtered_args[0]) filtered_args = tuple(filtered_args) cd = call_descriptor.CallDescriptor( hash = call_hash, stack = trace_string, method = handle.__name__, returnval = returnval, args = filtered_args, kwargs = filtered_kwargs ) cd.save() util.set_last_hash(cd.hash) if call_stack != UNDEFINED: call_stack.add(cd) if callback != UNDEFINED: call_stack.add_hook(Hook(call_descriptor_hash=cd.hash, callback=callback)) if subsequent_rvalue == UNDEFINED: return cd.returnval else: return original_rvalue
def cache(handle=lambda *args, **kwargs: None, args=UNDEFINED, kwargs=UNDEFINED, ignore=UNDEFINED, call_stack=UNDEFINED, callback=UNDEFINED): """ Store a call descriptor :param lambda handle: Any callable will work here. The method to cache. :param tuple args: The arguments to the method. :param dict kwargs: The keyword arguments to the method. :param tuple(list(int), list(str)) ignore: A tuple of arguments to ignore. The first element should be a list of positional arguments. The second should be a list of keys for keyword arguments. :param caliendo.hooks.CallStack call_stack: The stack of calls thus far for this patch. :param function callback: The callback function to execute each time there is a cache hit for 'handle' (actually mechanism is more complicated, but this is what it boils down to) :returns: The value of handle(*args, **kwargs) """ if args == UNDEFINED: args = tuple() if kwargs == UNDEFINED: kwargs = {} if not USE_CALIENDO: return handle(*args, **kwargs) trace_string = util.get_stack(handle.__name__) call_hash = get_hash(args, trace_string, kwargs, ignore) cd = call_descriptor.fetch(call_hash) modify_or_replace = 'no' util.set_current_hash(call_hash) if config.CALIENDO_PROMPT: display_name = ("(test %s): " % caliendo.util.current_test) if caliendo.util.current_test else '' if hasattr(handle, '__module__') and hasattr(handle, '__name__'): display_name += "%s.%s" % (handle.__module__, handle.__name__) else: display_name += handle if cd: modify_or_replace = prompt.should_modify_or_replace_cached(display_name) if not cd or modify_or_replace == 'replace': returnval = handle(*args, **kwargs) elif cd and modify_or_replace == 'modify': returnval = prompt.modify_cached_value(cd.returnval, calling_method=display_name, calling_test='') if not cd or modify_or_replace != 'no': if isinstance(handle, types.MethodType): args = list(args) args[0] = util.serialize_item(args[0]) args = tuple(args) cd = call_descriptor.CallDescriptor( hash = call_hash, stack = trace_string, method = handle.__name__, returnval = returnval, args = args, kwargs = kwargs ) cd.save() util.set_last_hash(cd.hash) if call_stack != UNDEFINED: call_stack.add(cd) if callback != UNDEFINED: call_stack.add_hook(Hook(call_descriptor_hash=cd.hash, callback=callback)) return cd.returnval