Skip to content

custom_var_printers definition #33

@JustMyD

Description

@JustMyD

How does custom_var_printers should work?

Here is the simple code copy from examples:

from traceback_with_variables import Format, ColorSchemes, hide, skip, is_ipython_global, print_exc

fmt_config = Format(
    before=3,
    after=1,
    max_value_str_len=10000,
    objects_details=0,
    ellipsis_rel_pos=0.7,
    max_exc_str_len=1000,
    ellipsis_='...',
    color_scheme=ColorSchemes.synthwave,
    skip_files_except=['my_project', 'site-packages'],
    brief_files_except='my_project',
    custom_var_printers=[  # first matching is used
        ('password', hide),  # by name, print const str
        (list, lambda v: f'list{v}'),  # by type, print fancy str
        (lambda name, type_, filename, is_global: is_global, skip),  # custom filter, skip printing
        (is_ipython_global, lambda v: None),  # same, handy for Jupyter
        (['secret', dict, (lambda name, *_: 'asd' in name)], lambda v: '???'),  # by different things, print const str
    ]
)

def func_divide(var1, var2):
    try:
        res = var1 / var2
    except ZeroDivisionError as e:
        print_exc(fmt=fmt_config)
        return None

    return res

one = 2
two = 0

result = func_divide(one, two)

This code is crashing with error:

Traceback (most recent call last):
  File ".../tmp.py", line 51, in <module>
    result = func_divide(one, two)
  File ".../tmp.py", line 43, in func_divide
    print_exc(fmt=fmt_config)
  File ".../.venv/lib/python3.10/site-packages/traceback_with_variables/print.py", line 33, in print_exc
    for line in iter_exc_lines(
  File ".../.venv/lib/python3.10/site-packages/traceback_with_variables/core.py", line 225, in _iter_lines
    var_str = _to_cropped_str(
  File ".../.venv/lib/python3.10/site-packages/traceback_with_variables/core.py", line 275, in _to_cropped_str
    print_ = get_print(name=name, obj=obj, filename=filename, is_global=is_global, var_printers=custom_var_printers)
  File ".../.venv/lib/python3.10/site-packages/traceback_with_variables/core.py", line 37, in get_print
    return next((p for should_p, p in var_printers if should_p(name, type_, filename, is_global)), show)
  File ".../.venv/lib/python3.10/site-packages/traceback_with_variables/core.py", line 37, in <genexpr>
    return next((p for should_p, p in var_printers if should_p(name, type_, filename, is_global)), show)
TypeError: 'str' object is not callable

Am i missing smthg?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions