|
@@ -30,33 +30,33 @@ def str_to_list(s: Union[str, Sequence[str]]) -> Sequence[str]:
|
|
|
return s
|
|
|
|
|
|
|
|
|
-def dedent_initial(s: str, n: int=4) -> str:
|
|
|
+def dedent_initial(s: str, n: int = 4) -> str:
|
|
|
"""Remove identation from first line of text."""
|
|
|
return s[n:] if s[:n] == ' ' * n else s
|
|
|
|
|
|
|
|
|
-def dedent(s: str, n: int=4, sep: str='\n') -> str:
|
|
|
+def dedent(s: str, n: int=4, sep: str = '\n') -> str:
|
|
|
"""Remove identation."""
|
|
|
return sep.join(dedent_initial(l) for l in s.splitlines())
|
|
|
|
|
|
|
|
|
-def fill_paragraphs(s: str, width: int, sep: str='\n') -> str:
|
|
|
+def fill_paragraphs(s: str, width: int, sep: str = '\n') -> str:
|
|
|
"""Fill paragraphs with newlines (or custom separator)."""
|
|
|
return sep.join(fill(p, width) for p in s.split(sep))
|
|
|
|
|
|
|
|
|
-def join(l: Sequence[str], sep: str='\n') -> str:
|
|
|
+def join(l: Sequence[str], sep: str = '\n') -> str:
|
|
|
"""Concatenate list of strings."""
|
|
|
return sep.join(v for v in l if v)
|
|
|
|
|
|
|
|
|
-def ensure_sep(sep: str, s: str, n: int=2) -> str:
|
|
|
+def ensure_sep(sep: str, s: str, n: int = 2) -> str:
|
|
|
"""Ensure text s ends in separator sep'."""
|
|
|
return s + sep * (n - s.count(sep))
|
|
|
ensure_newlines = partial(ensure_sep, '\n')
|
|
|
|
|
|
|
|
|
-def abbr(S: str, max: int, ellipsis: str='...') -> str:
|
|
|
+def abbr(S: str, max: int, ellipsis: str = '...') -> str:
|
|
|
"""Abbreviate word."""
|
|
|
if S is None:
|
|
|
return '???'
|
|
@@ -76,12 +76,12 @@ def abbrtask(S: str, max: int) -> str:
|
|
|
return S
|
|
|
|
|
|
|
|
|
-def indent(t: str, indent: int=0, sep: str='\n') -> str:
|
|
|
+def indent(t: str, indent: int = 0, sep: str = '\n') -> str:
|
|
|
"""Indent text."""
|
|
|
return sep.join(' ' * indent + p for p in t.split(sep))
|
|
|
|
|
|
|
|
|
-def truncate(s: str, maxlen: int=128, suffix: str='...') -> str:
|
|
|
+def truncate(s: str, maxlen: int = 128, suffix: str = '...') -> str:
|
|
|
"""Truncate text to a maximum number of characters."""
|
|
|
if maxlen and len(s) >= maxlen:
|
|
|
return s[:maxlen].rsplit(' ', 1)[0] + suffix
|
|
@@ -89,13 +89,14 @@ def truncate(s: str, maxlen: int=128, suffix: str='...') -> str:
|
|
|
|
|
|
|
|
|
def truncate_bytes(s: ByteString,
|
|
|
- maxlen: int=128, suffix: ByteString=b'...') -> ByteString:
|
|
|
+ maxlen: int = 128,
|
|
|
+ suffix: ByteString = b'...') -> ByteString:
|
|
|
if maxlen and len(s) >= maxlen:
|
|
|
return s[:maxlen].rsplit(b' ', 1)[0] + suffix
|
|
|
return s
|
|
|
|
|
|
|
|
|
-def pluralize(n: int, text: str, suffix: str='s') -> str:
|
|
|
+def pluralize(n: int, text: str, suffix: str = 's') -> str:
|
|
|
"""Pluralize term when n is greater than one."""
|
|
|
if n != 1:
|
|
|
return text + suffix
|
|
@@ -103,7 +104,9 @@ def pluralize(n: int, text: str, suffix: str='s') -> str:
|
|
|
|
|
|
|
|
|
def pretty(value: Any,
|
|
|
- width: int=80, nl_width: int=80, sep: str='\n', **kw) -> str:
|
|
|
+ width: int = 80,
|
|
|
+ nl_width: int = 80,
|
|
|
+ sep: str = '\n', **kw) -> str:
|
|
|
"""Format value for printing to console."""
|
|
|
if isinstance(value, dict):
|
|
|
return '{{{0} {1}'.format(sep, pformat(value, 4, nl_width)[1:])
|
|
@@ -120,7 +123,8 @@ def match_case(s: str, other: str) -> str:
|
|
|
|
|
|
|
|
|
def simple_format(s: str, keys: Mapping[str, Any],
|
|
|
- pattern: Pattern=RE_FORMAT, expand: Pattern=r'\1') -> str:
|
|
|
+ pattern: Pattern = RE_FORMAT,
|
|
|
+ expand: Pattern = r'\1') -> str:
|
|
|
"""Format string, expanding abbreviations in keys'."""
|
|
|
if s:
|
|
|
keys.setdefault('%', '%')
|
|
@@ -139,8 +143,7 @@ def simple_format(s: str, keys: Mapping[str, Any],
|
|
|
return s
|
|
|
|
|
|
|
|
|
-def remove_repeating_from_task(task_name, s):
|
|
|
- # type: (str, str) -> str
|
|
|
+def remove_repeating_from_task(task_name: str, s: str) -> str:
|
|
|
"""Given task name, remove repeating module names.
|
|
|
|
|
|
Example:
|
|
@@ -155,8 +158,7 @@ def remove_repeating_from_task(task_name, s):
|
|
|
return remove_repeating(module, s)
|
|
|
|
|
|
|
|
|
-def remove_repeating(substr, s):
|
|
|
- # type: (str, str) -> str
|
|
|
+def remove_repeating(substr: str, s: str) -> str:
|
|
|
"""Remove repeating module names from string.
|
|
|
|
|
|
Arguments:
|