Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions soda/core/soda/execution/identity.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
import warnings
from dataclasses import dataclass
from datetime import timedelta
from hashlib import blake2b
from numbers import Number
from typing import Optional

try:
from hashlib import blake2b

blake2b_available = True
except ImportError:
warnings.warn("The `hashlib.blake2b` hashing algorithm is not available. No identities will be generated.")
blake2b_available = False


class Identity:
@staticmethod
Expand Down Expand Up @@ -60,9 +68,9 @@ def __init__(self, hash_string_length: int = 8):
self.hash_string_length = hash_string_length
self.blake2b = None

def get_blake2b(self) -> blake2b:
def get_blake2b(self) -> "blake2b":
# Lazy initialization of blake2b in order to return None in the self.get_hash(self) in case nothing was added
if self.blake2b is None:
if self.blake2b is None and blake2b_available:
self.blake2b = blake2b(digest_size=int(self.hash_string_length / 2))
return self.blake2b

Expand All @@ -75,10 +83,14 @@ def add(self, value: Optional[str]):

if value is None:
return

blake2b = self.get_blake2b()
if blake2b is None:
return
Comment on lines +88 to +89

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the downside to none Identity ?

elif isinstance(value, str):
self.get_blake2b().update(value.encode("utf-8"))
blake2b.update(value.encode("utf-8"))
elif isinstance(value, Number) or isinstance(value, bool):
self.get_blake2b().update(str(value).encode("utf-8"))
blake2b.update(str(value).encode("utf-8"))
elif isinstance(value, list) or isinstance(value, dict):
self.add_all(value)
elif isinstance(value, timedelta):
Expand Down