nshutils 0.15.0__tar.gz → 0.16.0__tar.gz

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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: nshutils
3
- Version: 0.15.0
3
+ Version: 0.16.0
4
4
  Summary:
5
5
  Author: Nima Shoghi
6
6
  Author-email: nimashoghi@gmail.com
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "nshutils"
3
- version = "0.15.0"
3
+ version = "0.16.0"
4
4
  description = ""
5
5
  authors = ["Nima Shoghi <nimashoghi@gmail.com>"]
6
6
  readme = "README.md"
@@ -1,8 +1,12 @@
1
1
  from . import actsave as actsave
2
2
  from . import typecheck as typecheck
3
+ from ._display import display as display
3
4
  from .actsave import ActLoad as ActLoad
4
5
  from .actsave import ActSave as ActSave
5
6
  from .logging import init_python_logging as init_python_logging
6
7
  from .logging import lovely as lovely
7
8
  from .logging import pretty as pretty
8
9
  from .snoop import snoop as snoop
10
+ from .typecheck import tassert as tassert
11
+ from .typecheck import typecheck_modules as typecheck_modules
12
+ from .typecheck import typecheck_this_module as typecheck_this_module
@@ -0,0 +1,60 @@
1
+ import importlib.util
2
+ from functools import cache
3
+ from typing import Any
4
+
5
+
6
+ @cache
7
+ def _in_ipython():
8
+ try:
9
+ from IPython import get_ipython
10
+
11
+ return get_ipython() is not None
12
+ except ImportError:
13
+ return False
14
+
15
+
16
+ @cache
17
+ def _treescope_installed():
18
+ return importlib.util.find_spec("treescope") is not None
19
+
20
+
21
+ @cache
22
+ def _rich_installed():
23
+ return importlib.util.find_spec("rich") is not None
24
+
25
+
26
+ def display(*args: Any):
27
+ """
28
+ Display the given arguments in the current environment.
29
+
30
+ If executed in an IPython environment, the display will be handled
31
+ by treescope if installed, or rich if available. If neither are
32
+ installed, it will fall back to IPython's display function. In a
33
+ non-IPython environment, rich will be used if available, otherwise
34
+ the standard print function will be used.
35
+
36
+ Args:
37
+ *args: Any objects to display.
38
+ """
39
+ if _in_ipython():
40
+ if _treescope_installed():
41
+ import treescope
42
+
43
+ with treescope.active_autovisualizer.set_scoped(
44
+ treescope.ArrayAutovisualizer()
45
+ ):
46
+ treescope.display(*args)
47
+ elif _rich_installed():
48
+ import rich
49
+
50
+ rich.print(*args)
51
+ else:
52
+ from IPython.display import display
53
+
54
+ display(*args)
55
+ elif _rich_installed():
56
+ import rich
57
+
58
+ rich.print(*args)
59
+ else:
60
+ print(*args)
File without changes