halib 0.2.23__py3-none-any.whl → 0.2.27__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
halib/utils/dict.py CHANGED
@@ -1,5 +1,9 @@
1
+ from pygments.token import Other
1
2
  from typing import Dict, Any, Callable, Optional
2
3
  from rich.pretty import pprint
4
+ import json
5
+ import hashlib
6
+
3
7
 
4
8
  class DictUtils:
5
9
  """
@@ -112,8 +116,26 @@ class DictUtils:
112
116
  target = {}
113
117
  target[parts[-1]] = value
114
118
 
119
+ @staticmethod
120
+ def get_unique_hash(input_dict, length=12):
121
+ """
122
+ Returns a unique hash string for a dictionary.
115
123
 
116
- if __name__ == "__main__":
124
+ :param input_dict: The dictionary params
125
+ :param length: The desired length of the hash string (default 12)
126
+ """
127
+ assert length >= 12, "Hash length must be at least 12 to ensure uniqueness."
128
+ # 1. Sort keys to ensure {a:1, b:2} == {b:2, a:1}
129
+ config_str = json.dumps(input_dict, sort_keys=True)
130
+
131
+ # 2. Generate full SHA-256 hash (64 chars long)
132
+ full_hash = hashlib.sha256(config_str.encode("utf-8")).hexdigest()
133
+
134
+ # 3. Truncate to desired length
135
+ return full_hash[:length]
136
+
137
+
138
+ def test_update():
117
139
  # --- Setup ---
118
140
  base_config = {
119
141
  "model": {
@@ -154,3 +176,28 @@ if __name__ == "__main__":
154
176
  # --- Deep Update (The Solution) ---
155
177
  pprint("Deep Update Result:")
156
178
  pprint(DictUtils.deep_update(b2, n2))
179
+
180
+
181
+ def test_hash():
182
+ # --- Usage ---
183
+ cfg1 = {"learning_rate": 0.01, "batch_size": 32, "optimizer": "adam"}
184
+ cfg1_shuffle = {
185
+ "batch_size": 32,
186
+ "optimizer": "adam",
187
+ "learning_rate": 0.01,
188
+ }
189
+ cfg2 = {"learning_rate": 0.02, "batch_size": 32, "optimizer": "adam"}
190
+ hash1 = DictUtils.get_unique_hash(cfg1)
191
+ hash2 = DictUtils.get_unique_hash(cfg1_shuffle)
192
+ hash3 = DictUtils.get_unique_hash(cfg2)
193
+ pprint(f"Config 1 Hash: {hash1}")
194
+ pprint(f"Config 1_shuffle Hash: {hash2}")
195
+ pprint(f"Config 2 Hash: {hash3}")
196
+
197
+ assert hash1 == hash2, "Hashes should match for identical dicts."
198
+ assert hash1 != hash3, "Hashes should differ for different dicts."
199
+
200
+
201
+ if __name__ == "__main__":
202
+ test_update()
203
+ test_hash()
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: halib
3
- Version: 0.2.23
3
+ Version: 0.2.27
4
4
  Summary: Small library for common tasks
5
5
  Author: Hoang Van Ha
6
6
  Author-email: hoangvanhauit@gmail.com
@@ -56,7 +56,7 @@ Dynamic: summary
56
56
 
57
57
  ## v0.2.x (Experiment & Core Updates)
58
58
 
59
- ### **v0.2.23**
59
+ ### **v0.2.27**
60
60
  - ✨ **New Feature:** Added `utils.dict.DictUtils` for advanced dictionary manipulations (merging, filtering, transforming).
61
61
 
62
62
  - ✨ **New Feature:** Added `common.common.pprint_stack_trace` to print stack traces with optional custom messages and force stop capability.
@@ -95,15 +95,15 @@ halib/system/filesys.py,sha256=102J2fkQhmH1_-HQVy2FQ4NOU8LTjMWV3hToT_APtq8,4401
95
95
  halib/system/path.py,sha256=k_pveq41uXEzKPU2KTIdqjUSb4MVM-hCFXHGeO-6x6Q,3694
96
96
  halib/utils/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
97
97
  halib/utils/dataclass_util.py,sha256=rj2IMLlUzbm2OlF5_B2dRTk9njZOaF7tTjYkOsq8uLY,1416
98
- halib/utils/dict.py,sha256=HUSWucEVq7drhmMYmTXisKCKmKGfEFyZCP3J7DCS1o0,5375
98
+ halib/utils/dict.py,sha256=tUmwKNbTLOyGLl5nKYdT8QIoS4pBFfdsZHCJ6NQCgp4,6922
99
99
  halib/utils/dict_op.py,sha256=wYE6Iw-_CnCWdMg9tpJ2Y2-e2ESkW9FxmdBkZkbUh80,299
100
100
  halib/utils/gpu_mon.py,sha256=vD41_ZnmPLKguuq9X44SB_vwd9JrblO4BDzHLXZhhFY,2233
101
101
  halib/utils/list.py,sha256=BM-8sRhYyqF7bh4p7TQtV7P_gnFruUCA6DTUOombaZg,337
102
102
  halib/utils/listop.py,sha256=Vpa8_2fI0wySpB2-8sfTBkyi_A4FhoFVVvFiuvW8N64,339
103
103
  halib/utils/tele_noti.py,sha256=-4WXZelCA4W9BroapkRyIdUu9cUVrcJJhegnMs_WpGU,5928
104
104
  halib/utils/video.py,sha256=zLoj5EHk4SmP9OnoHjO8mLbzPdtq6gQPzTQisOEDdO8,3261
105
- halib-0.2.23.dist-info/licenses/LICENSE.txt,sha256=qZssdna4aETiR8znYsShUjidu-U4jUT9Q-EWNlZ9yBQ,1100
106
- halib-0.2.23.dist-info/METADATA,sha256=X2N2koeHn7O7_mxOhuiyu7DZaxOdjBWyCxTi_6787w0,7719
107
- halib-0.2.23.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
108
- halib-0.2.23.dist-info/top_level.txt,sha256=7AD6PLaQTreE0Fn44mdZsoHBe_Zdd7GUmjsWPyQ7I-k,6
109
- halib-0.2.23.dist-info/RECORD,,
105
+ halib-0.2.27.dist-info/licenses/LICENSE.txt,sha256=qZssdna4aETiR8znYsShUjidu-U4jUT9Q-EWNlZ9yBQ,1100
106
+ halib-0.2.27.dist-info/METADATA,sha256=Or9W2uqrbMh_PQd3KCda2KZRuuH4Wi7I4aicUPq2Ks0,7719
107
+ halib-0.2.27.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
108
+ halib-0.2.27.dist-info/top_level.txt,sha256=7AD6PLaQTreE0Fn44mdZsoHBe_Zdd7GUmjsWPyQ7I-k,6
109
+ halib-0.2.27.dist-info/RECORD,,
File without changes