nshutils 0.22.2__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.
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.2
3
+ Version: 0.22.3
4
4
  Summary:
5
5
  Author: Nima Shoghi
6
6
  Author-email: nimashoghi@gmail.com
@@ -11,12 +11,12 @@ nshutils/lovely/_base.py,sha256=c2XxNJlEdET2mP2gLzlYY1KHsEN4H9eDD_x8SptuBTA,4277
11
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.2.dist-info/METADATA,sha256=Fyf30EVxNxg10Vrrg3lEXnTXA5aByv2UBtMqBS6LEg4,4406
21
- nshutils-0.22.2.dist-info/WHEEL,sha256=b4K_helf-jlQoXBBETfwnf4B04YC67LOev0jo4fX5m8,88
22
- nshutils-0.22.2.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,,