pythonwrench 0.6.7__py3-none-any.whl → 0.6.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.
Files changed (57) hide show
  1. pythonwrench/__init__.py +28 -6
  2. pythonwrench/__main__.py +0 -1
  3. pythonwrench/_core.py +138 -10
  4. pythonwrench/abc.py +0 -1
  5. pythonwrench/argparse/__init__.py +0 -1
  6. pythonwrench/argparse/dataclass_.py +210 -48
  7. pythonwrench/argparse/parsers.py +24 -40
  8. pythonwrench/cast.py +8 -16
  9. pythonwrench/checksum.py +0 -4
  10. pythonwrench/collections/__init__.py +0 -1
  11. pythonwrench/collections/collections.py +33 -54
  12. pythonwrench/collections/prop.py +0 -3
  13. pythonwrench/collections/reducers.py +2 -24
  14. pythonwrench/concurrent.py +0 -1
  15. pythonwrench/csv.py +0 -1
  16. pythonwrench/dataclasses.py +70 -4
  17. pythonwrench/datetime.py +7 -5
  18. pythonwrench/difflib.py +0 -1
  19. pythonwrench/disk_cache.py +1 -23
  20. pythonwrench/entrypoints/info.py +2 -1
  21. pythonwrench/entrypoints/safe_rmdir.py +45 -57
  22. pythonwrench/entrypoints/tree.py +26 -45
  23. pythonwrench/enum.py +0 -1
  24. pythonwrench/functools.py +8 -38
  25. pythonwrench/hashlib.py +40 -10
  26. pythonwrench/importlib.py +15 -9
  27. pythonwrench/inspect.py +0 -1
  28. pythonwrench/json.py +0 -1
  29. pythonwrench/jsonl.py +0 -1
  30. pythonwrench/logging.py +2 -3
  31. pythonwrench/math.py +0 -2
  32. pythonwrench/os.py +44 -1
  33. pythonwrench/pickle.py +0 -1
  34. pythonwrench/random.py +0 -3
  35. pythonwrench/re.py +75 -5
  36. pythonwrench/semver.py +31 -15
  37. pythonwrench/serialization/__init__.py +17 -2
  38. pythonwrench/serialization/_core.py +353 -14
  39. pythonwrench/serialization/csv.py +188 -207
  40. pythonwrench/serialization/json.py +128 -95
  41. pythonwrench/serialization/jsonl.py +109 -97
  42. pythonwrench/serialization/pickle.py +96 -96
  43. pythonwrench/serialization/yaml/__init__.py +48 -0
  44. pythonwrench/serialization/yaml/_yaml_fallback.py +63 -0
  45. pythonwrench/serialization/yaml/definitions.py +81 -0
  46. pythonwrench/serialization/yaml/func.py +318 -0
  47. pythonwrench/time.py +0 -1
  48. pythonwrench/typing/__init__.py +2 -1
  49. pythonwrench/typing/checks.py +159 -72
  50. pythonwrench/typing/classes.py +0 -1
  51. pythonwrench/warnings.py +5 -116
  52. {pythonwrench-0.6.7.dist-info → pythonwrench-0.6.9.dist-info}/METADATA +8 -5
  53. pythonwrench-0.6.9.dist-info/RECORD +56 -0
  54. {pythonwrench-0.6.7.dist-info → pythonwrench-0.6.9.dist-info}/WHEEL +1 -1
  55. pythonwrench-0.6.7.dist-info/RECORD +0 -52
  56. {pythonwrench-0.6.7.dist-info → pythonwrench-0.6.9.dist-info}/entry_points.txt +0 -0
  57. {pythonwrench-0.6.7.dist-info → pythonwrench-0.6.9.dist-info}/licenses/LICENSE +0 -0
pythonwrench/__init__.py CHANGED
@@ -1,5 +1,4 @@
1
1
  #!/usr/bin/env python
2
- # -*- coding: utf-8 -*-
3
2
 
4
3
  """Python library with tools for typing, manipulating collections, and more!"""
5
4
 
@@ -9,7 +8,7 @@ __author_email__ = "labbeti.pub@gmail.com"
9
8
  __license__ = "MIT"
10
9
  __maintainer__ = "Étienne Labbé (Labbeti)"
11
10
  __status__ = "Development"
12
- __version__ = "0.6.7"
11
+ __version__ = "0.6.9"
13
12
 
14
13
 
15
14
  from typing import TYPE_CHECKING
@@ -127,7 +126,7 @@ if TYPE_CHECKING or lazy is None:
127
126
  identity,
128
127
  repeat_fn,
129
128
  )
130
- from .hashlib import hash_file
129
+ from .hashlib import hash_bytes, hash_bytes_buffer, hash_file, hash_str
131
130
  from .importlib import (
132
131
  ModulePlaceholder,
133
132
  Placeholder,
@@ -157,12 +156,14 @@ if TYPE_CHECKING or lazy is None:
157
156
  setup_logging_verbose,
158
157
  )
159
158
  from .math import argmax, argmin, argsort, clamp, clip
160
- from .os import get_num_cpus_available, safe_rmdir, tree_iter
159
+ from .os import get_num_cpus_available, remove_children, safe_rmdir, tree_iter
161
160
  from .random import randstr
162
161
  from .re import (
163
162
  PatternLike,
164
163
  PatternListLike,
165
164
  compile_patterns,
165
+ filter_dict_with_patterns,
166
+ filter_iter_with_patterns,
166
167
  filter_with_patterns,
167
168
  find_patterns,
168
169
  get_key_fn,
@@ -175,26 +176,32 @@ if TYPE_CHECKING or lazy is None:
175
176
  dump_json,
176
177
  dump_jsonl,
177
178
  dump_pickle,
179
+ dump_yaml,
178
180
  dumps_csv,
179
181
  dumps_json,
180
182
  dumps_jsonl,
181
183
  dumps_pickle,
184
+ dumps_yaml,
182
185
  load_csv,
183
186
  load_json,
184
187
  load_jsonl,
185
188
  load_pickle,
189
+ load_yaml,
186
190
  loads_csv,
187
191
  loads_json,
188
192
  loads_jsonl,
189
193
  loads_pickle,
194
+ loads_yaml,
190
195
  read_csv,
191
196
  read_json,
192
197
  read_jsonl,
193
198
  read_pickle,
199
+ read_yaml,
194
200
  save_csv,
195
201
  save_json,
196
202
  save_jsonl,
197
203
  save_pickle,
204
+ save_yaml,
198
205
  )
199
206
  from .time import Ticker
200
207
  from .typing import (
@@ -245,6 +252,7 @@ if TYPE_CHECKING or lazy is None:
245
252
  is_special_form,
246
253
  is_typed_dict,
247
254
  isinstance_generic,
255
+ isinstance_parametrized,
248
256
  )
249
257
  from .warnings import deprecated_alias, deprecated_function, warn_once
250
258
 
@@ -370,6 +378,12 @@ else:
370
378
  "save_json",
371
379
  "save_jsonl",
372
380
  "save_pickle",
381
+ "dump_yaml",
382
+ "dumps_yaml",
383
+ "load_yaml",
384
+ "loads_yaml",
385
+ "read_yaml",
386
+ "save_yaml",
373
387
  ],
374
388
  "dataclasses": ["add_dict_methods", "dataclassdict", "get_defaults_values"],
375
389
  "datetime": ["get_now", "get_now_iso8601"],
@@ -384,7 +398,7 @@ else:
384
398
  "identity",
385
399
  "repeat_fn",
386
400
  ],
387
- "hashlib": ["hash_file"],
401
+ "hashlib": ["hash_file", "hash_bytes", "hash_bytes_buffer", "hash_str"],
388
402
  "importlib": [
389
403
  "import_if_available",
390
404
  "ModulePlaceholder",
@@ -414,13 +428,20 @@ else:
414
428
  "setup_logging_verbose",
415
429
  ],
416
430
  "math": ["argmax", "argmin", "argsort", "clamp", "clip"],
417
- "os": ["get_num_cpus_available", "safe_rmdir", "tree_iter"],
431
+ "os": [
432
+ "get_num_cpus_available",
433
+ "remove_children",
434
+ "safe_rmdir",
435
+ "tree_iter",
436
+ ],
418
437
  "random": ["randstr"],
419
438
  "re": [
420
439
  "PatternLike",
421
440
  "PatternListLike",
422
441
  "compile_patterns",
423
442
  "filter_with_patterns",
443
+ "filter_iter_with_patterns",
444
+ "filter_dict_with_patterns",
424
445
  "find_patterns",
425
446
  "get_key_fn",
426
447
  "match_patterns",
@@ -476,6 +497,7 @@ else:
476
497
  "is_special_form",
477
498
  "is_typed_dict",
478
499
  "isinstance_generic",
500
+ "isinstance_parametrized",
479
501
  ],
480
502
  "warnings": ["deprecated_alias", "deprecated_function", "warn_once"],
481
503
  },
pythonwrench/__main__.py CHANGED
@@ -1,5 +1,4 @@
1
1
  #!/usr/bin/env python
2
- # -*- coding: utf-8 -*-
3
2
 
4
3
  from .entrypoints.info import main_info
5
4
 
pythonwrench/_core.py CHANGED
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env python
2
- # -*- coding: utf-8 -*-
3
2
 
4
- from functools import wraps
3
+ import warnings
4
+ from functools import lru_cache, partial, wraps
5
5
  from typing import (
6
6
  Any,
7
7
  Callable,
@@ -11,8 +11,10 @@ from typing import (
11
11
  Optional,
12
12
  Protocol,
13
13
  Tuple,
14
+ Type,
14
15
  Union,
15
16
  get_args,
17
+ overload,
16
18
  runtime_checkable,
17
19
  )
18
20
 
@@ -21,6 +23,7 @@ from typing_extensions import ParamSpec, TypeVar
21
23
  P = ParamSpec("P")
22
24
  T = TypeVar("T", covariant=True)
23
25
  U = TypeVar("U", covariant=True)
26
+
24
27
  T_Output = TypeVar("T_Output", default=Any)
25
28
  T_PredicateArg = TypeVar("T_PredicateArg", contravariant=True, default=Any)
26
29
  T_Function = TypeVar("T_Function", bound=Callable[..., Any])
@@ -38,7 +41,32 @@ class Predicate(Protocol[T_PredicateArg]):
38
41
 
39
42
  def return_none(*args, **kwargs) -> None:
40
43
  """Return None function placeholder."""
41
- return None
44
+ return
45
+
46
+
47
+ def function_alias(
48
+ alternative: T_Function,
49
+ *,
50
+ pre_fn: Optional[Callable[..., Any]] = None,
51
+ post_fn: Optional[Callable[..., Any]] = None,
52
+ ) -> Callable[..., T_Function]:
53
+ """Decorator to wrap function aliases.
54
+
55
+ Unlike setting directly an alias, this preserve the nature of the alias for language server.
56
+
57
+ Example
58
+ -------
59
+ >>> def f(a: int, b: str) -> str:
60
+ >>> return a * b
61
+ >>> @function_alias(f)
62
+ >>> def g(*args, **kwargs): ...
63
+ >>> f(2, "a")
64
+ ... "aa"
65
+ >>> g(3, "b") # calls function f() internally.
66
+ ... "bbb"
67
+
68
+ """
69
+ return _decorator_factory(alternative, pre_fn=pre_fn, post_fn=post_fn)
42
70
 
43
71
 
44
72
  def _decorator_factory(
@@ -133,18 +161,17 @@ class _FunctionRegistry(Generic[T_Output]):
133
161
  unk_mode: UnkMode = "error",
134
162
  **kwargs,
135
163
  ) -> T_Output:
136
- """Perform the apply operation."""
164
+ def wrapped_isinstance(x: Any, target_cls_or_tuple: Any) -> bool:
165
+ return isinstance_fn(x, target_cls_or_tuple)
166
+
137
167
  for fn, (class_or_tuple, custom_predicate, _) in self.fns.items():
138
168
  if custom_predicate is not None:
139
169
  predicate = custom_predicate
140
170
 
141
171
  elif class_or_tuple is not None:
142
-
143
- def target_isinstance_fn_wrap(x: Any) -> bool:
144
- """Perform the target isinstance fn wrap operation."""
145
- return isinstance_fn(x, class_or_tuple) # type: ignore
146
-
147
- predicate = target_isinstance_fn_wrap
172
+ predicate = partial(
173
+ wrapped_isinstance, target_cls_or_tuple=class_or_tuple
174
+ )
148
175
  else:
149
176
  msg = f"Invalid function registered. (found {class_or_tuple=} and {custom_predicate=})"
150
177
  raise TypeError(msg)
@@ -190,3 +217,104 @@ def _get_insertion_index(lst: list, priority: int, priority_key: int) -> int:
190
217
  if priority >= other_priority:
191
218
  return i
192
219
  return len(lst)
220
+
221
+
222
+ @overload
223
+ def warn_once(
224
+ message: str,
225
+ category: Optional[Type[Warning]] = None,
226
+ stacklevel: int = 1,
227
+ source: Any = None,
228
+ ) -> None:
229
+ """Warn message once using warnings module."""
230
+
231
+
232
+ @overload
233
+ def warn_once(
234
+ message: Warning,
235
+ category: Any = None,
236
+ stacklevel: int = 1,
237
+ source: Any = None,
238
+ ) -> None:
239
+ """Warn message once using warnings module."""
240
+
241
+
242
+ @lru_cache(maxsize=None)
243
+ def warn_once(
244
+ message: Union[str, Warning],
245
+ category: Optional[Type[Warning]] = None,
246
+ stacklevel: int = 1,
247
+ source: Any = None,
248
+ ) -> None:
249
+ """Warn message once using warnings module."""
250
+ warnings.warn(message, category, stacklevel, source)
251
+
252
+
253
+ def deprecated_alias(
254
+ alternative: T_Function,
255
+ msg_fmt: str = "Deprecated call to '{fn_name}', use '{alternative_name}' instead.",
256
+ warn_fn: Callable[[str], Any] = partial(warn_once, category=DeprecationWarning),
257
+ *,
258
+ pre_fn: Optional[Callable[..., Any]] = None,
259
+ post_fn: Optional[Callable[..., Any]] = None,
260
+ ) -> Callable[..., T_Function]:
261
+ """Decorator to wrap deprecated function aliases."""
262
+ alternative_name = alternative.__name__ if alternative is not None else "None"
263
+ if pre_fn is None:
264
+ pre_fn = return_none
265
+
266
+ def inner_pre_fn(fn, *args, **kwargs) -> None:
267
+ """Perform the inner pre fn operation."""
268
+ msg = msg_fmt.format(fn_name=fn.__name__, alternative_name=alternative_name)
269
+ warn_fn(msg)
270
+ pre_fn(fn, *args, **kwargs)
271
+
272
+ return _decorator_factory(alternative, pre_fn=inner_pre_fn, post_fn=post_fn)
273
+
274
+
275
+ @overload
276
+ def deprecated_function(
277
+ fn: None = None,
278
+ /,
279
+ *,
280
+ msg_fmt: str = "Deprecated call to '{fn_name}'.",
281
+ warn_fn: Callable[[str], Any] = partial(warn_once, category=DeprecationWarning),
282
+ ) -> Callable[[T_Function], T_Function]:
283
+ """Perform the deprecated function operation."""
284
+
285
+
286
+ @overload
287
+ def deprecated_function(
288
+ fn: T_Function,
289
+ /,
290
+ *,
291
+ msg_fmt: str = "Deprecated call to '{fn_name}'.",
292
+ warn_fn: Callable[[str], Any] = partial(warn_once, category=DeprecationWarning),
293
+ ) -> T_Function:
294
+ """Perform the deprecated function operation."""
295
+
296
+
297
+ def deprecated_function(
298
+ fn: Optional[T_Function] = None,
299
+ /,
300
+ *,
301
+ msg_fmt: str = "Deprecated call to '{fn_name}'.",
302
+ warn_fn: Callable[[str], Any] = partial(warn_once, category=DeprecationWarning),
303
+ pre_fn: Optional[Callable[..., Any]] = None,
304
+ post_fn: Optional[Callable[..., Any]] = None,
305
+ ) -> Union[Callable[[T_Function], T_Function], T_Function]:
306
+ """Decorator to wrap deprecated functions."""
307
+ if pre_fn is None:
308
+ pre_fn = return_none
309
+
310
+ def inner_pre_fn(fn, *args, **kwargs):
311
+ """Perform the inner pre fn operation."""
312
+ msg = msg_fmt.format(fn_name=fn.__qualname__)
313
+ warn_fn(msg)
314
+ pre_fn(fn, *args, **kwargs)
315
+
316
+ decorator = _decorator_factory(None, pre_fn=inner_pre_fn, post_fn=post_fn)
317
+ if fn is None:
318
+ return decorator
319
+ else:
320
+ return decorator(fn)
pythonwrench/abc.py CHANGED
@@ -1,5 +1,4 @@
1
1
  #!/usr/bin/env python
2
- # -*- coding: utf-8 -*-
3
2
 
4
3
  from typing import Any, ClassVar, Dict, Type
5
4
 
@@ -1,5 +1,4 @@
1
1
  #!/usr/bin/env python
2
- # -*- coding: utf-8 -*-
3
2
 
4
3
  from typing import TYPE_CHECKING
5
4