Deterministic hash
@Tom-Kingstone 已经在做这个了。
开始于 2026年1月22日。
评估
这个 Issue 还没有评估数据。
描述
Description:
@tg359 has made a method that can be used to convert a list of values into a has in python:
import hashlib
import json
from typing import Any, Union
from ladybug.epw import EPW
# OLD EXT COMFORT METHOD
def _create_hash(
epw_file: Path,
ground_material: Union[EnergyMaterial, EnergyMaterialVegetation],
shade_material: Union[EnergyMaterial, EnergyMaterialVegetation],
) -> str:
"""Create unique hash for this configuration."""
content = (
f"{epw_file.name}_{ground_material.identifier}_{shade_material.identifier}"
)
return hashlib.shake_256(
content.encode(
"utf-8"
)
).hexdigest(10)
# GENERIC METHOD + SUPPORTING METHODS
def deep_sort_dict(obj: dict[Any, Any], sort_lists: bool = True) -> Any:
"""Recursively sort nested dictionaries and lists for consistent ordering."""
if isinstance(obj, dict):
return {
k: deep_sort_dict(v, sort_lists=sort_lists) for k, v in sorted(obj.items())
}
elif sort_lists and isinstance(obj, (list, tuple)):
processed = [deep_sort_dict(x, sort_lists=sort_lists) for x in obj]
if isinstance(obj, tuple):
return tuple(
sorted(
processed,
key=lambda x: json.dumps(x, sort_keys=True, cls=AllPowerfulEncoder),
)
)
else:
return sorted(
processed,
key=lambda x: json.dumps(x, sort_keys=True, cls=AllPowerfulEncoder),
)
else:
return obj
def deterministic_hash(*values: Any, length: int = 16, algorithm: str = "md5", sort_lists: bool = True) -> int:
"""Create a deterministic hash from one or more values.
This function produces consistent hash values across Python sessions,
unlike the built-in hash() function which uses random salting.
Args:
*values (Any):
One or more values to hash. Will be converted to strings and
concatenated before hashing.
length (int, optional):
Number of hexadecimal characters to use from the hash.
Defaults to 16 (64 bits).
algorithm (str, optional):
Hash algorithm to use. Must be supported by hashlib.
Common options: 'md5', 'sha1', 'sha256', 'sha512'.
Defaults to 'md5'.
sort_lists (bool, optional):
Whether to sort lists and dictionaries recursively before hashing,
to ensure consistent ordering. Defaults to True.
Returns:
int:
An integer hash value derived from the input values.
Raises:
ValueError:
If the algorithm is not supported by hashlib.
Example:
>>> deterministic_hash("hello", "world", 123)
123456789012345678
>>> deterministic_hash("hello", "world", 123) # same result in new session
123456789012345678
>>> deterministic_hash("test", length=8)
12345678
"""
# validate algorithm
if algorithm not in hashlib.algorithms_available:
raise ValueError(
f"Hash algorithm '{algorithm}' not supported. "
f"Available: {sorted(hashlib.algorithms_available)}"
)
# canonicalize each value
canonicalized = []
for v in values:
if isinstance(v, (dict, list)):
v = deep_sort_dict(v, sort_lists=sort_lists)
v = json.dumps(v, sort_keys=True, cls=AllPowerfulEncoder)
canonicalized.append(str(v))
concatenated = "".join(canonicalized)
hasher = hashlib.new(algorithm)
hasher.update(concatenated.encode("utf-8"))
hex_digest = hasher.hexdigest()[:length]
return int(hex_digest, 16)
This would be most useful in toolkits like LadybugTools_Toolkit where ExternalComfort simulations require identifiers to be under 100 characters, but will be placed in this toolkit in case other tools require it.
- 主要语言
- Python
- 星标
- 5
- 派生
- 1
- 平均合并
- 1 天 10 小时
- 30 天内合并 PR
- 4
环境准备
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
BHoM/Python_Toolkit 的其他 Issue
-
Add python version 3.13可能已有人在做 @Tom-Kingstone 于 11 天前认领。 未关闭type:feature
BHoM/Python_Toolkit#250 · 已指派 1 人 ·
-
Searchable list box可能重新可做 @Felix-Mallinder 于 38 天前认领,目前没有进行中的 PR。 未关闭type:feature
BHoM/Python_Toolkit#243 · 已指派 1 人 ·
-
type:feature
难度 4/5 3-5 天 新手友好度 35/100
BHoM/Python_Toolkit#234 ·
-
capture toolkit name properly in python logs可能重新可做 @Tom-Kingstone 于 68 天前认领,目前没有进行中的 PR。 未关闭type:feature
BHoM/Python_Toolkit#228 · 已指派 1 人 ·
-
Colormap builder可能重新可做 @Felix-Mallinder 于 131 天前认领,目前没有进行中的 PR。 未关闭type:feature
BHoM/Python_Toolkit#217 · 已指派 1 人 ·
查看 BHoM/Python_Toolkit 的全部 Issue
相似的 Issue
-
难度 2/5 1-3 小时 新手友好度 88/100
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 84/100
spec-kitty/spec-kitty#5319 ·
维护者通常 1 天内回复
-
backend::vllm diffusion multimodal
难度 2/5 1-3 小时 新手友好度 72/100
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 84/100
openai/openai-agents-python#5229 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 84/100
维护者通常 1 天内回复