Skip to content

Commit

Permalink
add default format
Browse files Browse the repository at this point in the history
  • Loading branch information
andy-landy committed Dec 5, 2021
1 parent e97f94f commit 0047463
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 19 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ _Contents:_ **[Installation](#installation)** | **[🚀 Quick Start](#-quick-sta

---

> :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!
> :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!
---

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

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


Expand Down
3 changes: 2 additions & 1 deletion README.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ Using a logger [<a href="{{ examples_code_url }}/log_for_function.py">with a dec

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


Expand Down
20 changes: 17 additions & 3 deletions examples/format_customized.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
from traceback_with_variables import Format, ColorSchemes, is_ipython_global
from traceback_with_variables import Format, ColorSchemes, is_ipython_global, default_format as defaults


fmt = Format(
# approach 1

defaults.max_value_str_len = 10000


# approach 2

fmt2 = Format(
before=3,
after=1,
max_value_str_len=100,
max_value_str_len=10000,
max_exc_str_len=1000,
ellipsis_='...',
color_scheme=ColorSchemes.synthwave,
Expand All @@ -18,3 +25,10 @@
(['secret', dict, (lambda name, *_: 'asd' in name)], lambda v: '???'), # by different things, print const str
]
)
# print_exc(fmt=fmt2)


# approach 3

fmt3 = defaults.replace(max_value_str_len=10000)
# print_exc(fmt=fmt3)
4 changes: 3 additions & 1 deletion examples/simple.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from traceback_with_variables import activate_by_import
from traceback_with_variables import activate_by_import # , defaut_format


# default_format.max_value_str_len = 10000

def main():
n = 0
print(1 / n)
Expand Down
4 changes: 2 additions & 2 deletions traceback_with_variables/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
"""

from .color import ColorScheme, ColorSchemes, supports_ansi # noqa
from .core import Format, format_exc, iter_exc_lines, format_cur_tb, iter_cur_tb_lines # noqa
from .core import Format, format_exc, iter_exc_lines, format_cur_tb, iter_cur_tb_lines, default_format # noqa
from .print import print_exc, print_cur_tb, printing_exc, prints_exc, LoggerAsFile # noqa
from .global_hooks import global_print_exc, global_print_exc_in_ipython, is_ipython_global # noqa


__version__ = '2.0.3'
__version__ = '2.0.4'
10 changes: 6 additions & 4 deletions traceback_with_variables/activate_by_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
For the simplest usage possible. Just import it
"""

from traceback_with_variables.global_hooks import global_print_exc, Format
from traceback_with_variables.global_hooks import global_print_exc
from traceback_with_variables import core


global_print_exc(fmt=Format(custom_var_printers=[
((lambda n, t, fn, is_global: is_global), lambda v: None)
]))
core.default_format = core.default_format.replace(
custom_var_printers=[((lambda n, t, fn, is_global: is_global), lambda v: None)]
)
global_print_exc()
11 changes: 7 additions & 4 deletions traceback_with_variables/activate_in_ipython_by_import.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
"""
For the simplest usage possible. Just import it
For the simplest usage possible. Jupyter or IPython. Just import it
"""

from traceback_with_variables.color import ColorSchemes
from traceback_with_variables.global_hooks import global_print_exc_in_ipython, Format, is_ipython_global
from traceback_with_variables.global_hooks import global_print_exc_in_ipython, is_ipython_global
from traceback_with_variables import core

global_print_exc_in_ipython(fmt=Format(

core.default_format = core.default_format.replace(
custom_var_printers=[(is_ipython_global, lambda v: None)],
color_scheme=ColorSchemes.common
))
)
global_print_exc_in_ipython()
17 changes: 15 additions & 2 deletions traceback_with_variables/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import re
import sys
import traceback
from typing import Any, Iterator, Union, Optional, TextIO, NoReturn, List, Callable, Type, Tuple
from typing import Any, Iterator, Union, Optional, TextIO, NoReturn, List, Callable, Type, Tuple, Dict

from traceback_with_variables.color import ColorScheme, ColorSchemes, supports_ansi

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

def replace(self, **kwargs: Dict[str, Any]) -> 'Format':
result = Format()

for key, value in self.__dict__.items():
setattr(result, key, value)
for key, value in kwargs.items():
setattr(result, key, value)

return result


def format_exc(
e: Optional[Exception] = None,
Expand Down Expand Up @@ -142,7 +152,7 @@ def _iter_lines(
fmt: Optional[Format] = None,
for_file: Optional[TextIO] = None,
) -> Iterator[str]:
fmt_: Format = fmt or Format()
fmt_: Format = fmt or default_format
c: ColorScheme = fmt_.color_scheme or \
(ColorSchemes.common if (for_file and supports_ansi(for_file)) else ColorSchemes.none)

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

return should_print


default_format = Format()

0 comments on commit 0047463

Please sign in to comment.