-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
47 lines (41 loc) · 1.53 KB
/
utils.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
from datetime import datetime
def calculate_duration_in_seconds(time_start: str, time_end: str) -> int:
"""
Calculate the duration in seconds between two time strings.
:param time_start:
:param time_end:
:return:
"""
time_start = datetime.strptime(time_start, '%H:%M')
time_end = datetime.strptime(time_end, '%H:%M')
duration = time_end - time_start
return duration.seconds
def save_json_to_file(json_obj, file_name):
"""
Save the json schedule to a file.
:param json_obj:
:param file_name:
:return:
"""
import json
with open(file_name, 'w') as json_file:
# add indent=4 to make the json file more readable
json.dump(json_obj, json_file, indent=4)
def parse_json_from_response_text(text: str) -> dict:
# parse the response text to a json object
# Let's do something manually:
# sometimes GPT responds with something BEFORE the braces:
# "I'm sorry, I don't understand. Please try again."
# {"text": "I'm sorry, I don't understand. Please try again.",
# "confidence": 0.0}
# So let's try to find the first brace and then parse the rest
# of the string
try:
brace_index = text.index("{")
maybe_fixed_json = text[brace_index:]
last_brace_index = maybe_fixed_json.rindex("}")
maybe_fixed_json = maybe_fixed_json[: last_brace_index + 1]
actions = eval(maybe_fixed_json)
return actions
except SyntaxError:
assert False, "The response text is not a valid json object: " + text