Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add crc32_hash #755

Merged
merged 1 commit into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ zzz-test-typing = []
zzz-test-warnings = []
zzz-test-whenever = ["whenever >= 0.6.9, < 0.7"]
zzz-test-zipfile = []
zzz-test-zlib = ["orjson >= 3.10.7, < 3.11"]
zzz-test-zoneinfo = ["tzdata >= 2024.1, < 2024.2"]

[project.scripts]
Expand Down
4 changes: 2 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ multidict==6.1.0
# via
# aiohttp
# yarl
narwhals==1.8.1
narwhals==1.8.2
# via altair
nest-asyncio==1.6.0
# via dycw-utilities (pyproject.toml)
Expand Down Expand Up @@ -234,7 +234,7 @@ packaging==24.1
# pytest
# pytest-rerunfailures
# streamlit
pandas==2.2.2
pandas==2.2.3
# via
# streamlit
# vegafusion
Expand Down
2 changes: 1 addition & 1 deletion requirements/altair.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ lxml==5.3.0
# via pikepdf
markupsafe==2.1.5
# via jinja2
narwhals==1.8.1
narwhals==1.8.2
# via altair
packaging==24.1
# via
Expand Down
4 changes: 2 additions & 2 deletions requirements/jupyter.txt
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ packaging==24.1
# nbconvert
# pytest
# pytest-rerunfailures
pandas==2.2.2
pandas==2.2.3
# via dycw-utilities (pyproject.toml)
pandocfilters==1.5.1
# via nbconvert
Expand All @@ -175,7 +175,7 @@ pluggy==1.5.0
# via pytest
polars-lts-cpu==1.7.1
# via dycw-utilities (pyproject.toml)
prometheus-client==0.20.0
prometheus-client==0.21.0
# via jupyter-server
prompt-toolkit==3.0.47
# via ipython
Expand Down
4 changes: 2 additions & 2 deletions requirements/streamlit.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ markupsafe==2.1.5
# via jinja2
mdurl==0.1.2
# via markdown-it-py
narwhals==1.8.1
narwhals==1.8.2
# via altair
numpy==2.1.1
# via
Expand All @@ -57,7 +57,7 @@ packaging==24.1
# pytest
# pytest-rerunfailures
# streamlit
pandas==2.2.2
pandas==2.2.3
# via streamlit
pillow==10.4.0
# via streamlit
Expand Down
39 changes: 39 additions & 0 deletions requirements/zlib.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# This file was autogenerated by uv via the following command:
# uv pip compile --extra=zzz-test-defaults --extra=zzz-test-zlib --prerelease=disallow --output-file=requirements/zlib.txt --python-version=3.11 pyproject.toml
attrs==24.2.0
# via hypothesis
coverage==7.6.1
# via pytest-cov
hypothesis==6.112.1
# via dycw-utilities (pyproject.toml)
iniconfig==2.0.0
# via pytest
orjson==3.10.7
# via dycw-utilities (pyproject.toml)
packaging==24.1
# via
# pytest
# pytest-rerunfailures
pluggy==1.5.0
# via pytest
pytest==8.3.3
# via
# dycw-utilities (pyproject.toml)
# pytest-asyncio
# pytest-cov
# pytest-randomly
# pytest-rerunfailures
pytest-asyncio==0.23.8
# via dycw-utilities (pyproject.toml)
pytest-cov==5.0.0
# via dycw-utilities (pyproject.toml)
pytest-randomly==3.15.0
# via dycw-utilities (pyproject.toml)
pytest-rerunfailures==14.0
# via dycw-utilities (pyproject.toml)
sortedcontainers==2.4.0
# via hypothesis
tomli==2.0.1
# via coverage
typing-extensions==4.12.2
# via dycw-utilities (pyproject.toml)
1 change: 1 addition & 0 deletions scripts/packages.txt
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,5 @@ typing
warnings
whenever
zipfile
zlib
zoneinfo
37 changes: 37 additions & 0 deletions src/tests/test_zlib.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from __future__ import annotations

from typing import Any

from hypothesis import given
from hypothesis.strategies import (
DataObject,
SearchStrategy,
data,
dictionaries,
frozensets,
lists,
sets,
tuples,
)
from pytest import mark, param

from utilities.hypothesis import int64s, text_ascii
from utilities.zlib import crc32_hash


class TestCRC32Hash:
@given(data=data())
@mark.parametrize(
"strategy",
[
param(dictionaries(text_ascii(), int64s(), max_size=3)),
param(frozensets(int64s(), max_size=3)),
param(lists(int64s(), max_size=3)),
param(sets(int64s(), max_size=3)),
],
)
def test_main(self, *, data: DataObject, strategy: SearchStrategy[Any]) -> None:
x, y = data.draw(tuples(strategy, strategy))
res = crc32_hash(x) == crc32_hash(y)
expected = x == y
assert res is expected
2 changes: 1 addition & 1 deletion src/utilities/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from __future__ import annotations

__version__ = "0.57.11"
__version__ = "0.57.12"
4 changes: 2 additions & 2 deletions src/utilities/hashlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
from hashlib import md5
from typing import Any

from utilities.orjson import serialize


def md5_hash(obj: Any, /) -> str:
"""Compute the MD5 hash of an arbitrary object."""
from utilities.orjson import serialize

return md5(serialize(obj), usedforsecurity=False).hexdigest()


Expand Down
14 changes: 14 additions & 0 deletions src/utilities/zlib.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from __future__ import annotations

from typing import Any
from zlib import crc32


def crc32_hash(obj: Any, /) -> int:
"""Compute the CRC32 hash of an arbitrary object."""
from utilities.orjson import serialize

return crc32(serialize(obj))


__all__ = ["crc32_hash"]