1- from compas . datastructures import Tree
2- from compas . datastructures import TreeNode
1+ import hashlib
2+
33from compas .data import Data
44from compas .data import json_dumps
5- import hashlib
5+ from compas .datastructures import Tree
6+ from compas .datastructures import TreeNode
67
78
89class HashNode (TreeNode ):
@@ -99,8 +100,10 @@ def from_dict(cls, data_dict, path=""):
99100
100101
101102class HashTree (Tree ):
102- """A Hash tree (or Merkle tree) is a tree in which every leaf node is labelled with the cryptographic hash of a data block
103- and every non-leaf node is labelled with the hash of the labels of its child nodes.
103+ """HashTree data structure to compare differences in hierarchical data.
104+
105+ A Hash tree (or Merkle tree) is a tree in which every leaf node is labelled with the cryptographic hash
106+ of a data block and every non-leaf node is labelled with the hash of the labels of its child nodes.
104107 Hash trees allow efficient and secure verification of the contents of large data structures.
105108 They can also be used to compare different versions(states) of the same data structure for changes.
106109
@@ -109,19 +112,18 @@ class HashTree(Tree):
109112 signatures : dict[str, str]
110113 The SHA256 signatures of the nodes in the tree. The keys are the absolute paths of the nodes, the values are the signatures.
111114
112-
113115 Examples
114116 --------
115117 >>> tree1 = HashTree.from_dict({"a": {"b": 1, "c": 3}, "d": [1, 2, 3], "e": 2})
116118 >>> tree2 = HashTree.from_dict({"a": {"b": 1, "c": 2}, "d": [1, 2, 3], "f": 2})
117- >>> tree1.print_hierarchy( )
119+ >>> print(tree1 )
118120 +-- ROOT @ 4cd56
119121 +-- .a @ c16fd
120122 | +-- .b:1 @ c9b55
121123 | +-- .c:3 @ 518d4
122124 +-- .d:[1, 2, 3] @ 9be3a
123125 +-- .e:2 @ 68355
124- >>> tree2.print_hierarchy( )
126+ >>> print(tree2 )
125127 +-- ROOT @ fbe39
126128 +-- .a @ c2022
127129 | +-- .b:1 @ c9b55
@@ -136,11 +138,10 @@ class HashTree(Tree):
136138 Modified:
137139 {'path': '.a.c', 'old': 3, 'new': 2}
138140
139-
140141 """
141142
142- def __init__ (self ):
143- super ().__init__ ()
143+ def __init__ (self , ** kwargs ):
144+ super ().__init__ (** kwargs )
144145 self .signatures = {}
145146
146147 @classmethod
@@ -233,15 +234,11 @@ def _diff(node1, node2):
233234 if path in node2 .children_dict :
234235 _diff (node1 .children_dict [path ], node2 .children_dict [path ])
235236 else :
236- added .append (
237- {"path" : node1 .children_dict [path ].absolute_path , "value" : node1 .children_dict [path ].value }
238- )
237+ added .append ({"path" : node1 .children_dict [path ].absolute_path , "value" : node1 .children_dict [path ].value })
239238
240239 for path in node2 .children_paths :
241240 if path not in node1 .children_dict :
242- removed .append (
243- {"path" : node2 .children_dict [path ].absolute_path , "value" : node2 .children_dict [path ].value }
244- )
241+ removed .append ({"path" : node2 .children_dict [path ].absolute_path , "value" : node2 .children_dict [path ].value })
245242
246243 _diff (self .root , other .root )
247244
0 commit comments