Skip to content
Merged
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
7 changes: 5 additions & 2 deletions apps/common/utils/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,10 @@ def bulk_create_in_batches(model, data, batch_size=1000):
model.objects.bulk_create(batch)


def get_sha256_hash(_v: str):
def get_sha256_hash(_v: str | bytes):
sha256 = hashlib.sha256()
sha256.update(_v.encode())
if isinstance(_v, str):
sha256.update(_v.encode())
else:
sha256.update(_v)
return sha256.hexdigest()
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The provided code has several improvements. First, it replaces an if statement used to distinguish between strings and binary values with a more robust approach using type checking via Python's built-in isinstance() function. This enhances readability and makes the code easier to maintain. Additionally, there are no significant issues or optimizations required; however, ensuring that _v is either a string or bytes is generally good practice for consistent input handling.

If you have specific areas of concern or further details needed on optimizing this particular function, feel free to let me know!

Loading