def test_dictionary_conversion(): """ Test converting dictionaries to tuples and ensuring proper key order. """ assert convert_to_hashable({}) == () expected = (('a', 1), ('b', 2)) assert convert_to_hashable({'a': 1, 'b': 2}) == expected assert convert_to_hashable({'b': 2, 'a': 1}) == expected assert convert_to_hashable(('a', [1, 2, 3, 4])) == ('a', (1, 2, 3, 4)) assert convert_to_hashable(('a', {'b': {3: [1, 2, 3, 4]}})) == ('a', (('b', ((3, (1, 2, 3, 4)), )), ))
def argument_hash(function, *args, **kwargs): # TODO: Allow usage of a different postprocessor than MD5. # TODO: Replace `hash` with a safe hashing implementation. arguments = inspect.getcallargs(function, *args, **kwargs) arguments = convert_to_hashable(arguments) return md5(':'.join(map(lambda value: str(hash(value)), arguments)))
def test_dictionary_conversion(): """ Test converting dictionaries to tuples and ensuring proper key order. """ assert convert_to_hashable({}) == () expected = (('a', 1), ('b', 2)) assert convert_to_hashable({'a': 1, 'b': 2}) == expected assert convert_to_hashable({'b': 2, 'a': 1}) == expected assert convert_to_hashable(('a', [1, 2, 3, 4])) == ('a', (1, 2, 3, 4)) assert convert_to_hashable(('a', { 'b': { 3: [1, 2, 3, 4] } })) == ('a', (('b', ((3, (1, 2, 3, 4)), )), ))