nshutils 0.22.1__py3-none-any.whl → 0.22.3__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.
@@ -41,21 +41,20 @@ def monkey_patch(libraries: list[Library] | Literal["auto"] = "auto"):
41
41
 
42
42
  with contextlib.ExitStack() as stack:
43
43
  for library in libraries:
44
- match library:
45
- case "torch":
46
- from .torch_ import torch_monkey_patch
44
+ if library == "torch":
45
+ from .torch_ import torch_monkey_patch
47
46
 
48
- stack.enter_context(torch_monkey_patch())
49
- case "jax":
50
- from .jax_ import jax_monkey_patch
47
+ stack.enter_context(torch_monkey_patch())
48
+ elif library == "jax":
49
+ from .jax_ import jax_monkey_patch
51
50
 
52
- stack.enter_context(jax_monkey_patch())
53
- case "numpy":
54
- from .numpy_ import numpy_monkey_patch
51
+ stack.enter_context(jax_monkey_patch())
52
+ elif library == "numpy":
53
+ from .numpy_ import numpy_monkey_patch
55
54
 
56
- stack.enter_context(numpy_monkey_patch())
57
- case _:
58
- assert_never(library)
55
+ stack.enter_context(numpy_monkey_patch())
56
+ else:
57
+ assert_never(library)
59
58
 
60
59
  log.info(
61
60
  f"Monkey patched libraries: {', '.join(libraries)}. "
nshutils/lovely/numpy_.py CHANGED
@@ -8,6 +8,18 @@ from ._base import lovely_repr, monkey_patch_contextmanager
8
8
  from .utils import LovelyStats, array_stats
9
9
 
10
10
 
11
+ def _np_ge_2():
12
+ import importlib.metadata
13
+
14
+ from packaging.version import Version
15
+
16
+ try:
17
+ numpy_version = importlib.metadata.version("numpy")
18
+ return Version(numpy_version) >= Version("2.0")
19
+ except importlib.metadata.PackageNotFoundError:
20
+ return False
21
+
22
+
11
23
  def _type_name(array: np.ndarray):
12
24
  return (
13
25
  "array"
@@ -55,18 +67,43 @@ def numpy_repr(array: np.ndarray) -> LovelyStats:
55
67
  }
56
68
 
57
69
 
58
- @monkey_patch_contextmanager(dependencies=["numpy"])
59
- def numpy_monkey_patch():
60
- try:
61
- np.set_printoptions(override_repr=numpy_repr)
62
- logging.info(
63
- f"Numpy monkey patching: using {numpy_repr.__name__} for numpy arrays. "
64
- f"{np.get_printoptions()=}"
65
- )
66
- yield
67
- finally:
68
- np.set_printoptions(override_repr=None)
69
- logging.info(
70
- f"Numpy unmonkey patching: using {numpy_repr.__name__} for numpy arrays. "
71
- f"{np.get_printoptions()=}"
72
- )
70
+ # If numpy 2.0, use the new API override_repr.
71
+ if _np_ge_2():
72
+
73
+ @monkey_patch_contextmanager(dependencies=["numpy"])
74
+ def numpy_monkey_patch():
75
+ try:
76
+ np.set_printoptions(override_repr=numpy_repr)
77
+ logging.info(
78
+ f"Numpy monkey patching: using {numpy_repr.__name__} for numpy arrays. "
79
+ f"{np.get_printoptions()=}"
80
+ )
81
+ yield
82
+ finally:
83
+ np.set_printoptions(override_repr=None)
84
+ logging.info(
85
+ f"Numpy unmonkey patching: using {numpy_repr.__name__} for numpy arrays. "
86
+ f"{np.get_printoptions()=}"
87
+ )
88
+
89
+ else:
90
+
91
+ @monkey_patch_contextmanager(dependencies=["numpy"])
92
+ def numpy_monkey_patch():
93
+ try:
94
+ np.set_string_function(numpy_repr, True) # pyright: ignore[reportAttributeAccessIssue]
95
+ np.set_string_function(numpy_repr, False) # pyright: ignore[reportAttributeAccessIssue]
96
+
97
+ logging.info(
98
+ f"Numpy monkey patching: using {numpy_repr.__name__} for numpy arrays. "
99
+ f"{np.get_printoptions()=}"
100
+ )
101
+ yield
102
+ finally:
103
+ np.set_string_function(None, True) # pyright: ignore[reportAttributeAccessIssue]
104
+ np.set_string_function(None, False) # pyright: ignore[reportAttributeAccessIssue]
105
+
106
+ logging.info(
107
+ f"Numpy unmonkey patching: using {numpy_repr.__name__} for numpy arrays. "
108
+ f"{np.get_printoptions()=}"
109
+ )
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: nshutils
3
- Version: 0.22.1
3
+ Version: 0.22.3
4
4
  Summary:
5
5
  Author: Nima Shoghi
6
6
  Author-email: nimashoghi@gmail.com
@@ -8,15 +8,15 @@ nshutils/display.py,sha256=Ge63yllx7gi-MKL3mKQeQ5doql_nj56-o5aoTVmusDg,1473
8
8
  nshutils/logging.py,sha256=78pv3-I_gmbKSf5_mYYBr6_H4GNBGErghAdhH9wfYIc,2205
9
9
  nshutils/lovely/__init__.py,sha256=gbWMNs7xfK1CiNdkHvfH0KcyaGjdZ8_WUBGfaEUDN4I,451
10
10
  nshutils/lovely/_base.py,sha256=c2XxNJlEdET2mP2gLzlYY1KHsEN4H9eDD_x8SptuBTA,4277
11
- nshutils/lovely/_monkey_patch_all.py,sha256=WZsC6Xp5-Z2GBd6xyZZEsD4C2xNyZy0YBfjZzAy3m8M,2028
11
+ nshutils/lovely/_monkey_patch_all.py,sha256=zgMupp2Wc_O9R3arl-BAIePpvQSi6TCeshGMaui-Cc8,1986
12
12
  nshutils/lovely/config.py,sha256=lVNMuU1oUvsYlGN0Sn-m6iOLbJIchVnWDpyHm09nWo8,1224
13
13
  nshutils/lovely/jax_.py,sha256=mPH-tSOzWE27ymupllBnO4O6avvJJxpF2a1-G4dN5Ow,2214
14
- nshutils/lovely/numpy_.py,sha256=iHA4BCJIW9IU6DXKEfYbh9RA2xyeXeL0tyHdEja89Sw,1866
14
+ nshutils/lovely/numpy_.py,sha256=CI6pidaohK0tFw9mmSBTaDfjCKrFhY87ga0RIqTqSFk,3162
15
15
  nshutils/lovely/torch_.py,sha256=3wnXLa-1xwuQVk1fM50mBqOVDv5wZHjfnBwzOnjcFjg,2638
16
16
  nshutils/lovely/utils.py,sha256=2ksT5YGVViFuWc8jSkwVCsABripJmyVJdEDDH7aab70,10459
17
17
  nshutils/snoop.py,sha256=7d7_Q5sJmINL1J29wcnxEvpV95zvZYNoVn5frCq-rww,7393
18
18
  nshutils/typecheck.py,sha256=Gi7xtfilN_UwZ1FTFqBVKDhcQzBEDonVxIv3bUj-uXY,5582
19
19
  nshutils/util.py,sha256=tx-XiRbOrpafV3OkJDE5IVFtzn3kN7uSZ8FkMor0H5c,2845
20
- nshutils-0.22.1.dist-info/METADATA,sha256=GHCw6WkX4yi-KGaDXf-EVgee6AfYnUPD4Z452Wq14rc,4406
21
- nshutils-0.22.1.dist-info/WHEEL,sha256=b4K_helf-jlQoXBBETfwnf4B04YC67LOev0jo4fX5m8,88
22
- nshutils-0.22.1.dist-info/RECORD,,
20
+ nshutils-0.22.3.dist-info/METADATA,sha256=Or1chA_NrLS05hiiqbXaj_ph8tZ91TwRT1A5iBfTOW8,4406
21
+ nshutils-0.22.3.dist-info/WHEEL,sha256=b4K_helf-jlQoXBBETfwnf4B04YC67LOev0jo4fX5m8,88
22
+ nshutils-0.22.3.dist-info/RECORD,,