-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.py
52 lines (36 loc) · 967 Bytes
/
util.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
from datetime import date
def parse_bool(b):
return b.lower() != "false"
def str_to_int_or_zero(s):
try:
return int(s)
except ValueError:
return 0
def parse_date(s):
try:
return date.fromisoformat(s)
except ValueError:
return None
def is_nonneg_num(s):
try:
return int(s) >= 0
except ValueError:
return False
def clamp(n, minn, maxn):
return max(min(maxn, n), minn)
# Extremely hacky bullshit
def pad_to_length(font, text, length):
text_len = font.measure(text)
if text_len >= length:
return text
space_len = font.measure(" ")
spaces_required = int((length - text_len) / space_len)
spaces = n_spaces(spaces_required)
return text + spaces
FIFTY_SPACES = " "
def n_spaces(n):
spaces = ""
while len(spaces) < n:
spaces += FIFTY_SPACES
spaces = spaces[:n]
return spaces