-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathutils.py
More file actions
52 lines (38 loc) · 1.55 KB
/
utils.py
File metadata and controls
52 lines (38 loc) · 1.55 KB
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 datetime, timezone
from enum import Enum
STRING_SEPARATOR = " | "
class InlineList(list):
"""Class wrapper to indicate that the list should be written as one line in YAML.
Used for 'bbox' property."""
pass
def is_valid_string(text):
if len(str(text).replace(" ", "")) >= 1:
return True
return False
def get_enum_value_from_string(enum_type: Enum, text: str):
if isinstance(enum_type, type) and issubclass(enum_type, Enum):
for member in enum_type:
if text == member.value:
return member
# handle case with non-string enums
for member in enum_type:
if text == str(member.value) or (text == "" and member.value is None):
return member
raise ValueError(f"Unexpected value '{text}', expected type: '{enum_type}'")
def bbox_from_list(raw_bbox_list: list):
# this loop is to not add empty decimals unnecessarily
list_bbox_val = []
for part in raw_bbox_list:
if isinstance(part, str): # if the list is read from UI widgets
part = part.strip()
if "." in part:
list_bbox_val.append(float(part))
else:
list_bbox_val.append(int(part))
else: # if the list is already taken from actual data (int or float)
list_bbox_val.append(part)
if len(list_bbox_val) != 4 and len(list_bbox_val) != 6:
raise ValueError(
f"Wrong number of values: {len(list_bbox_val)}. Expected: 4 or 6"
)
return InlineList(list_bbox_val)