projects
/
python_utils.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Cleanup
[python_utils.git]
/
text_utils.py
diff --git
a/text_utils.py
b/text_utils.py
index 76cc7e816db9ae87581989bc314fd7d07d4ca154..564d67eb98c479efdfa00671a98ea179f0f189a8 100644
(file)
--- a/
text_utils.py
+++ b/
text_utils.py
@@
-3,20
+3,25
@@
"""Utilities for dealing with "text"."""
"""Utilities for dealing with "text"."""
+import contextlib
import logging
import math
import sys
from collections import defaultdict
import logging
import math
import sys
from collections import defaultdict
-from typing import Dict, Generator, List, NamedTuple, Optional, Tuple
+from dataclasses import dataclass
+from typing import Dict, Generator, List, Literal, Optional, Tuple
from ansi import fg, reset
logger = logging.getLogger(__file__)
from ansi import fg, reset
logger = logging.getLogger(__file__)
-class RowsColumns(NamedTuple):
- rows: int
- columns: int
+@dataclass
+class RowsColumns:
+ """Row + Column"""
+
+ rows: int = 0
+ columns: int = 0
def get_console_rows_columns() -> RowsColumns:
def get_console_rows_columns() -> RowsColumns:
@@
-31,7
+36,7
@@
def get_console_rows_columns() -> RowsColumns:
).split()
except Exception as e:
logger.exception(e)
).split()
except Exception as e:
logger.exception(e)
- raise Exception('Can\'t determine console size?!')
+ raise Exception('Can\'t determine console size?!')
from e
return RowsColumns(int(rows), int(columns))
return RowsColumns(int(rows), int(columns))
@@
-66,13
+71,13
@@
def bar_graph(
include_text=True,
width=70,
fgcolor=fg("school bus yellow"),
include_text=True,
width=70,
fgcolor=fg("school bus yellow"),
- reset=reset(),
+ reset
_seq
=reset(),
left_end="[",
right_end="]",
) -> str:
"""Returns a string containing a bar graph.
left_end="[",
right_end="]",
) -> str:
"""Returns a string containing a bar graph.
- >>> bar_graph(0.5, fgcolor='', reset='')
+ >>> bar_graph(0.5, fgcolor='', reset
_seq
='')
'[███████████████████████████████████ ] 50.0%'
"""
'[███████████████████████████████████ ] 50.0%'
"""
@@
-99,7
+104,7
@@
def bar_graph(
+ "█" * whole_width
+ part_char
+ " " * (width - whole_width - 1)
+ "█" * whole_width
+ part_char
+ " " * (width - whole_width - 1)
- + reset
+ + reset
_seq
+ right_end
+ " "
+ text
+ right_end
+ " "
+ text
@@
-122,10
+127,10
@@
def sparkline(numbers: List[float]) -> Tuple[float, float, str]:
barcount = len(_bar)
min_num, max_num = min(numbers), max(numbers)
span = max_num - min_num
barcount = len(_bar)
min_num, max_num = min(numbers), max(numbers)
span = max_num - min_num
- s
park
line = ''.join(
+ sline = ''.join(
_bar[min([barcount - 1, int((n - min_num) / span * barcount)])] for n in numbers
)
_bar[min([barcount - 1, int((n - min_num) / span * barcount)])] for n in numbers
)
- return min_num, max_num, s
park
line
+ return min_num, max_num, sline
def distribute_strings(
def distribute_strings(
@@
-259,7
+264,7
@@
def wrap_string(text: str, n: int) -> str:
return out
return out
-class Indenter(
object
):
+class Indenter(
contextlib.AbstractContextManager
):
"""
with Indenter(pad_count = 8) as i:
i.print('test')
"""
with Indenter(pad_count = 8) as i:
i.print('test')
@@
-287,10
+292,11
@@
class Indenter(object):
self.level += 1
return self
self.level += 1
return self
- def __exit__(self, exc_type, exc_value, exc_tb):
+ def __exit__(self, exc_type, exc_value, exc_tb)
-> Literal[False]
:
self.level -= 1
if self.level < -1:
self.level = -1
self.level -= 1
if self.level < -1:
self.level = -1
+ return False
def print(self, *arg, **kwargs):
import string_utils
def print(self, *arg, **kwargs):
import string_utils