zhmiscellany 6.1.7__py3-none-any.whl → 6.1.9__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.
zhmiscellany/fileio.py CHANGED
@@ -8,6 +8,9 @@ from itertools import chain
8
8
  import tempfile
9
9
  import random
10
10
  import string
11
+ import orjson
12
+ from datetime import datetime
13
+ import inspect
11
14
 
12
15
 
13
16
  def read_json_file(file_path):
@@ -207,22 +210,58 @@ def chdir_to_script_dir():
207
210
  os.chdir(os.path.dirname(get_script_path()))
208
211
 
209
212
 
210
- def cache(seed, function):
213
+ def cache(function, *args, **kwargs):
211
214
  cache_folder = 'zhmiscellany_cache'
212
- zhmiscellany.fileio.create_folder(cache_folder)
213
215
 
214
- def generate_hash(obj):
215
- obj_str = str(obj)
216
- return hashlib.md5(obj_str.encode()).hexdigest()
217
-
218
- seed_hash = generate_hash(seed)
219
-
220
- cache_file = f'{cache_folder}/fn_cache_{seed_hash}.pkl'
216
+ def get_hash_orjson(data):
217
+ def default_converter(obj):
218
+ if callable(obj):
219
+ try:
220
+ return inspect.getsource(obj)
221
+ except (OSError, TypeError):
222
+ return str(obj) # Fallback for lambdas/partials
223
+
224
+ # SETS: Must be sorted to ensure determinism!
225
+ # JSON doesn't support sets, so we turn them into sorted lists.
226
+ if isinstance(obj, set):
227
+ return sorted(list(obj))
228
+
229
+ # BYTES: Decode to string (if utf-8) or hex
230
+ if isinstance(obj, bytes):
231
+ return obj.hex()
232
+
233
+ # DATETIMES: Convert to ISO format string
234
+ if isinstance(obj, datetime):
235
+ return obj.isoformat()
236
+
237
+ # CUSTOM OBJECTS: Try to return their __dict__ or string rep
238
+ if hasattr(obj, '__dict__'):
239
+ return obj.__dict__
240
+
241
+ # Fallback: String representation (risky if str() format changes)
242
+ return str(obj)
243
+ json_bytes = orjson.dumps(
244
+ data,
245
+ default=default_converter,
246
+ option=orjson.OPT_SORT_KEYS
247
+ )
248
+ return hashlib.md5(json_bytes).hexdigest()
249
+
250
+ seed = {
251
+ 'function': function,
252
+ 'args': args,
253
+ 'kwargs': kwargs
254
+ }
255
+
256
+ seed_hash = get_hash_orjson(seed)
257
+
258
+ cache_file = f'{cache_folder}/cache_{function.__name__}_{seed_hash}.pkl'
221
259
 
222
260
  if os.path.exists(cache_file):
223
261
  return load_object_from_file(cache_file)
224
262
  else:
225
- result = function(seed)
263
+ result = function(*args, **kwargs)
264
+ zhmiscellany.fileio.create_folder(cache_folder)
226
265
  save_object_to_file(result, cache_file)
227
266
  return result
228
267
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: zhmiscellany
3
- Version: 6.1.7
3
+ Version: 6.1.9
4
4
  Summary: A collection of useful/interesting python libraries made by zh.
5
5
  Home-page: https://discord.gg/ThBBAuueVJ
6
6
  Author: zh
@@ -20,6 +20,7 @@ Requires-Dist: psutil>=0
20
20
  Requires-Dist: kthread>=0
21
21
  Requires-Dist: pillow>=0
22
22
  Requires-Dist: fuzzywuzzy>=0
23
+ Requires-Dist: orjson>=0
23
24
  Requires-Dist: pyautogui>=0; sys_platform == "linux"
24
25
  Requires-Dist: ray>=0; sys_platform == "win32"
25
26
  Requires-Dist: pywin32>=0; sys_platform == "win32"
@@ -8,7 +8,7 @@ zhmiscellany/_resource_files_lookup.py,sha256=hsgPW0dngokgqWrAVAv5rUo-eobzJPjvWH
8
8
  zhmiscellany/cpp.py,sha256=XEUEoIKCDCdY5VgwNWE5oXrGjtsmGdz_MnaVwQmi2dk,179
9
9
  zhmiscellany/dict.py,sha256=0BZJ5eK-MurAHYV1OPa0jdGTr-QEWos7ZM0npb-tN9I,81
10
10
  zhmiscellany/discord.py,sha256=nzXjRnJbuYbuH4hScqqdsF61VeXx8xiYrwp_AKTWt9o,19774
11
- zhmiscellany/fileio.py,sha256=KmRnWWZJWBc5Or3_mKcRY2ehE_Fuk870iS-imtyERyg,18169
11
+ zhmiscellany/fileio.py,sha256=QpF4j2XzJd3naIQXQJL-pdNWnj4cOT1BF-qhMNZUFdo,19520
12
12
  zhmiscellany/gui.py,sha256=BDmM7e5ITKQuzxSzaYvPapQuPbQjCMTOq-GyMcLCIgg,10198
13
13
  zhmiscellany/image.py,sha256=qUjxiYpc2VVZp2vwr1vN36O2PVQ7YlMKzhegQ1u4c0M,8198
14
14
  zhmiscellany/list.py,sha256=S8Z85bLJEP9lk2JkGpzUcG6kpRB7a-NWDIHMPiF5bKo,1473
@@ -21,7 +21,7 @@ zhmiscellany/pipes.py,sha256=zETvWP4PF-PuSzYwR1UCodY4ftNAgmCChb9DUMofXok,4657
21
21
  zhmiscellany/processing.py,sha256=sDKIbzG9TNFyT6yJ4TJL59taG-59_v3CBLekVSLrwgE,10899
22
22
  zhmiscellany/rust.py,sha256=znN6DYNoa_p-braTuDZKvUnXX8reWLFu_dG4fv2vLR0,442
23
23
  zhmiscellany/string.py,sha256=xyqE6V5YF2nieZDcg5ZrXTIrH2D9oDRbZ5vQGz8rPys,4787
24
- zhmiscellany-6.1.7.dist-info/METADATA,sha256=GDm6CiJPv9IGENIs3mGXKOVEZV00fS1lJYJyY8tZJ9o,43871
25
- zhmiscellany-6.1.7.dist-info/WHEEL,sha256=tZoeGjtWxWRfdplE7E3d45VPlLNQnvbKiYnx7gwAy8A,92
26
- zhmiscellany-6.1.7.dist-info/top_level.txt,sha256=ioDtsrevCI52rTxZntMPljRIBsZs73tD0hI00HektiE,13
27
- zhmiscellany-6.1.7.dist-info/RECORD,,
24
+ zhmiscellany-6.1.9.dist-info/METADATA,sha256=gA3JaF8Ci0hQk4stp_Mjapd39GslDf99mqgfGZ9-EeQ,43897
25
+ zhmiscellany-6.1.9.dist-info/WHEEL,sha256=hPN0AlP2dZM_3ZJZWP4WooepkmU9wzjGgCLCeFjkHLA,92
26
+ zhmiscellany-6.1.9.dist-info/top_level.txt,sha256=ioDtsrevCI52rTxZntMPljRIBsZs73tD0hI00HektiE,13
27
+ zhmiscellany-6.1.9.dist-info/RECORD,,
@@ -1,5 +1,5 @@
1
1
  Wheel-Version: 1.0
2
- Generator: bdist_wheel (0.45.1)
2
+ Generator: bdist_wheel (0.46.3)
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
5
5