nshutils 0.3.0__tar.gz → 0.4.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.3.0
3
+ Version: 0.4.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.3.0"
3
+ version = "0.4.0"
4
4
  description = ""
5
5
  authors = ["Nima Shoghi <nimashoghi@gmail.com>"]
6
6
  readme = "README.md"
@@ -0,0 +1,6 @@
1
+ from . import actsave as actsave
2
+ from . import typecheck as typecheck
3
+ from .logging import init_python_logging as init_python_logging
4
+ from .logging import lovely as lovely
5
+ from .logging import pretty as pretty
6
+ from .snoop import snoop as snoop
@@ -0,0 +1,88 @@
1
+ import logging
2
+ from pathlib import Path
3
+
4
+
5
+ def init_python_logging(
6
+ *,
7
+ lovely_tensors: bool = False,
8
+ lovely_numpy: bool = False,
9
+ rich: bool = False,
10
+ log_level: int | str | None = logging.INFO,
11
+ log_save_dir: Path | None = None,
12
+ ):
13
+ if lovely_tensors:
14
+ try:
15
+ import lovely_tensors as _lovely_tensors
16
+
17
+ _lovely_tensors.monkey_patch()
18
+ except ImportError:
19
+ logging.warning(
20
+ "Failed to import `lovely_tensors`. Ignoring pretty PyTorch tensor formatting"
21
+ )
22
+
23
+ if lovely_numpy:
24
+ try:
25
+ import lovely_numpy as _lovely_numpy
26
+
27
+ _lovely_numpy.set_config(repr=_lovely_numpy.lovely)
28
+ except ImportError:
29
+ logging.warning(
30
+ "Failed to import `lovely_numpy`. Ignoring pretty numpy array formatting"
31
+ )
32
+
33
+ log_handlers: list[logging.Handler] = []
34
+ if log_save_dir:
35
+ log_file = log_save_dir / "logging.log"
36
+ log_file.touch(exist_ok=True)
37
+ log_handlers.append(logging.FileHandler(log_file))
38
+
39
+ if rich:
40
+ try:
41
+ from rich.logging import RichHandler
42
+
43
+ log_handlers.append(RichHandler())
44
+ except ImportError:
45
+ logging.warning(
46
+ "Failed to import rich. Falling back to default Python logging."
47
+ )
48
+
49
+ logging.basicConfig(
50
+ level=log_level,
51
+ format="%(message)s",
52
+ datefmt="[%X]",
53
+ handlers=log_handlers,
54
+ )
55
+
56
+
57
+ def pretty(
58
+ *,
59
+ lovely_tensors: bool = True,
60
+ lovely_numpy: bool = True,
61
+ log_level: int | str | None = logging.INFO,
62
+ log_save_dir: Path | None = None,
63
+ rich_log_handler: bool = True,
64
+ ):
65
+ init_python_logging(
66
+ lovely_tensors=lovely_tensors,
67
+ lovely_numpy=lovely_numpy,
68
+ rich=rich_log_handler,
69
+ log_level=log_level,
70
+ log_save_dir=log_save_dir,
71
+ )
72
+
73
+
74
+ def lovely(
75
+ *,
76
+ lovely_tensors: bool = True,
77
+ lovely_numpy: bool = True,
78
+ log_level: int | str | None = logging.INFO,
79
+ log_save_dir: Path | None = None,
80
+ rich_log_handler: bool = True,
81
+ ):
82
+ pretty(
83
+ lovely_tensors=lovely_tensors,
84
+ lovely_numpy=lovely_numpy,
85
+ log_level=log_level,
86
+ log_save_dir=log_save_dir,
87
+ rich_log_handler=rich_log_handler,
88
+ )
@@ -1,3 +0,0 @@
1
- from . import actsave as actsave
2
- from . import typecheck as typecheck
3
- from .snoop import snoop as snoop
File without changes
File without changes