Skip to content

Commit

Permalink
fix: Fixed a bug in the TreePrint class.
Browse files Browse the repository at this point in the history
  • Loading branch information
MPCodeWriter21 committed Jul 15, 2023
1 parent 2fc8af4 commit 9f4b9ad
Show file tree
Hide file tree
Showing 20 changed files with 281 additions and 104 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,7 @@ dmypy.json

# Pyre type checker
.pyre/

# IDEs
.idea/
.vscode/
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ Help this project by [Donation](DONATE.md)
Changes
-----------

### 2.5.5

Fixed a bug in the `TreePrint` class.

### 2.5.4

Added constant colors directly to the Colors module. Now you can do this:
Expand Down
10 changes: 2 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,9 @@ python setup.py install
Changes
-------

### 2.5.4
### 2.5.5

Added constant colors directly to the Colors module. Now you can do this:
```python
from log21 import print
from log21.colors import GREEN, WHITE, RED

print(GREEN + 'This' + WHITE + ' is' + RED + ' Red')
```
Fixed a bug in the `TreePrint` class.

[Full CHANGELOG](https://github.com/MPCodeWriter21/log21/blob/master/CHANGELOG.md)

Expand Down
5 changes: 4 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ classifiers = [
dependencies = [
"webcolors"
]
version = "2.5.4"
version = "2.5.5"

[tool.setuptools.packages.find]
where = ["src"]

[project.urls]
Homepage = "https://github.com/MPCodeWriter21/log21"
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
128 changes: 91 additions & 37 deletions log21/TreePrint.py → src/log21/TreePrint.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,26 @@

from __future__ import annotations

from typing import Union as _Union, Mapping as _Mapping, Sequence as _Sequence, Optional as _Optional, List as _List
from typing import (
List as _List, Union as _Union, Mapping as _Mapping, Optional as _Optional, Sequence
as _Sequence
)

from log21.Colors import get_colors as _gc


class TreePrint:

class Node:
def __init__(self, value: _Union[str, int], children: _Optional[_List[TreePrint.Node]] = None, indent: int = 4,
colors: _Optional[_Mapping[str, str]] = None, mode: str='-'):

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)
if children:
self._children = children
Expand Down Expand Up @@ -82,7 +93,8 @@ def __str__(self, level=0, prefix='', mode=None):
else:
prefix_ += chars[5] # ├ OR ╠
prefix_ += chars[0] * (self.indent - 1) # ─ OR ═
prefix_ = prefix_[:len(prefix)] + _gc(self.colors['branches']) + prefix_[len(prefix):]
prefix_ = prefix_[:len(prefix)] + _gc(self.colors['branches']
) + prefix_[len(prefix):]
text += child.__str__(level=level + 1, prefix=prefix_, mode=mode)

return text
Expand All @@ -107,44 +119,78 @@ def get_child(self, value: _Optional[str] = None, index: _Optional[int] = None):
if index:
return self._children[index]

@staticmethod
def add_to(node: TreePrint.Node, data: _Union[_Mapping, _Sequence, str, int], indent: int = 4,
colors: _Optional[_Mapping[str, str]] = None, mode='-'):
def add_to(
self: TreePrint.Node,
data: _Union[_Mapping, _Sequence, str, int],
indent: int = 4,
colors: _Optional[_Mapping[str, str]] = None,
mode='-'
):
if isinstance(data, _Mapping):
if len(data) == 1:
child = TreePrint.Node(list(data.keys())[0], indent=indent, colors=colors, mode=mode)
TreePrint.Node.add_to(child, list(data.values())[0], indent=indent, colors=colors, mode=mode)
node.add_child(child)
child = TreePrint.Node(
list(data.keys())[0], indent=indent, colors=colors, mode=mode
)
child.add_to(
list(data.values())[0], indent=indent, colors=colors, mode=mode
)
self.add_child(child)
else:
for key, value in data.items():
child = TreePrint.Node(key, indent=indent, colors=colors, mode=mode)
TreePrint.Node.add_to(child, value, indent=indent, colors=colors, mode=mode)
node.add_child(child)
elif isinstance(data, _Sequence):
for value in data:
if isinstance(value, _Mapping):
for key, dict_value in value.items():
child = TreePrint.Node(key, indent=indent, colors=colors, mode=mode)
TreePrint.Node.add_to(child, dict_value, indent=indent, colors=colors, mode=mode)
node.add_child(child)
elif isinstance(value, _Sequence):
child = TreePrint.Node('>', indent=indent, colors=colors, mode=mode)
TreePrint.Node.add_to(child, value, indent=indent, colors=colors, mode=mode)
node.add_child(child)
else:
child = TreePrint.Node(str(value), indent=indent, colors=colors, mode=mode)
node.add_child(child)
child = TreePrint.Node(
key, indent=indent, colors=colors, mode=mode
)
child.add_to(value, indent=indent, colors=colors, mode=mode)
self.add_child(child)
elif isinstance(data, _Sequence) and not isinstance(data, str):
if len(data) == 1:
self.add_child(
TreePrint.Node(
data[0], indent=indent, colors=colors, mode=mode
)
)
else:
for value in data:
if isinstance(value, _Mapping):
for key, dict_value in value.items():
child = TreePrint.Node(
key, indent=indent, colors=colors, mode=mode
)
child.add_to(
dict_value, indent=indent, colors=colors, mode=mode
)
self.add_child(child)
elif isinstance(value, _Sequence):
child = TreePrint.Node(
'>', indent=indent, colors=colors, mode=mode
)
child.add_to(value, indent=indent, colors=colors, mode=mode)
self.add_child(child)
else:
child = TreePrint.Node(
str(value), indent=indent, colors=colors, mode=mode
)
self.add_child(child)
else:
child = TreePrint.Node(str(data), indent=indent, colors=colors, mode=mode)
node.add_child(child)

def __init__(self, data: _Union[_Mapping, _Sequence, str, int], indent: int = 4,
colors: _Optional[_Mapping[str, str]] = None, mode='-'):
child = TreePrint.Node(
str(data), indent=indent, colors=colors, mode=mode
)
self.add_child(child)

def __init__(
self,
data: _Union[_Mapping, _Sequence, str, int],
indent: int = 4,
colors: _Optional[_Mapping[str, str]] = None,
mode='-'
):
self.indent = indent
self.mode = mode
if isinstance(data, _Mapping):
if len(data) == 1:
self.root = self.Node(list(data.keys())[0], indent=indent, colors=colors)
self.root = self.Node(
list(data.keys())[0], indent=indent, colors=colors
)
self.add_to_root(list(data.values()), colors=colors)
else:
self.root = self.Node('root', indent=indent, colors=colors)
Expand All @@ -155,15 +201,23 @@ def __init__(self, data: _Union[_Mapping, _Sequence, str, int], indent: int = 4,
else:
self.root = self.Node(str(data), indent=indent, colors=colors)

def add_to_root(self, data: _Union[_Mapping, _Sequence, str, int], colors: _Optional[_Mapping[str, str]] = None):
self.Node.add_to(self.root, data, indent=self.indent, colors=colors, mode=self.mode)
def add_to_root(
self,
data: _Union[_Mapping, _Sequence, str, int],
colors: _Optional[_Mapping[str, str]] = None
):
self.root.add_to(data, indent=self.indent, colors=colors, mode=self.mode)

def __str__(self, mode=None):
if not mode:
mode = self.mode
return self.root.__str__(mode=mode)


def tree_format(data: _Union[_Mapping, _Sequence, str, int], indent: int = 4, mode='-',
colors: _Optional[_Mapping[str, str]] = None):
def tree_format(
data: _Union[_Mapping, _Sequence, str, int],
indent: int = 4,
mode='-',
colors: _Optional[_Mapping[str, str]] = None
):
return str(TreePrint(data, indent=indent, colors=colors, mode=mode))
Loading

0 comments on commit 9f4b9ad

Please sign in to comment.