Skip to content

Commit 0047463

Browse files
author
andy-landy
committed
add default format
1 parent e97f94f commit 0047463

File tree

8 files changed

+55
-19
lines changed

8 files changed

+55
-19
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ _Contents:_ **[Installation](#installation)** | **[🚀 Quick Start](#-quick-sta
4040

4141
---
4242

43-
> :warning: **This module is being updated and has a substantial list of features to add some time soon**: so any proposal or advice or warning is very welcome and will be taken into account of course. When I started it I wanted to make a tool meeting all standard use cases. I think in this particular domain this is rather achievable, so I'll try. Note `next_version` branch also. Have fun!
43+
> :warning: **This module is actively updated and has a substantial list of features to add this week**: so any proposal or advice or warning is very welcome and will be taken into account of course. When I started it I wanted to make a tool meeting all standard use cases. I think in this particular domain this is rather achievable, so I'll try. Note `next_version` branch also. Have fun!
4444
4545
---
4646

@@ -106,7 +106,8 @@ Using a logger [<a href="https://github.com/andy-landy/traceback_with_variables/
106106

107107
<a href="https://github.com/andy-landy/traceback_with_variables/tree/master/examples/format_customized.py">Customize</a> any of the previous examples:
108108
```python
109-
...(fmt=Format(max_value_str_len=80, skip_files_except='my_project'))
109+
default_format.max_value_str_len = 10000
110+
default_format.skip_files_except = 'my_project'
110111
```
111112

112113

README.tmpl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,8 @@ Using a logger [<a href="{{ examples_code_url }}/log_for_function.py">with a dec
106106

107107
<a href="{{ examples_code_url }}/format_customized.py">Customize</a> any of the previous examples:
108108
```python
109-
...(fmt=Format(max_value_str_len=80, skip_files_except='my_project'))
109+
default_format.max_value_str_len = 10000
110+
default_format.skip_files_except = 'my_project'
110111
```
111112

112113

examples/format_customized.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
1-
from traceback_with_variables import Format, ColorSchemes, is_ipython_global
1+
from traceback_with_variables import Format, ColorSchemes, is_ipython_global, default_format as defaults
22

33

4-
fmt = Format(
4+
# approach 1
5+
6+
defaults.max_value_str_len = 10000
7+
8+
9+
# approach 2
10+
11+
fmt2 = Format(
512
before=3,
613
after=1,
7-
max_value_str_len=100,
14+
max_value_str_len=10000,
815
max_exc_str_len=1000,
916
ellipsis_='...',
1017
color_scheme=ColorSchemes.synthwave,
@@ -18,3 +25,10 @@
1825
(['secret', dict, (lambda name, *_: 'asd' in name)], lambda v: '???'), # by different things, print const str
1926
]
2027
)
28+
# print_exc(fmt=fmt2)
29+
30+
31+
# approach 3
32+
33+
fmt3 = defaults.replace(max_value_str_len=10000)
34+
# print_exc(fmt=fmt3)

examples/simple.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
from traceback_with_variables import activate_by_import
1+
from traceback_with_variables import activate_by_import # , defaut_format
22

33

4+
# default_format.max_value_str_len = 10000
5+
46
def main():
57
n = 0
68
print(1 / n)

traceback_with_variables/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
"""
66

77
from .color import ColorScheme, ColorSchemes, supports_ansi # noqa
8-
from .core import Format, format_exc, iter_exc_lines, format_cur_tb, iter_cur_tb_lines # noqa
8+
from .core import Format, format_exc, iter_exc_lines, format_cur_tb, iter_cur_tb_lines, default_format # noqa
99
from .print import print_exc, print_cur_tb, printing_exc, prints_exc, LoggerAsFile # noqa
1010
from .global_hooks import global_print_exc, global_print_exc_in_ipython, is_ipython_global # noqa
1111

1212

13-
__version__ = '2.0.3'
13+
__version__ = '2.0.4'

traceback_with_variables/activate_by_import.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22
For the simplest usage possible. Just import it
33
"""
44

5-
from traceback_with_variables.global_hooks import global_print_exc, Format
5+
from traceback_with_variables.global_hooks import global_print_exc
6+
from traceback_with_variables import core
67

78

8-
global_print_exc(fmt=Format(custom_var_printers=[
9-
((lambda n, t, fn, is_global: is_global), lambda v: None)
10-
]))
9+
core.default_format = core.default_format.replace(
10+
custom_var_printers=[((lambda n, t, fn, is_global: is_global), lambda v: None)]
11+
)
12+
global_print_exc()
Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
"""
2-
For the simplest usage possible. Just import it
2+
For the simplest usage possible. Jupyter or IPython. Just import it
33
"""
44

55
from traceback_with_variables.color import ColorSchemes
6-
from traceback_with_variables.global_hooks import global_print_exc_in_ipython, Format, is_ipython_global
6+
from traceback_with_variables.global_hooks import global_print_exc_in_ipython, is_ipython_global
7+
from traceback_with_variables import core
78

8-
global_print_exc_in_ipython(fmt=Format(
9+
10+
core.default_format = core.default_format.replace(
911
custom_var_printers=[(is_ipython_global, lambda v: None)],
1012
color_scheme=ColorSchemes.common
11-
))
13+
)
14+
global_print_exc_in_ipython()

traceback_with_variables/core.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import re
44
import sys
55
import traceback
6-
from typing import Any, Iterator, Union, Optional, TextIO, NoReturn, List, Callable, Type, Tuple
6+
from typing import Any, Iterator, Union, Optional, TextIO, NoReturn, List, Callable, Type, Tuple, Dict
77

88
from traceback_with_variables.color import ColorScheme, ColorSchemes, supports_ansi
99

@@ -67,6 +67,16 @@ def parse(cls, ns: argparse.Namespace) -> 'Format':
6767
custom_var_printers=[((lambda n, t, fn, is_global: is_global), lambda v: None)] if ns.no_globals else None,
6868
)
6969

70+
def replace(self, **kwargs: Dict[str, Any]) -> 'Format':
71+
result = Format()
72+
73+
for key, value in self.__dict__.items():
74+
setattr(result, key, value)
75+
for key, value in kwargs.items():
76+
setattr(result, key, value)
77+
78+
return result
79+
7080

7181
def format_exc(
7282
e: Optional[Exception] = None,
@@ -142,7 +152,7 @@ def _iter_lines(
142152
fmt: Optional[Format] = None,
143153
for_file: Optional[TextIO] = None,
144154
) -> Iterator[str]:
145-
fmt_: Format = fmt or Format()
155+
fmt_: Format = fmt or default_format
146156
c: ColorScheme = fmt_.color_scheme or \
147157
(ColorSchemes.common if (for_file and supports_ansi(for_file)) else ColorSchemes.none)
148158

@@ -245,3 +255,6 @@ def should_print(name: str, type_: Type, file: str, is_global: bool) -> bool:
245255
)
246256

247257
return should_print
258+
259+
260+
default_format = Format()

0 commit comments

Comments
 (0)