dttb 0.3.1rc1__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.
dttb-0.3.1rc1/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Rocky Haotian Du
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
dttb-0.3.1rc1/PKG-INFO ADDED
@@ -0,0 +1,193 @@
1
+ Metadata-Version: 2.4
2
+ Name: dttb
3
+ Version: 0.3.1rc1
4
+ Summary: DateTime TraceBack
5
+ Author: Rocky Haotian Du
6
+ Author-email: Rocky Haotian Du <2712479005@qq.com>
7
+ License-Expression: MIT
8
+ License-File: LICENSE
9
+ Classifier: Development Status :: 4 - Beta
10
+ Classifier: Intended Audience :: Developers
11
+ Classifier: License :: OSI Approved :: MIT License
12
+ Classifier: Operating System :: OS Independent
13
+ Classifier: Programming Language :: Python
14
+ Classifier: Programming Language :: Python :: 3 :: Only
15
+ Classifier: Programming Language :: Python :: 3.8
16
+ Classifier: Programming Language :: Python :: 3.9
17
+ Classifier: Programming Language :: Python :: 3.10
18
+ Classifier: Programming Language :: Python :: 3.11
19
+ Classifier: Programming Language :: Python :: 3.12
20
+ Classifier: Programming Language :: Python :: 3.13
21
+ Classifier: Programming Language :: Python :: 3.14
22
+ Requires-Python: >=3.8
23
+ Project-URL: Repository, https://github.com/rocky-d/dttb
24
+ Description-Content-Type: text/markdown
25
+
26
+ # dttb
27
+
28
+ [![license](https://img.shields.io/github/license/rocky-d/dttb?logo=github&logoColor=white)](https://github.com/rocky-d/dttb/blob/main/LICENSE)
29
+ [![lint](https://img.shields.io/github/actions/workflow/status/rocky-d/dttb/lint.yml?logo=githubactions&logoColor=white&label=lint)](https://github.com/rocky-d/dttb/actions/workflows/lint.yml)
30
+ [![test](https://img.shields.io/github/actions/workflow/status/rocky-d/dttb/test.yml?logo=githubactions&logoColor=white&label=test)](https://github.com/rocky-d/dttb/actions/workflows/test.yml)
31
+ [![codecov](https://img.shields.io/codecov/c/github/rocky-d/dttb?logo=codecov&logoColor=white)](https://codecov.io/gh/rocky-d/dttb)
32
+ [![pypi-v](https://img.shields.io/pypi/v/dttb?logo=pypi&logoColor=white)](https://pypi.org/project/dttb/)
33
+ [![pypi-dm](https://img.shields.io/pypi/dm/dttb?logo=pypi&logoColor=white)](https://pypi.org/project/dttb/)
34
+ [![sponsors](https://img.shields.io/github/sponsors/rocky-d?logo=githubsponsors&logoColor=white)](https://github.com/sponsors/rocky-d)
35
+
36
+ [![python](https://img.shields.io/python/required-version-toml?tomlFilePath=https%3A%2F%2Fraw.githubusercontent.com%2Frocky-d%2Fdttb%2Fmain%2Fpyproject.toml&logo=python&logoColor=white)](https://www.python.org/)
37
+ [![uv](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/uv/main/assets/badge/v0.json)](https://github.com/astral-sh/uv)
38
+ [![ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json)](https://github.com/astral-sh/ruff)
39
+ [![ty](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ty/main/assets/badge/v0.json)](https://github.com/astral-sh/ty)
40
+
41
+ **D**ate**T**ime **T**race**B**ack
42
+
43
+ ## Installation
44
+
45
+ - **uv**
46
+
47
+ ```sh
48
+ uv add dttb
49
+ ```
50
+
51
+ - **pip**
52
+
53
+ ```sh
54
+ pip install dttb
55
+ ```
56
+
57
+ ## Usage
58
+
59
+ ### Apply
60
+
61
+ **`demo1.py`**
62
+
63
+ ```python
64
+ import dttb
65
+
66
+ dttb.apply()
67
+
68
+ 1 / 0
69
+ ```
70
+
71
+ ```text
72
+ [2026-01-15 22:37:09.882049]
73
+ Traceback (most recent call last):
74
+ File "demo1.py", line 5, in <module>
75
+ 1 / 0
76
+ ZeroDivisionError: division by zero
77
+ ```
78
+
79
+ ### Reset
80
+
81
+ **`demo2.py`**
82
+
83
+ ```python
84
+ import dttb
85
+
86
+ dttb.apply()
87
+ dttb.reset()
88
+
89
+ 1 / 0
90
+ ```
91
+
92
+ ```text
93
+ Traceback (most recent call last):
94
+ File "demo2.py", line 4, in <module>
95
+ 1 / 0
96
+ ZeroDivisionError: division by zero
97
+ ```
98
+
99
+ ### With *logging*
100
+
101
+ **`demo3.py`**
102
+
103
+ ```python
104
+ import dttb
105
+ import logging
106
+
107
+ dttb_logger = logging.getLogger("dttb")
108
+ dttb_logger.propagate = False
109
+ dttb_logger.setLevel(logging.ERROR)
110
+ file_handler = logging.FileHandler("logging.log")
111
+ file_handler.setFormatter(logging.Formatter("%(asctime)s | %(levelname)s | %(message)s"))
112
+ dttb_logger.addHandler(file_handler)
113
+
114
+ dttb.apply()
115
+
116
+ 1 / 0
117
+ ```
118
+
119
+ ```text
120
+ [2026-01-19 23:29:35.817684]
121
+ Traceback (most recent call last):
122
+ File "demo3.py", line 13, in <module>
123
+ 1 / 0
124
+ ZeroDivisionError: division by zero
125
+ ```
126
+
127
+ **`logging.log`**
128
+
129
+ ```log
130
+ 2026-01-19 23:29:35,817 | ERROR | An uncaught exception logged by dttb:
131
+ [2026-01-19 23:29:35.817684]
132
+ Traceback (most recent call last):
133
+ File "demo3.py", line 13, in <module>
134
+ 1 / 0
135
+ ZeroDivisionError: division by zero
136
+ ```
137
+
138
+ ### With *threading*
139
+
140
+ **`demo4.py`**
141
+
142
+ ```python
143
+ import dttb
144
+ import threading
145
+ import time
146
+
147
+ dttb.apply()
148
+
149
+
150
+ def func(seconds):
151
+ time.sleep(seconds)
152
+ seconds / 0
153
+
154
+
155
+ thread1 = threading.Thread(target=func, kwargs={"seconds": 1})
156
+ thread2 = threading.Thread(target=func, kwargs={"seconds": 2})
157
+ thread1.start()
158
+ thread2.start()
159
+ thread1.join()
160
+ thread2.join()
161
+
162
+ func(seconds=3)
163
+ ```
164
+
165
+ ```text
166
+ [2026-01-15 23:00:03.048290]
167
+ Exception in thread Thread-1:
168
+ Traceback (most recent call last):
169
+ File "/**/threading.py", line 932, in _bootstrap_inner
170
+ self.run()
171
+ File "/**/threading.py", line 870, in run
172
+ self._target(*self._args, **self._kwargs)
173
+ File "demo4.py", line 10, in func
174
+ seconds / 0
175
+ ZeroDivisionError: division by zero
176
+ [2026-01-15 23:00:04.048362]
177
+ Exception in thread Thread-2:
178
+ Traceback (most recent call last):
179
+ File "/**/threading.py", line 932, in _bootstrap_inner
180
+ self.run()
181
+ File "/**/threading.py", line 870, in run
182
+ self._target(*self._args, **self._kwargs)
183
+ File "demo4.py", line 10, in func
184
+ seconds / 0
185
+ ZeroDivisionError: division by zero
186
+ [2026-01-15 23:00:07.055083]
187
+ Traceback (most recent call last):
188
+ File "demo4.py", line 20, in <module>
189
+ func(seconds=3)
190
+ File "demo4.py", line 10, in func
191
+ seconds / 0
192
+ ZeroDivisionError: division by zero
193
+ ```
@@ -0,0 +1,168 @@
1
+ # dttb
2
+
3
+ [![license](https://img.shields.io/github/license/rocky-d/dttb?logo=github&logoColor=white)](https://github.com/rocky-d/dttb/blob/main/LICENSE)
4
+ [![lint](https://img.shields.io/github/actions/workflow/status/rocky-d/dttb/lint.yml?logo=githubactions&logoColor=white&label=lint)](https://github.com/rocky-d/dttb/actions/workflows/lint.yml)
5
+ [![test](https://img.shields.io/github/actions/workflow/status/rocky-d/dttb/test.yml?logo=githubactions&logoColor=white&label=test)](https://github.com/rocky-d/dttb/actions/workflows/test.yml)
6
+ [![codecov](https://img.shields.io/codecov/c/github/rocky-d/dttb?logo=codecov&logoColor=white)](https://codecov.io/gh/rocky-d/dttb)
7
+ [![pypi-v](https://img.shields.io/pypi/v/dttb?logo=pypi&logoColor=white)](https://pypi.org/project/dttb/)
8
+ [![pypi-dm](https://img.shields.io/pypi/dm/dttb?logo=pypi&logoColor=white)](https://pypi.org/project/dttb/)
9
+ [![sponsors](https://img.shields.io/github/sponsors/rocky-d?logo=githubsponsors&logoColor=white)](https://github.com/sponsors/rocky-d)
10
+
11
+ [![python](https://img.shields.io/python/required-version-toml?tomlFilePath=https%3A%2F%2Fraw.githubusercontent.com%2Frocky-d%2Fdttb%2Fmain%2Fpyproject.toml&logo=python&logoColor=white)](https://www.python.org/)
12
+ [![uv](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/uv/main/assets/badge/v0.json)](https://github.com/astral-sh/uv)
13
+ [![ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json)](https://github.com/astral-sh/ruff)
14
+ [![ty](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ty/main/assets/badge/v0.json)](https://github.com/astral-sh/ty)
15
+
16
+ **D**ate**T**ime **T**race**B**ack
17
+
18
+ ## Installation
19
+
20
+ - **uv**
21
+
22
+ ```sh
23
+ uv add dttb
24
+ ```
25
+
26
+ - **pip**
27
+
28
+ ```sh
29
+ pip install dttb
30
+ ```
31
+
32
+ ## Usage
33
+
34
+ ### Apply
35
+
36
+ **`demo1.py`**
37
+
38
+ ```python
39
+ import dttb
40
+
41
+ dttb.apply()
42
+
43
+ 1 / 0
44
+ ```
45
+
46
+ ```text
47
+ [2026-01-15 22:37:09.882049]
48
+ Traceback (most recent call last):
49
+ File "demo1.py", line 5, in <module>
50
+ 1 / 0
51
+ ZeroDivisionError: division by zero
52
+ ```
53
+
54
+ ### Reset
55
+
56
+ **`demo2.py`**
57
+
58
+ ```python
59
+ import dttb
60
+
61
+ dttb.apply()
62
+ dttb.reset()
63
+
64
+ 1 / 0
65
+ ```
66
+
67
+ ```text
68
+ Traceback (most recent call last):
69
+ File "demo2.py", line 4, in <module>
70
+ 1 / 0
71
+ ZeroDivisionError: division by zero
72
+ ```
73
+
74
+ ### With *logging*
75
+
76
+ **`demo3.py`**
77
+
78
+ ```python
79
+ import dttb
80
+ import logging
81
+
82
+ dttb_logger = logging.getLogger("dttb")
83
+ dttb_logger.propagate = False
84
+ dttb_logger.setLevel(logging.ERROR)
85
+ file_handler = logging.FileHandler("logging.log")
86
+ file_handler.setFormatter(logging.Formatter("%(asctime)s | %(levelname)s | %(message)s"))
87
+ dttb_logger.addHandler(file_handler)
88
+
89
+ dttb.apply()
90
+
91
+ 1 / 0
92
+ ```
93
+
94
+ ```text
95
+ [2026-01-19 23:29:35.817684]
96
+ Traceback (most recent call last):
97
+ File "demo3.py", line 13, in <module>
98
+ 1 / 0
99
+ ZeroDivisionError: division by zero
100
+ ```
101
+
102
+ **`logging.log`**
103
+
104
+ ```log
105
+ 2026-01-19 23:29:35,817 | ERROR | An uncaught exception logged by dttb:
106
+ [2026-01-19 23:29:35.817684]
107
+ Traceback (most recent call last):
108
+ File "demo3.py", line 13, in <module>
109
+ 1 / 0
110
+ ZeroDivisionError: division by zero
111
+ ```
112
+
113
+ ### With *threading*
114
+
115
+ **`demo4.py`**
116
+
117
+ ```python
118
+ import dttb
119
+ import threading
120
+ import time
121
+
122
+ dttb.apply()
123
+
124
+
125
+ def func(seconds):
126
+ time.sleep(seconds)
127
+ seconds / 0
128
+
129
+
130
+ thread1 = threading.Thread(target=func, kwargs={"seconds": 1})
131
+ thread2 = threading.Thread(target=func, kwargs={"seconds": 2})
132
+ thread1.start()
133
+ thread2.start()
134
+ thread1.join()
135
+ thread2.join()
136
+
137
+ func(seconds=3)
138
+ ```
139
+
140
+ ```text
141
+ [2026-01-15 23:00:03.048290]
142
+ Exception in thread Thread-1:
143
+ Traceback (most recent call last):
144
+ File "/**/threading.py", line 932, in _bootstrap_inner
145
+ self.run()
146
+ File "/**/threading.py", line 870, in run
147
+ self._target(*self._args, **self._kwargs)
148
+ File "demo4.py", line 10, in func
149
+ seconds / 0
150
+ ZeroDivisionError: division by zero
151
+ [2026-01-15 23:00:04.048362]
152
+ Exception in thread Thread-2:
153
+ Traceback (most recent call last):
154
+ File "/**/threading.py", line 932, in _bootstrap_inner
155
+ self.run()
156
+ File "/**/threading.py", line 870, in run
157
+ self._target(*self._args, **self._kwargs)
158
+ File "demo4.py", line 10, in func
159
+ seconds / 0
160
+ ZeroDivisionError: division by zero
161
+ [2026-01-15 23:00:07.055083]
162
+ Traceback (most recent call last):
163
+ File "demo4.py", line 20, in <module>
164
+ func(seconds=3)
165
+ File "demo4.py", line 10, in func
166
+ seconds / 0
167
+ ZeroDivisionError: division by zero
168
+ ```
@@ -0,0 +1,41 @@
1
+ [build-system]
2
+ requires = ["uv_build>=0.9.17,<0.10.0"]
3
+ build-backend = "uv_build"
4
+
5
+ [project]
6
+ name = "dttb"
7
+ version = "0.3.1rc1"
8
+ description = "DateTime TraceBack"
9
+ readme = "README.md"
10
+ license = "MIT"
11
+ license-files = ["LICENSE"]
12
+ authors = [{ name = "Rocky Haotian Du", email = "2712479005@qq.com" }]
13
+ requires-python = ">=3.8"
14
+ classifiers = [
15
+ "Development Status :: 4 - Beta",
16
+ "Intended Audience :: Developers",
17
+ "License :: OSI Approved :: MIT License",
18
+ "Operating System :: OS Independent",
19
+ "Programming Language :: Python",
20
+ "Programming Language :: Python :: 3 :: Only",
21
+ "Programming Language :: Python :: 3.8",
22
+ "Programming Language :: Python :: 3.9",
23
+ "Programming Language :: Python :: 3.10",
24
+ "Programming Language :: Python :: 3.11",
25
+ "Programming Language :: Python :: 3.12",
26
+ "Programming Language :: Python :: 3.13",
27
+ "Programming Language :: Python :: 3.14",
28
+ ]
29
+ dependencies = []
30
+
31
+ [project.urls]
32
+ Repository = "https://github.com/rocky-d/dttb"
33
+
34
+ [dependency-groups]
35
+ dev = [
36
+ "coverage>=7.6.1",
37
+ "pyyaml>=6.0.3",
38
+ "ruff>=0.14.11",
39
+ "ty>=0.0.11",
40
+ "typing-extensions>=4.13.2",
41
+ ]
@@ -0,0 +1,123 @@
1
+ """
2
+ DateTime TraceBack
3
+
4
+ PyPI: https://pypi.org/project/dttb/
5
+ GitHub: https://github.com/rocky-d/dttb
6
+ """
7
+
8
+ from __future__ import annotations
9
+
10
+ import datetime as dt
11
+ import functools
12
+ import logging
13
+ import sys
14
+ import threading
15
+ from threading import ExceptHookArgs
16
+ from types import TracebackType
17
+ from typing import Any, Callable, Optional, Type
18
+
19
+ __all__ = [
20
+ "apply",
21
+ "reset",
22
+ ]
23
+
24
+
25
+ _logger = logging.getLogger(__name__)
26
+ _logger.addHandler(logging.NullHandler())
27
+
28
+ _sys_excepthook = sys.excepthook
29
+ _threading_excepthook = threading.excepthook
30
+
31
+ _SysExcepthook = Callable[
32
+ [Type[BaseException], BaseException, Optional[TracebackType]],
33
+ Any,
34
+ ]
35
+ _ThreadingExcepthook = Callable[
36
+ [ExceptHookArgs],
37
+ object,
38
+ ]
39
+
40
+
41
+ def _now(
42
+ tz: Optional[dt.tzinfo] = None,
43
+ ) -> dt.datetime:
44
+ return dt.datetime.now(tz=tz)
45
+
46
+
47
+ def _print_dt(
48
+ now: dt.datetime,
49
+ ) -> None:
50
+ print(f"[{now}]", file=sys.stderr)
51
+
52
+
53
+ def _log_dttb(
54
+ now: dt.datetime,
55
+ exc_value: Optional[BaseException],
56
+ ) -> None:
57
+ _logger.error(
58
+ f"An uncaught exception logged by dttb:\n[{now}]\n",
59
+ exc_info=exc_value,
60
+ )
61
+
62
+
63
+ def _dttb_sys_excepthook(
64
+ tz: Optional[dt.tzinfo] = None,
65
+ ) -> Callable[[_SysExcepthook], _SysExcepthook]:
66
+ def decorator(
67
+ func: _SysExcepthook,
68
+ ) -> _SysExcepthook:
69
+ @functools.wraps(func)
70
+ def wrapper(
71
+ exc_type: Type[BaseException],
72
+ exc_value: BaseException,
73
+ exc_traceback: Optional[TracebackType],
74
+ ) -> Any:
75
+ now = _now(tz=tz)
76
+ _log_dttb(now, exc_value)
77
+ _print_dt(now)
78
+ return func(exc_type, exc_value, exc_traceback)
79
+
80
+ return wrapper
81
+
82
+ return decorator
83
+
84
+
85
+ def _dttb_threading_excepthook(
86
+ tz: Optional[dt.tzinfo] = None,
87
+ ) -> Callable[[_ThreadingExcepthook], _ThreadingExcepthook]:
88
+ def decorator(
89
+ func: _ThreadingExcepthook,
90
+ ) -> _ThreadingExcepthook:
91
+ @functools.wraps(func)
92
+ def wrapper(
93
+ args: ExceptHookArgs,
94
+ ) -> object:
95
+ now = _now(tz=tz)
96
+ _log_dttb(now, args.exc_value)
97
+ _print_dt(now)
98
+ return func(args)
99
+
100
+ return wrapper
101
+
102
+ return decorator
103
+
104
+
105
+ def apply(
106
+ tz: Optional[dt.tzinfo] = None,
107
+ ) -> None:
108
+ """Applies attaching datetime to exception traceback.
109
+
110
+ This also supports logging and threading if involved.
111
+
112
+ Args:
113
+ tz: An optional `datetime.tzinfo` object used to determine the timezone of the
114
+ timestamp. If `None` or not given, the local timezone is used.
115
+ """
116
+ sys.excepthook = _dttb_sys_excepthook(tz=tz)(_sys_excepthook)
117
+ threading.excepthook = _dttb_threading_excepthook(tz=tz)(_threading_excepthook)
118
+
119
+
120
+ def reset() -> None:
121
+ """Resets to the default exception traceback."""
122
+ sys.excepthook = _sys_excepthook
123
+ threading.excepthook = _threading_excepthook
File without changes