freeze Documentation Release 0.7.0alpha Jean-Louis Fuchs April 10, 2014
Contents i
ii
freeze.freeze(data_structure) Freeze tries to convert any data-structure in a hierarchy of tuples. freeze.object_to_items(data_structure) Converts a object to a items list respecting also slots. freeze.dump(data_structure) Dump will create a human readable version of your data-structure. freeze.tree_diff_assert(a, b[, n, sort]) User tree_diff() to assert a equals b. freeze.stable_hash(data_structure) Stable hash does: hash(recursive_sort(freeze(data_structure))) freeze.recursive_hash(data_structure) Recursive hash does: hash(freeze(data_structure)) freeze.tree_diff(a, b[, n, sort]) Dump any data-structure or object, traverse it depth-first in-order and appl freeze.vformat(*args, **kwargs) A pformat wrapper that produces narrow representations of data-structure freeze.transparent_repr(string) The result is repr transparent. freeze.traverse_frozen_data(data_structure) Yields the leaves of the frozen data-structure pre-order. freeze.traversalbasedreprcompare(payload) Implements the comparison method for frozen data-structures based on tra Freeze the state of data-structures and objects for data-analysis or testing (diffing data-structures). Frozen datastructures consist of only tuples and these are comparable/sortable/hashable. The freeze() function can be used for many purposes for example implement hash () for your complex object very fast. dump() is intended for testing and analysis. Authors: Jean-Louis Fuchs <ganwell@fangorn.ch> https://github.com/ganwell Run doctests with python -m freeze freeze.freeze(data_structure) Freeze tries to convert any data-structure in a hierarchy of tuples. Tuples are comparable/sortable/hashable, you can use this with with recursive_sort(). freeze has no recursion detection. Parameters data_structure The structure to convert. >>> recursive_sort(freeze(_testclass())) (( a, huhu ), ( sub, (( a, slot ), ( b, (1, (1, 2, 3), 2, 3))))) >>> dump((none, (None, None))) (None, (None, None)) freeze.object_to_items(data_structure) Converts a object to a items list respecting also slots. Use dict(object_to_items(obj)) to get a dictionary. freeze.dump(data_structure) Dump will create a human readable version of your data-structure. It will try to dump almost anything, it has recursion detection and will try to display the recursion in a meaningful way. Parameters data_structure The structure to convert. When you freeze only content counts, same content same hash >>> a = hash(freeze(_testclass())) >>> b = hash(freeze(_testclass())) >>> b == a >>> a = freeze(_testclass()) >>> b = freeze(_testclass()) >>> b == a >>> x = _TestClass() >>> a = freeze(dump(x)) >>> b = freeze(dump(x)) Contents 1
>>> b == a When you dump-freeze only content/type counts, same content/type same hash Two object of the same type with same content will be equal Two object of the different type with same content will be different >>> a = hash(freeze(dump(_testclass()))) >>> b = hash(freeze(dump(_testclass()))) >>> b == a >>> a = freeze(dump(_testclass())) >>> b = freeze(dump(_testclass())) >>> b == a >>> a = hash(freeze(dump(_testclass()))) >>> b = hash(freeze(dump(_testclass2()))) >>> b!= a >>> a = freeze(dump(_testclass())) >>> b = freeze(dump(_testclass2())) >>> b!= a >>> _py2_to_py3(vformat(dump([1, { a : b }]))) [1, ["<class dict >", { a : b }]] >>> vformat(recursive_sort(dump(_testclass()))) ["<class freeze._testclass >", (( a, huhu ), ( sub, ["<class freeze._testslots >", (( a, slot ), ( b, (1, (1, 2, 3), 2, 3)))]))] >>> a = _TestSlots() >>> b = [a, 1, 2, [a, "banane"]] >>> _no_null_x(vformat(dump(b))) { a : slot, b : [1, 2, 3, (1, 2, 3)]}], 2 Contents
1, 2, banane ]] >>> a = [1, 2] >>> _no_null_x(vformat(dump((a, (a, a))))) [1, 2]], >>> recursive_sort(dump(freeze(_testclass()))) (( a, huhu ), ((( a, slot ), ( b, (1, (1, 2, 3), 2, 3))), sub )) >>> dump((none, (None, None))) (None, (None, None)) freeze.tree_diff_assert(a, b, n=5, sort=false) User tree_diff() to assert a equals b. Dump any data-structure or object, traverse it depth-first and apply a unified diff, to display the result. Parameters a data_structure a b data_structure b n (int) lines of context sort sort the data-structure ATTENTION: Sorting means changing the data-structure. The test-result may differ. But in case of dictionaries the results become comparable because the sorting negates the hash-algorithms de-sorting. >>> a = [... [3, 4],... { a : [3, { w : set([4, tree, frozenset([3,5,2])])}]}, >>> b = [... [4, 3],... { a : [3, { w : set([4, 3, frozenset([2,5,3])])}]}, >>> try:... tree_diff_assert(a, b, sort=)... except:... "GOT IT" GOT IT >>> a = [... [3, 4],... { a : [3, { w : set([4, 3, frozenset([3,5,2])])}]}, >>> b = [... [4, 3],... { a : [3, { w : set([ 3, 4, frozenset([2,5,3])])}]}, Contents 3
>>> tree_diff_assert(a, b, sort=) >>> a = [... [3, 4],... { a : [3, { w : set([4, 3, frozenset([3,5,2])])}]}, >>> b = [... [4, 3],... { a : [3, { w : set([ 3, 4, frozenset([2,5,3])])}]}, >>> try:... tree_diff_assert(a, b, sort=false)... except:... "GOT IT" GOT IT freeze.stable_hash(data_structure) Stable hash does: hash(recursive_sort(freeze(data_structure))) >>> a = stable_hash(_testclass()) >>> b = stable_hash(_testclass()) >>> a == b freeze.recursive_hash(data_structure) Recursive hash does: hash(freeze(data_structure)) >>> a = recursive_hash(_testclass()) >>> b = recursive_hash(_testclass()) >>> a == b freeze.tree_diff(a, b, n=5, sort=false) Dump any data-structure or object, traverse it depth-first in-order and apply a unified diff. Depth-first in-order is just like structure would be printed. Parameters a data_structure a b data_structure b n (int) lines of context sort sort the data-structure ATTENTION: Sorting means changing the data-structure. The test-result may differ. But in case of dictionaries the results become comparable because the sorting negates the hash-algorithms de-sorting. >>> a = recursive_sort(freeze([... [3, 4],... { a : [3, { w : set([4, 3, frozenset([3,5,2])])}]}, )) 4 Contents
>>> b = recursive_sort(freeze([... [7, 3],... { a : [3, { w : set([4, 3, frozenset([2,5,3])])}]}, )) >>> transparent_repr("\n".join(tree_diff(a, b).split("\n")[2:])) @@ -7,6 +7,6 @@ w ),), 3), a ),), a, (3, - 4)) + 7)) >>> a = [... [3, 4],... { a : [3, { w : set([4, 3, frozenset([3,5,2])])}]}, >>> b = [... [7, 3],... { a : [3, { w : set([4, 3, frozenset([2,5,3])])}]}, >>> transparent_repr("\n".join(... tree_diff(a, b, sort=... ).split("\n")[2:])) @@ -11,6 +11,6 @@ 3, 4)]),)], 3)),)], a, (3, - 4)) + 7)) freeze.vformat(*args, **kwargs) A pformat wrapper that produces narrow representations of data-structures. The result is repr transparent. Non-printable characters won t be escaped freeze.transparent_repr(string) The result is repr transparent. Non-printable characters won t be escaped >>> transparent_repr(3) 3 freeze.traverse_frozen_data(data_structure) Yields the leaves of the frozen data-structure pre-order. It will produce the same order as one would write the data-structure. class freeze.traversalbasedreprcompare(payload) Implements the comparison method for frozen data-structures based on traverse_frozen_data. Contents 5
>>> cm = TraversalBasedReprCompare >>> cm(3) < cm(4) >>> cm(4) > cm(3) >>> cm(3) > cm(4) False >>> cm(3) == cm(3) >>> cm(3) == cm(4) False >>> cm((3, 3)) > cm((3,)) >>> cm((3, 3)) == cm((3, 3)) >>> cm((3,)) > cm((3, 3)) False >>> cm((3,)) == cm((3, 3)) False 6 Contents
CHAPTER 1 Indices and tables genindex modindex search 7
8 Chapter 1. Indices and tables
Python Module Index f freeze,?? 9