-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtext.py
64 lines (46 loc) Β· 1.31 KB
/
text.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
"""
Library text module.
"""
import json
import sys
from typing import Union
# TODO: Also write to log to make it easier to keep track of later.
def eprint(*args, **kwargs):
"""
Print text to stderr.
"""
print(*args, file=sys.stderr, **kwargs)
def prettify(data: Union[list, dict]) -> str:
"""
Return input data structure (list or dict) as a prettified JSON-formatted string.
Default is set here to stringify values like datetime values.
"""
return json.dumps(data, indent=4, sort_keys=True, default=str)
def print_args_on_error(func: object):
"""
Decorator used to print variables given to a function if the function
call fails.
"""
def wrapper(*args, **kwargs):
try:
return func(*args, **kwargs)
except Exception:
print("ARGS")
print(*args)
print("KWARGS")
print(**kwargs)
raise
return wrapper
def parse_bool(value: str):
value = value.lower()
if value == "true":
return True
if value == "false":
return False
raise ValueError(f"Could not parse value to bool. Got: {value}")
def test():
assert parse_bool("true") is True
assert parse_bool("FALSE") is False
assert parse_bool(None) is None
if __name__ == "__main__":
test()