11# log21.TreePrint.py
22# CodeWriter21
33
4- from typing import Union as _Union , Mapping as _Mapping , Sequence as _Sequence
4+ from __future__ import annotations
5+
6+ from typing import Union as _Union , Mapping as _Mapping , Sequence as _Sequence , Optional as _Optional , List as _List
57
68from log21 .Colors import get_colors as _gc
79
@@ -13,8 +15,8 @@ class Node:
1315 'fruit' : _gc ('LightMagenta' ),
1416 }
1517
16- def __init__ (self , value : _Union [str , int ], children : list = None , indent : int = 4 ,
17- colors : _Mapping [str , str ] = None , mode = '-' ):
18+ def __init__ (self , value : _Union [str , int ], children : _Optional [ _List [ TreePrint . Node ]] = None , indent : int = 4 ,
19+ colors : _Optional [ _Mapping [str , str ]] = None , mode : str = '-' ):
1820 self .value = str (value )
1921 if children :
2022 self ._children = children
@@ -33,16 +35,17 @@ def __init__(self, value: _Union[str, int], children: list = None, indent: int =
3335 if self .mode == - 1 :
3436 raise ValueError ('`mode` must be - or =' )
3537
36- def _get_mode (self , mode = None ) -> int :
38+ def _get_mode (self , mode : _Optional [ _Union [ str , int ]] = None ) -> int :
3739 if not mode :
3840 mode = self .mode
3941 if isinstance (mode , int ):
4042 if mode in [1 , 2 ]:
4143 return mode
42- if mode in '-_─┌│|└┬├└' :
43- return 1
44- if mode in '=═╔║╠╚╦╚' :
45- return 2
44+ elif isinstance (mode , str ):
45+ if mode in '-_─┌│|└┬├└' :
46+ return 1
47+ if mode in '=═╔║╠╚╦╚' :
48+ return 2
4649 return - 1
4750
4851 def __str__ (self , level = 0 , prefix = '' , mode = None ):
@@ -88,12 +91,12 @@ def __str__(self, level=0, prefix='', mode=None):
8891 def has_child (self ):
8992 return len (self ._children ) > 0
9093
91- def add_child (self , child : ' TreePrint.Node' ):
94+ def add_child (self , child : TreePrint .Node ):
9295 if not isinstance (child , TreePrint .Node ):
9396 raise TypeError ('`child` must be TreePrint.Node' )
9497 self ._children .append (child )
9598
96- def get_child (self , value : str = None , index : int = None ):
99+ def get_child (self , value : _Optional [ str ] = None , index : _Optional [ int ] = None ):
97100 if value and index :
98101 raise ValueError ('`value` and `index` can not be both set' )
99102 if not value and not index :
@@ -106,8 +109,8 @@ def get_child(self, value: str = None, index: int = None):
106109 return self ._children [index ]
107110
108111 @staticmethod
109- def add_to (node : ' TreePrint.Node' , data : _Union [_Mapping , _Sequence , str , int ], indent : int = 4 ,
110- colors : _Mapping [str , str ] = None , mode = '-' ):
112+ def add_to (node : TreePrint .Node , data : _Union [_Mapping , _Sequence , str , int ], indent : int = 4 ,
113+ colors : _Optional [ _Mapping [str , str ] ] = None , mode = '-' ):
111114 if isinstance (data , _Mapping ):
112115 if len (data ) == 1 :
113116 child = TreePrint .Node (list (data .keys ())[0 ], indent = indent , colors = colors , mode = mode )
@@ -137,7 +140,7 @@ def add_to(node: 'TreePrint.Node', data: _Union[_Mapping, _Sequence, str, int],
137140 node .add_child (child )
138141
139142 def __init__ (self , data : _Union [_Mapping , _Sequence , str , int ], indent : int = 4 ,
140- colors : _Mapping [str , str ] = None , mode = '-' ):
143+ colors : _Optional [ _Mapping [str , str ] ] = None , mode = '-' ):
141144 self .indent = indent
142145 self .mode = mode
143146 if isinstance (data , _Mapping ):
@@ -153,7 +156,7 @@ def __init__(self, data: _Union[_Mapping, _Sequence, str, int], indent: int = 4,
153156 else :
154157 self .root = self .Node (str (data ), indent = indent , colors = colors )
155158
156- def add_to_root (self , data : _Union [_Mapping , _Sequence , str , int ], colors : _Mapping [str , str ] = None ):
159+ def add_to_root (self , data : _Union [_Mapping , _Sequence , str , int ], colors : _Optional [ _Mapping [str , str ] ] = None ):
157160 self .Node .add_to (self .root , data , indent = self .indent , colors = colors , mode = self .mode )
158161
159162 def __str__ (self , mode = None ):
@@ -163,5 +166,5 @@ def __str__(self, mode=None):
163166
164167
165168def tree_format (data : _Union [_Mapping , _Sequence , str , int ], indent : int = 4 , mode = '-' ,
166- colors : _Mapping [str , str ] = None ):
169+ colors : _Optional [ _Mapping [str , str ] ] = None ):
167170 return str (TreePrint (data , indent = indent , colors = colors , mode = mode ))
0 commit comments