Skip to content

Commit ce34888

Browse files
committed
reformats
1 parent 9208212 commit ce34888

File tree

2 files changed

+15
-17
lines changed

2 files changed

+15
-17
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1616
* Added `compas.geometry.Brep.from_plane`.
1717
* Added `compas.tolerance.Tolerance.angulardeflection`.
1818
* Added `compas.scene.SceneObject.scene` attribute.
19+
* Added `compas.datastructures.HashTree` and `compas.datastructures.HashNode`.
1920

2021
### Changed
2122

src/compas/datastructures/tree/hashtree.py

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
from compas.datastructures import Tree
2-
from compas.datastructures import TreeNode
1+
import hashlib
2+
33
from compas.data import Data
44
from compas.data import json_dumps
5-
import hashlib
5+
from compas.datastructures import Tree
6+
from compas.datastructures import TreeNode
67

78

89
class HashNode(TreeNode):
@@ -99,8 +100,10 @@ def from_dict(cls, data_dict, path=""):
99100

100101

101102
class 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

Comments
 (0)