diff options
author | noptuno <repollo.marrero@gmail.com> | 2023-04-28 02:29:30 +0200 |
---|---|---|
committer | noptuno <repollo.marrero@gmail.com> | 2023-04-28 02:29:30 +0200 |
commit | 355dee533bb34a571b9367820a63cccb668cf866 (patch) | |
tree | 838af886b4fec07320aeb10f0d1e74ba79e79b5c /venv/lib/python3.9/site-packages/pympler/util/stringutils.py | |
parent | added pyproject.toml file (diff) | |
download | gpt4free-355dee533bb34a571b9367820a63cccb668cf866.tar gpt4free-355dee533bb34a571b9367820a63cccb668cf866.tar.gz gpt4free-355dee533bb34a571b9367820a63cccb668cf866.tar.bz2 gpt4free-355dee533bb34a571b9367820a63cccb668cf866.tar.lz gpt4free-355dee533bb34a571b9367820a63cccb668cf866.tar.xz gpt4free-355dee533bb34a571b9367820a63cccb668cf866.tar.zst gpt4free-355dee533bb34a571b9367820a63cccb668cf866.zip |
Diffstat (limited to 'venv/lib/python3.9/site-packages/pympler/util/stringutils.py')
-rw-r--r-- | venv/lib/python3.9/site-packages/pympler/util/stringutils.py | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/venv/lib/python3.9/site-packages/pympler/util/stringutils.py b/venv/lib/python3.9/site-packages/pympler/util/stringutils.py new file mode 100644 index 00000000..7b09e8c3 --- /dev/null +++ b/venv/lib/python3.9/site-packages/pympler/util/stringutils.py @@ -0,0 +1,77 @@ +""" +String utility functions. +""" + +from typing import Any, Optional, Union + + +def safe_repr(obj: Any, clip: Optional[int] = None) -> str: + """ + Convert object to string representation, yielding the same result a `repr` + but catches all exceptions and returns 'N/A' instead of raising the + exception. Strings may be truncated by providing `clip`. + + >>> safe_repr(42) + '42' + >>> safe_repr('Clipped text', clip=8) + 'Clip..xt' + >>> safe_repr([1,2,3,4], clip=8) + '[1,2..4]' + """ + try: + s = repr(obj) + if not clip or len(s) <= clip: + return s + else: + return s[:clip - 4] + '..' + s[-2:] + except: + return 'N/A' + + +def trunc(obj: str, max: int, left: bool = False) -> str: + """ + Convert `obj` to string, eliminate newlines and truncate the string to + `max` characters. If there are more characters in the string add ``...`` to + the string. With `left=True`, the string can be truncated at the beginning. + + @note: Does not catch exceptions when converting `obj` to string with + `str`. + + >>> trunc('This is a long text.', 8) + This ... + >>> trunc('This is a long text.', 8, left=True) + ...text. + """ + s = str(obj) + s = s.replace('\n', '|') + if len(s) > max: + if left: + return '...' + s[len(s) - max + 3:] + else: + return s[:(max - 3)] + '...' + else: + return s + + +def pp(i: Union[int, float], base: int = 1024) -> str: + """ + Pretty-print the integer `i` as a human-readable size representation. + """ + degree = 0 + pattern = "%4d %s" + while i > base: + pattern = "%7.2f %s" + i = i / float(base) + degree += 1 + scales = ['B', 'KB', 'MB', 'GB', 'TB', 'EB'] + return pattern % (i, scales[degree]) + + +def pp_timestamp(t: Optional[float]) -> str: + """ + Get a friendly timestamp represented as a string. + """ + if t is None: + return '' + h, m, s = int(t / 3600), int(t / 60 % 60), t % 60 + return "%02d:%02d:%05.2f" % (h, m, s) |