aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthias Baumgartner <dev@igsor.net>2023-01-16 21:39:47 +0100
committerMatthias Baumgartner <dev@igsor.net>2023-01-16 21:39:47 +0100
commit76fa694911f54e293ddf517246c6c4a1e8e745fd (patch)
treefcd3ac9f386cc7f709460884100c2caaeba35bdf
parentafdfc25408c3b9d2c779c83e2e193d68a973810b (diff)
downloadbsfs-76fa694911f54e293ddf517246c6c4a1e8e745fd.tar.gz
bsfs-76fa694911f54e293ddf517246c6c4a1e8e745fd.tar.bz2
bsfs-76fa694911f54e293ddf517246c6c4a1e8e745fd.zip
uuid from dict
-rw-r--r--bsfs/utils/uuid.py7
-rw-r--r--test/utils/test_uuid.py4
2 files changed, 11 insertions, 0 deletions
diff --git a/bsfs/utils/uuid.py b/bsfs/utils/uuid.py
index 6366b18..ba5cf52 100644
--- a/bsfs/utils/uuid.py
+++ b/bsfs/utils/uuid.py
@@ -7,6 +7,7 @@ Author: Matthias Baumgartner, 2022
# imports
from collections import abc
import hashlib
+import json
import os
import platform
import random
@@ -105,4 +106,10 @@ class UCID():
with open(path, 'rb') as ifile:
return HASH(ifile.read()).hexdigest()
+
+ @staticmethod
+ def from_dict(content: dict) -> str:
+ """Get the content from a dict."""
+ return HASH(json.dumps(content).encode('ascii', 'ignore')).hexdigest()
+
## EOF ##
diff --git a/test/utils/test_uuid.py b/test/utils/test_uuid.py
index 49176d4..0de96ed 100644
--- a/test/utils/test_uuid.py
+++ b/test/utils/test_uuid.py
@@ -83,6 +83,10 @@ class TestUCID(unittest.TestCase):
def test_from_path(self):
self.assertEqual(UCID.from_path(self._path), self._checksum)
+ def test_from_dict(self):
+ self.assertEqual(UCID.from_dict({'hello': 'world', 'foo': 1234, 'bar': False}),
+ '8d2544395a0d2827e3d9ce8cd619d5e3f801e8126bf3f93ee5abd38158959585')
+
## main ##