projects
/
python_utils.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Fix a bug, add some testcases.
[python_utils.git]
/
text_utils.py
diff --git
a/text_utils.py
b/text_utils.py
index 4384a1e6134810982e9227d2bb1dfdb517627f72..720bf20561a63bcd5c1afeba42658c8924566adc 100644
(file)
--- a/
text_utils.py
+++ b/
text_utils.py
@@
-3,22
+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):
+@dataclass
+class RowsColumns:
"""Row + Column"""
"""Row + Column"""
- rows: int
- columns: int
+ rows: int
= 0
+ columns: int
= 0
def get_console_rows_columns() -> RowsColumns:
def get_console_rows_columns() -> RowsColumns:
@@
-261,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')
@@
-289,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