From e9d7ceb8dcdf009a862a1739daa886d89b91fbcc Mon Sep 17 00:00:00 2001 From: CodeWriter21 Date: Tue, 27 Jun 2023 14:29:03 +0330 Subject: [PATCH] fix: Moved some dictionaries to `__init__` methods. `colors` in `Argparse.ColorizingHelpFormatter` class. `_level_name` in `Formatters._Formatter` class and `level_colors` in `Formatters.ColorizingFormatter` class. `sign_colors` in `PPrint.PrettyPrinter` class. `colors` in `TreePrint.TreePrint.Node` class. --- CHANGELOG.md | 8 +++ README.md | 8 ++- log21/Argparse.py | 28 ++++---- log21/Formatters.py | 40 +++++------ log21/PPrint.py | 166 ++++++++++++++++++++++---------------------- log21/TreePrint.py | 11 ++- log21/__init__.py | 2 +- pyproject.toml | 2 +- 8 files changed, 139 insertions(+), 126 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 82e6827..aed3fee 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,14 @@ Help this project by [Donation](DONATE.md) Changes ----------- +### 2.5.3 + +Moved some dictionaries to `__init__` methods. +`colors` in `Argparse.ColorizingHelpFormatter` class. +`_level_name` in `Formatters._Formatter` class and `level_colors` in `Formatters.ColorizingFormatter` class. +`sign_colors` in `PPrint.PrettyPrinter` class. +`colors` in `TreePrint.TreePrint.Node` class. + ### 2.5.2 Improved type-hintings. diff --git a/README.md b/README.md index e7e8ae5..ed9c9c6 100644 --- a/README.md +++ b/README.md @@ -53,9 +53,13 @@ python setup.py install Changes ------- -### 2.5.2 +### 2.5.3 -Improved type-hintings. +Moved some dictionaries to `__init__` methods. +`colors` in `Argparse.ColorizingHelpFormatter` class. +`_level_name` in `Formatters._Formatter` class and `level_colors` in `Formatters.ColorizingFormatter` class. +`sign_colors` in `PPrint.PrettyPrinter` class. +`colors` in `TreePrint.TreePrint.Node` class. [Full CHANGELOG](https://github.com/MPCodeWriter21/log21/blob/master/CHANGELOG.md) diff --git a/log21/Argparse.py b/log21/Argparse.py index d2df76d..85a254b 100644 --- a/log21/Argparse.py +++ b/log21/Argparse.py @@ -17,20 +17,22 @@ class ColorizingHelpFormatter(_argparse.HelpFormatter): - colors = { - 'usage': 'Cyan', - 'brackets': 'LightRed', - 'switches': 'LightCyan', - 'values': 'Green', - 'colons': 'LightRed', - 'commas': 'LightRed', - 'section headers': 'LightGreen', - 'help': 'LightWhite', - 'choices': 'LightGreen' - } - - def __init__(self, prog, indent_increment=2, max_help_position=24, width=None, colors: _Mapping[str, str] = None): + def __init__(self, prog, indent_increment=2, max_help_position=24, width=None, + colors: _Optional[_Mapping[str, str]] = None): super().__init__(prog, indent_increment, max_help_position, width) + + self.colors = { + 'usage': 'Cyan', + 'brackets': 'LightRed', + 'switches': 'LightCyan', + 'values': 'Green', + 'colons': 'LightRed', + 'commas': 'LightRed', + 'section headers': 'LightGreen', + 'help': 'LightWhite', + 'choices': 'LightGreen' + } + if colors: for key, value in colors.items(): if key in self.colors: diff --git a/log21/Formatters.py b/log21/Formatters.py index 25ec005..d89affb 100644 --- a/log21/Formatters.py +++ b/log21/Formatters.py @@ -11,16 +11,6 @@ class _Formatter(__Formatter): - _level_names: _Dict[int, str] = { - DEBUG: 'DEBUG', - INFO: 'INFO', - WARNING: 'WARNING', - ERROR: 'ERROR', - CRITICAL: 'CRITICAL', - PRINT: 'PRINT', - INPUT: 'INPUT' - } - def __init__(self, fmt: _Optional[str] = None, datefmt: _Optional[str] = None, style: str = '%', level_names: _Optional[_Mapping[int, str]] = None): """ @@ -49,6 +39,17 @@ def __init__(self, fmt: _Optional[str] = None, datefmt: _Optional[str] = None, s >>> """ super().__init__(fmt=fmt, datefmt=datefmt, style=style) + + self._level_names: _Dict[int, str] = { + DEBUG: 'DEBUG', + INFO: 'INFO', + WARNING: 'WARNING', + ERROR: 'ERROR', + CRITICAL: 'CRITICAL', + PRINT: 'PRINT', + INPUT: 'INPUT' + } + if level_names: for level, name in level_names.items(): self.level_names[level] = name @@ -89,16 +90,6 @@ def format(self, record) -> str: class ColorizingFormatter(_Formatter): - # Default color values - level_colors: _Dict[int, _Tuple[str, ...]] = { - DEBUG: ('lightblue',), - INFO: ('green',), - WARNING: ('lightyellow',), - ERROR: ('light red',), - CRITICAL: ('background red', 'white'), - PRINT: ('Cyan',), - INPUT: ('Magenta',) - } time_color: _Tuple[str, ...] = ('lightblue',) name_color = pathname_color = filename_color = module_color = func_name_color = thread_name_color = \ message_color = tuple() @@ -112,6 +103,15 @@ def __init__(self, fmt: _Optional[str] = None, datefmt: _Optional[str] = None, s thread_name_color: _Optional[_Tuple[str, ...]] = None, message_color: _Optional[_Tuple[str, ...]] = None): super().__init__(fmt=fmt, datefmt=datefmt, style=style, level_names=level_names) + self.level_colors: _Dict[int, _Tuple[str, ...]] = { + DEBUG: ('lightblue',), + INFO: ('green',), + WARNING: ('lightyellow',), + ERROR: ('light red',), + CRITICAL: ('background red', 'white'), + PRINT: ('Cyan',), + INPUT: ('Magenta',) + } # Checks and sets colors if level_colors: if not isinstance(level_colors, _Mapping): diff --git a/log21/PPrint.py b/log21/PPrint.py index 17cd9c3..a4053f4 100644 --- a/log21/PPrint.py +++ b/log21/PPrint.py @@ -8,7 +8,7 @@ import dataclasses as _dataclasses from pprint import PrettyPrinter as _PrettyPrinter -from typing import Mapping as _Mapping, Optional as _Optional +from typing import Mapping as _Mapping, Optional as _Optional, Dict as _Dict from log21.Colors import get_colors as _gc @@ -65,17 +65,7 @@ def __lt__(self, other): class PrettyPrinter(_PrettyPrinter): - signs_colors: _Mapping[str, str] = { - 'square-brackets': _gc('LightCyan'), - 'curly-braces': _gc('LightBlue'), - 'parenthesis': _gc('LightGreen'), - 'comma': _gc('LightRed'), - 'colon': _gc('LightRed'), - '...': _gc('LightMagenta'), - 'data': _gc('Green') - } - - def __init__(self, indent=1, width=80, depth=None, stream=None, signs_colors: _Optional[_Mapping[str, str]] = None, + def __init__(self, indent=1, width=80, depth=None, stream=None, sign_colors: _Optional[_Mapping[str, str]] = None, *, compact=False, sort_dicts=True, underscore_numbers=False, **kwargs): super().__init__(indent=indent, width=width, depth=depth, stream=stream, compact=compact, **kwargs) self._depth = depth @@ -88,9 +78,19 @@ def __init__(self, indent=1, width=80, depth=None, stream=None, signs_colors: _O self._compact = bool(compact) self._sort_dicts = sort_dicts self._underscore_numbers = underscore_numbers - if signs_colors: - for sign, color in signs_colors.items(): - self.signs_colors[sign.lower()] = _gc(color) + + self.sign_colors: _Dict[str, str] = { + 'square-brackets': _gc('LightCyan'), + 'curly-braces': _gc('LightBlue'), + 'parenthesis': _gc('LightGreen'), + 'comma': _gc('LightRed'), + 'colon': _gc('LightRed'), + '...': _gc('LightMagenta'), + 'data': _gc('Green') + } + if sign_colors: + for sign, color in sign_colors.items(): + self.sign_colors[sign.lower()] = _gc(color) def _format(self, obj, stream, indent, allowance, context, level): objid = id(obj) @@ -153,11 +153,11 @@ def _safe_repr(self, object_, context, max_levels, level): if issubclass(type_, dict) and representation is dict.__repr__: if not object_: - return self.signs_colors.get('curly-braces') + "{}" + self.signs_colors.get('data'), True, False + return self.sign_colors.get('curly-braces') + "{}" + self.sign_colors.get('data'), True, False object_id = id(object_) if max_levels and level >= max_levels: - return self.signs_colors.get('curly-braces') + "{" + self.signs_colors.get('...') + "..." + \ - self.signs_colors.get('curly-braces') + "}" + self.signs_colors.get('data'), \ + return self.sign_colors.get('curly-braces') + "{" + self.sign_colors.get('...') + "..." + \ + self.sign_colors.get('curly-braces') + "}" + self.sign_colors.get('data'), \ False, object_id in context if object_id in context: return _recursion(object_), False, True @@ -174,34 +174,34 @@ def _safe_repr(self, object_, context, max_levels, level): for k, v in items: krepr, kreadable, krecur = self.format(k, context, max_levels, level) vrepr, vreadable, vrecur = self.format(v, context, max_levels, level) - append(f"{krepr}{self.signs_colors.get('colon')}:{self.signs_colors.get('data')} {vrepr}") + append(f"{krepr}{self.sign_colors.get('colon')}:{self.sign_colors.get('data')} {vrepr}") readable = readable and kreadable and vreadable if krecur or vrecur: recursive = True del context[object_id] - return self.signs_colors.get('curly-braces') + "{" + self.signs_colors.get('data') + \ - (self.signs_colors.get('comma') + ", " + self.signs_colors.get('data')).join(components) + \ - self.signs_colors.get('curly-braces') + "}", readable, recursive + return self.sign_colors.get('curly-braces') + "{" + self.sign_colors.get('data') + \ + (self.sign_colors.get('comma') + ", " + self.sign_colors.get('data')).join(components) + \ + self.sign_colors.get('curly-braces') + "}", readable, recursive if (issubclass(type_, list) and representation is list.__repr__) or \ (issubclass(type_, tuple) and representation is tuple.__repr__): if issubclass(type_, list): if not object_: - return self.signs_colors.get('square-brackets') + "[]" + self.signs_colors.get('data'), True, False - format_ = self.signs_colors.get('square-brackets') + "[" + self.signs_colors.get('data') + "%s" + \ - self.signs_colors.get('square-brackets') + "]" + self.signs_colors.get('data') + return self.sign_colors.get('square-brackets') + "[]" + self.sign_colors.get('data'), True, False + format_ = self.sign_colors.get('square-brackets') + "[" + self.sign_colors.get('data') + "%s" + \ + self.sign_colors.get('square-brackets') + "]" + self.sign_colors.get('data') elif len(object_) == 1: - format_ = self.signs_colors.get('parenthesis') + "(" + self.signs_colors.get('data') + "%s" + \ - self.signs_colors.get('comma') + "," + self.signs_colors.get('parenthesis') + ")" + \ - self.signs_colors.get('data') + format_ = self.sign_colors.get('parenthesis') + "(" + self.sign_colors.get('data') + "%s" + \ + self.sign_colors.get('comma') + "," + self.sign_colors.get('parenthesis') + ")" + \ + self.sign_colors.get('data') else: if not object_: - return self.signs_colors.get('parenthesis') + "()" + self.signs_colors.get('data'), True, False - format_ = self.signs_colors.get('parenthesis') + "(" + self.signs_colors.get('data') + "%s" + \ - self.signs_colors.get('parenthesis') + ")" + self.signs_colors.get('data') + return self.sign_colors.get('parenthesis') + "()" + self.sign_colors.get('data'), True, False + format_ = self.sign_colors.get('parenthesis') + "(" + self.sign_colors.get('data') + "%s" + \ + self.sign_colors.get('parenthesis') + ")" + self.sign_colors.get('data') object_id = id(object_) if max_levels and level >= max_levels: - return format_ % self.signs_colors.get('...') + "...", False, object_id in context + return format_ % self.sign_colors.get('...') + "...", False, object_id in context if object_id in context: return _recursion(object_), False, True context[object_id] = 1 @@ -218,7 +218,7 @@ def _safe_repr(self, object_, context, max_levels, level): if orecur: recursive = True del context[object_id] - return format_ % (self.signs_colors.get('comma') + ", " + self.signs_colors.get('data')).join(components), \ + return format_ % (self.sign_colors.get('comma') + ", " + self.sign_colors.get('data')).join(components), \ readable, recursive rep = repr(object_) @@ -228,7 +228,7 @@ def _safe_repr(self, object_, context, max_levels, level): def _pprint_dict(self, obj, stream, indent, allowance, context, level): write = stream.write - write(self.signs_colors.get('curly-braces') + '{' + self.signs_colors.get('data')) + write(self.sign_colors.get('curly-braces') + '{' + self.sign_colors.get('data')) if self._indent_per_level > 1: write((self._indent_per_level - 1) * ' ') length = len(obj) @@ -239,7 +239,7 @@ def _pprint_dict(self, obj, stream, indent, allowance, context, level): items = obj.items() self._format_dict_items(items, stream, indent, allowance + 1, context, level) - write(self.signs_colors.get('curly-braces') + '}' + self.signs_colors.get('data')) + write(self.sign_colors.get('curly-braces') + '}' + self.sign_colors.get('data')) _dispatch[dict.__repr__] = _pprint_dict @@ -248,27 +248,27 @@ def _pprint_ordered_dict(self, obj, stream, indent, allowance, context, level): stream.write(repr(obj)) return cls = obj.__class__ - stream.write(cls.__name__ + self.signs_colors.get('parenthesis') + '(' + self.signs_colors.get('data')) + stream.write(cls.__name__ + self.sign_colors.get('parenthesis') + '(' + self.sign_colors.get('data')) self._format(list(obj.items()), stream, indent + len(cls.__name__) + 1, allowance + 1, context, level) - stream.write(self.signs_colors.get('parenthesis') + ')' + self.signs_colors.get('data')) + stream.write(self.sign_colors.get('parenthesis') + ')' + self.sign_colors.get('data')) _dispatch[_collections.OrderedDict.__repr__] = _pprint_ordered_dict def _pprint_list(self, obj, stream, indent, allowance, context, level): - stream.write(self.signs_colors.get('square-brackets') + '[' + self.signs_colors.get('data')) + stream.write(self.sign_colors.get('square-brackets') + '[' + self.sign_colors.get('data')) self._format_items(obj, stream, indent, allowance + 1, context, level) - stream.write(self.signs_colors.get('square-brackets') + ']' + self.signs_colors.get('data')) + stream.write(self.sign_colors.get('square-brackets') + ']' + self.sign_colors.get('data')) _dispatch[list.__repr__] = _pprint_list def _pprint_tuple(self, obj, stream, indent, allowance, context, level): - stream.write(self.signs_colors.get('parenthesis') + '(' + self.signs_colors.get('data')) - endchar = self.signs_colors.get('comma') + ',' + self.signs_colors.get('parenthesis') + ')' + \ - self.signs_colors.get('data') if len(obj) == 1 else self.signs_colors.get('parenthesis') + ')' + \ - self.signs_colors.get('data') + stream.write(self.sign_colors.get('parenthesis') + '(' + self.sign_colors.get('data')) + endchar = self.sign_colors.get('comma') + ',' + self.sign_colors.get('parenthesis') + ')' + \ + self.sign_colors.get('data') if len(obj) == 1 else self.sign_colors.get('parenthesis') + ')' + \ + self.sign_colors.get('data') self._format_items(obj, stream, indent, allowance + len(endchar), context, level) stream.write(endchar) @@ -281,13 +281,13 @@ def _pprint_set(self, obj, stream, indent, allowance, context, level): return typ = obj.__class__ if typ is set: - stream.write(self.signs_colors.get('curly-braces') + '{' + self.signs_colors.get('data')) - endchar = self.signs_colors.get('curly-braces') + '}' + self.signs_colors.get('data') + stream.write(self.sign_colors.get('curly-braces') + '{' + self.sign_colors.get('data')) + endchar = self.sign_colors.get('curly-braces') + '}' + self.sign_colors.get('data') else: - stream.write(typ.__name__ + self.signs_colors.get('parenthesis') + '(' + - self.signs_colors.get('curly-braces') + '{' + self.signs_colors.get('data')) - endchar = self.signs_colors.get('curly-braces') + '}' + self.signs_colors.get('parenthesis') + ')' \ - + self.signs_colors.get('data') + stream.write(typ.__name__ + self.sign_colors.get('parenthesis') + '(' + + self.sign_colors.get('curly-braces') + '{' + self.sign_colors.get('data')) + endchar = self.sign_colors.get('curly-braces') + '}' + self.sign_colors.get('parenthesis') + ')' \ + + self.sign_colors.get('data') indent += len(typ.__name__) + 1 obj = sorted(obj, key=_SafeKey) self._format_items(obj, stream, indent, allowance + len(endchar), @@ -339,13 +339,13 @@ def _pprint_str(self, object_, stream, indent, allowance, context, level): write(representation) return if level == 1: - write(self.signs_colors.get('parenthesis') + '(' + self.signs_colors.get('data')) + write(self.sign_colors.get('parenthesis') + '(' + self.sign_colors.get('data')) for i, representation in enumerate(chunks): if i > 0: write('\n' + ' ' * indent) write(representation) if level == 1: - write(self.signs_colors.get('parenthesis') + ')' + self.signs_colors.get('data')) + write(self.sign_colors.get('parenthesis') + ')' + self.sign_colors.get('data')) _dispatch[str.__repr__] = _pprint_str @@ -358,7 +358,7 @@ def _pprint_bytes(self, obj, stream, indent, allowance, context, level): if parens: indent += 1 allowance += 1 - write(self.signs_colors.get('parenthesis') + '(' + self.signs_colors.get('data')) + write(self.sign_colors.get('parenthesis') + '(' + self.sign_colors.get('data')) delim = '' for rep in _wrap_bytes_repr(obj, self._width - indent, allowance): write(delim) @@ -366,24 +366,24 @@ def _pprint_bytes(self, obj, stream, indent, allowance, context, level): if not delim: delim = '\n' + ' ' * indent if parens: - write(self.signs_colors.get('parenthesis') + ')' + self.signs_colors.get('data')) + write(self.sign_colors.get('parenthesis') + ')' + self.sign_colors.get('data')) _dispatch[bytes.__repr__] = _pprint_bytes def _pprint_bytearray(self, obj, stream, indent, allowance, context, level): write = stream.write - write('bytearray' + self.signs_colors.get('parenthesis') + '(' + self.signs_colors.get('data')) + write('bytearray' + self.sign_colors.get('parenthesis') + '(' + self.sign_colors.get('data')) self._pprint_bytes(bytes(obj), stream, indent + 10, allowance + 1, context, level + 1) - write(self.signs_colors.get('parenthesis') + ')' + self.signs_colors.get('data')) + write(self.sign_colors.get('parenthesis') + ')' + self.sign_colors.get('data')) _dispatch[bytearray.__repr__] = _pprint_bytearray def _pprint_mappingproxy(self, obj, stream, indent, allowance, context, level): - stream.write('mappingproxy' + self.signs_colors.get('parenthesis') + '(' + self.signs_colors.get('data')) + stream.write('mappingproxy' + self.sign_colors.get('parenthesis') + '(' + self.sign_colors.get('data')) self._format(obj.copy(), stream, indent + 13, allowance + 1, context, level) - stream.write(self.signs_colors.get('parenthesis') + ')' + self.signs_colors.get('data')) + stream.write(self.sign_colors.get('parenthesis') + ')' + self.sign_colors.get('data')) _dispatch[_types.MappingProxyType.__repr__] = _pprint_mappingproxy @@ -396,22 +396,22 @@ def _pprint_simplenamespace(self, obj, stream, indent, allowance, context, level cls_name = obj.__class__.__name__ indent += len(cls_name) + 1 items = obj.__dict__.items() - stream.write(cls_name + self.signs_colors.get('parenthesis') + '(' + self.signs_colors.get('data')) + stream.write(cls_name + self.sign_colors.get('parenthesis') + '(' + self.sign_colors.get('data')) self._format_namespace_items(items, stream, indent, allowance, context, level) - stream.write(self.signs_colors.get('parenthesis') + ')' + self.signs_colors.get('data')) + stream.write(self.sign_colors.get('parenthesis') + ')' + self.sign_colors.get('data')) _dispatch[_types.SimpleNamespace.__repr__] = _pprint_simplenamespace def _format_dict_items(self, items, stream, indent, allowance, context, level): write = stream.write indent += self._indent_per_level - delimnl = self.signs_colors.get('comma') + ',\n' + self.signs_colors.get('data') + ' ' * indent + delimnl = self.sign_colors.get('comma') + ',\n' + self.sign_colors.get('data') + ' ' * indent last_index = len(items) - 1 for i, (key, ent) in enumerate(items): last = i == last_index rep = self._repr(key, context, level) write(rep) - write(self.signs_colors.get('colon') + ': ' + self.signs_colors.get('data')) + write(self.sign_colors.get('colon') + ': ' + self.sign_colors.get('data')) self._format(ent, stream, indent + len(rep) + 2, allowance if last else 1, context, level) @@ -420,7 +420,7 @@ def _format_dict_items(self, items, stream, indent, allowance, context, level): def _format_namespace_items(self, items, stream, indent, allowance, context, level): write = stream.write - delimnl = self.signs_colors.get('comma') + ',\n' + self.signs_colors.get('data') + ' ' * indent + delimnl = self.sign_colors.get('comma') + ',\n' + self.sign_colors.get('data') + ' ' * indent last_index = len(items) - 1 for i, (key, ent) in enumerate(items): last = i == last_index @@ -429,7 +429,7 @@ def _format_namespace_items(self, items, stream, indent, allowance, context, lev if id(ent) in context: # Special-case representation of recursion to match standard # recursive dataclass repr. - write(self.signs_colors.get('...') + "..." + self.signs_colors.get('data')) + write(self.sign_colors.get('...') + "..." + self.sign_colors.get('data')) else: self._format(ent, stream, indent + len(key) + 1, allowance if last else 1, context, level) if not last: @@ -440,7 +440,7 @@ def _format_items(self, items, stream, indent, allowance, context, level): indent += self._indent_per_level if self._indent_per_level > 1: write((self._indent_per_level - 1) * ' ') - delimnl = self.signs_colors.get('comma') + ',\n' + self.signs_colors.get('data') + ' ' * indent + delimnl = self.sign_colors.get('comma') + ',\n' + self.sign_colors.get('data') + ' ' * indent delim = '' width = max_width = self._width - indent + 1 it = iter(items) @@ -467,7 +467,7 @@ def _format_items(self, items, stream, indent, allowance, context, level): if width >= w: width -= w write(delim) - delim = self.signs_colors.get('comma') + ', ' + self.signs_colors.get('data') + delim = self.sign_colors.get('comma') + ', ' + self.sign_colors.get('data') write(rep) continue write(delim) @@ -481,10 +481,10 @@ def _pprint_default_dict(self, obj, stream, indent, allowance, context, level): rdf = self._repr(obj.default_factory, context, level) cls = obj.__class__ indent += len(cls.__name__) + 1 - stream.write(cls.__name__ + self.signs_colors.get('parenthesis') + '(' + self.signs_colors.get('data') + rdf + - self.signs_colors.get('comma') + ',\n' + self.signs_colors.get('data') + (' ' * indent)) + stream.write(cls.__name__ + self.sign_colors.get('parenthesis') + '(' + self.sign_colors.get('data') + rdf + + self.sign_colors.get('comma') + ',\n' + self.sign_colors.get('data') + (' ' * indent)) self._pprint_dict(obj, stream, indent, allowance + 1, context, level) - stream.write(self.signs_colors.get('parenthesis') + ')' + self.signs_colors.get('data')) + stream.write(self.sign_colors.get('parenthesis') + ')' + self.sign_colors.get('data')) _dispatch[_collections.defaultdict.__repr__] = _pprint_default_dict @@ -493,16 +493,16 @@ def _pprint_counter(self, obj, stream, indent, allowance, context, level): stream.write(repr(obj)) return cls = obj.__class__ - stream.write(cls.__name__ + self.signs_colors.get('parenthesis') + '(' + - self.signs_colors.get('curly-braces') + '{' + self.signs_colors.get('data')) + stream.write(cls.__name__ + self.sign_colors.get('parenthesis') + '(' + + self.sign_colors.get('curly-braces') + '{' + self.sign_colors.get('data')) if self._indent_per_level > 1: stream.write((self._indent_per_level - 1) * ' ') items = obj.most_common() self._format_dict_items(items, stream, indent + len(cls.__name__) + 1, allowance + 2, context, level) - stream.write(self.signs_colors.get('curly-braces') + '}' + - self.signs_colors.get('parenthesis') + ')' + self.signs_colors.get('data')) + stream.write(self.sign_colors.get('curly-braces') + '}' + + self.sign_colors.get('parenthesis') + ')' + self.sign_colors.get('data')) _dispatch[_collections.Counter.__repr__] = _pprint_counter @@ -511,15 +511,15 @@ def _pprint_chain_map(self, obj, stream, indent, allowance, context, level): stream.write(repr(obj)) return cls = obj.__class__ - stream.write(cls.__name__ + self.signs_colors.get('parenthesis') + '(' + self.signs_colors.get('data')) + stream.write(cls.__name__ + self.sign_colors.get('parenthesis') + '(' + self.sign_colors.get('data')) indent += len(cls.__name__) + 1 for i, m in enumerate(obj.maps): if i == len(obj.maps) - 1: self._format(m, stream, indent, allowance + 1, context, level) - stream.write(self.signs_colors.get('parenthesis') + ')' + self.signs_colors.get('data')) + stream.write(self.sign_colors.get('parenthesis') + ')' + self.sign_colors.get('data')) else: self._format(m, stream, indent, 1, context, level) - stream.write(self.signs_colors.get('comma') + ',\n' + self.signs_colors.get('data') + ' ' * indent) + stream.write(self.sign_colors.get('comma') + ',\n' + self.sign_colors.get('data') + ' ' * indent) _dispatch[_collections.ChainMap.__repr__] = _pprint_chain_map @@ -528,21 +528,21 @@ def _pprint_deque(self, obj, stream, indent, allowance, context, level): stream.write(repr(obj)) return cls = obj.__class__ - stream.write(cls.__name__ + self.signs_colors.get('parenthesis') + '(' + self.signs_colors.get('data')) + stream.write(cls.__name__ + self.sign_colors.get('parenthesis') + '(' + self.sign_colors.get('data')) indent += len(cls.__name__) + 1 - stream.write(self.signs_colors.get('square-brackets') + '[' + self.signs_colors.get('data')) + stream.write(self.sign_colors.get('square-brackets') + '[' + self.sign_colors.get('data')) if obj.maxlen is None: self._format_items(obj, stream, indent, allowance + 2, context, level) - stream.write(self.signs_colors.get('square-brackets') + ']' + self.signs_colors.get('parenthesis') + ')' + - self.signs_colors.get('data')) + stream.write(self.sign_colors.get('square-brackets') + ']' + self.sign_colors.get('parenthesis') + ')' + + self.sign_colors.get('data')) else: self._format_items(obj, stream, indent, 2, context, level) rml = self._repr(obj.maxlen, context, level) - stream.write(self.signs_colors.get('square-brackets') + ']' + self.signs_colors.get('comma') + - ',' + self.signs_colors.get('data') + '\n' + (' ' * indent) + 'maxlen=' + rml + - self.signs_colors.get('parenthesis') + ')') + stream.write(self.sign_colors.get('square-brackets') + ']' + self.sign_colors.get('comma') + + ',' + self.sign_colors.get('data') + '\n' + (' ' * indent) + 'maxlen=' + rml + + self.sign_colors.get('parenthesis') + ')') _dispatch[_collections.deque.__repr__] = _pprint_deque @@ -565,5 +565,5 @@ def _pprint_user_string(self, obj, stream, indent, allowance, context, level): def pformat(obj, indent=1, width=80, depth=None, signs_colors: _Optional[_Mapping[str, str]] = None, *, compact=False, sort_dicts=True, underscore_numbers=False, **kwargs): """Format a Python object into a pretty-printed representation.""" - return PrettyPrinter(indent=indent, width=width, depth=depth, compact=compact, signs_colors=signs_colors, + return PrettyPrinter(indent=indent, width=width, depth=depth, compact=compact, sign_colors=signs_colors, sort_dicts=sort_dicts, underscore_numbers=underscore_numbers, **kwargs).pformat(obj) diff --git a/log21/TreePrint.py b/log21/TreePrint.py index 25d8edd..b070218 100644 --- a/log21/TreePrint.py +++ b/log21/TreePrint.py @@ -10,11 +10,6 @@ class TreePrint: class Node: - colors = { - 'branches': _gc('Green'), - 'fruit': _gc('LightMagenta'), - } - def __init__(self, value: _Union[str, int], children: _Optional[_List[TreePrint.Node]] = None, indent: int = 4, colors: _Optional[_Mapping[str, str]] = None, mode: str='-'): self.value = str(value) @@ -23,7 +18,11 @@ def __init__(self, value: _Union[str, int], children: _Optional[_List[TreePrint. else: self._children = [] self.indent = indent - self.colors = self.colors.copy() + self.colors = { + 'branches': _gc('Green'), + 'fruit': _gc('LightMagenta'), + } + if colors: for key, value in colors.items(): if key in self.colors: diff --git a/log21/__init__.py b/log21/__init__.py index 0aaa0f9..c713435 100644 --- a/log21/__init__.py +++ b/log21/__init__.py @@ -22,7 +22,7 @@ from log21.Formatters import ColorizingFormatter, DecolorizingFormatter from log21.Colors import Colors, get_color, get_colors, ansi_escape, get_color_name, closest_color -__version__ = "2.5.2" +__version__ = "2.5.3" __author__ = "CodeWriter21 (Mehrad Pooryoussof)" __github__ = "Https://GitHub.com/MPCodeWriter21/log21" __all__ = ['ColorizingStreamHandler', 'DecolorizingFileHandler', 'ColorizingFormatter', 'DecolorizingFormatter', diff --git a/pyproject.toml b/pyproject.toml index a8ceff7..3d18475 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -25,7 +25,7 @@ classifiers = [ dependencies = [ "webcolors" ] -version = "2.5.2" +version = "2.5.3" [project.urls] Homepage = "https://github.com/MPCodeWriter21/log21"