Skip to content

Commit

Permalink
reformats
Browse files Browse the repository at this point in the history
  • Loading branch information
Licini committed May 13, 2024
1 parent 9208212 commit ce34888
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 17 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* Added `compas.geometry.Brep.from_plane`.
* Added `compas.tolerance.Tolerance.angulardeflection`.
* Added `compas.scene.SceneObject.scene` attribute.
* Added `compas.datastructures.HashTree` and `compas.datastructures.HashNode`.

### Changed

Expand Down
31 changes: 14 additions & 17 deletions src/compas/datastructures/tree/hashtree.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from compas.datastructures import Tree
from compas.datastructures import TreeNode
import hashlib

from compas.data import Data
from compas.data import json_dumps
import hashlib
from compas.datastructures import Tree
from compas.datastructures import TreeNode


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


class HashTree(Tree):
"""A Hash tree (or Merkle tree) is a tree in which every leaf node is labelled with the cryptographic hash of a data block
and every non-leaf node is labelled with the hash of the labels of its child nodes.
"""HashTree data structure to compare differences in hierarchical data.
A Hash tree (or Merkle tree) is a tree in which every leaf node is labelled with the cryptographic hash
of a data block and every non-leaf node is labelled with the hash of the labels of its child nodes.
Hash trees allow efficient and secure verification of the contents of large data structures.
They can also be used to compare different versions(states) of the same data structure for changes.
Expand All @@ -109,19 +112,18 @@ class HashTree(Tree):
signatures : dict[str, str]
The SHA256 signatures of the nodes in the tree. The keys are the absolute paths of the nodes, the values are the signatures.
Examples
--------
>>> tree1 = HashTree.from_dict({"a": {"b": 1, "c": 3}, "d": [1, 2, 3], "e": 2})
>>> tree2 = HashTree.from_dict({"a": {"b": 1, "c": 2}, "d": [1, 2, 3], "f": 2})
>>> tree1.print_hierarchy()
>>> print(tree1)
+-- ROOT @ 4cd56
+-- .a @ c16fd
| +-- .b:1 @ c9b55
| +-- .c:3 @ 518d4
+-- .d:[1, 2, 3] @ 9be3a
+-- .e:2 @ 68355
>>> tree2.print_hierarchy()
>>> print(tree2)
+-- ROOT @ fbe39
+-- .a @ c2022
| +-- .b:1 @ c9b55
Expand All @@ -136,11 +138,10 @@ class HashTree(Tree):
Modified:
{'path': '.a.c', 'old': 3, 'new': 2}
"""

def __init__(self):
super().__init__()
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.signatures = {}

@classmethod
Expand Down Expand Up @@ -233,15 +234,11 @@ def _diff(node1, node2):
if path in node2.children_dict:
_diff(node1.children_dict[path], node2.children_dict[path])
else:
added.append(
{"path": node1.children_dict[path].absolute_path, "value": node1.children_dict[path].value}
)
added.append({"path": node1.children_dict[path].absolute_path, "value": node1.children_dict[path].value})

for path in node2.children_paths:
if path not in node1.children_dict:
removed.append(
{"path": node2.children_dict[path].absolute_path, "value": node2.children_dict[path].value}
)
removed.append({"path": node2.children_dict[path].absolute_path, "value": node2.children_dict[path].value})

_diff(self.root, other.root)

Expand Down

0 comments on commit ce34888

Please sign in to comment.