This repository was archived by the owner on Aug 11, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
68 lines (55 loc) · 1.9 KB
/
utils.py
File metadata and controls
68 lines (55 loc) · 1.9 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import datetime
from constants import *
def read_date(date):
if date[0:4] == '2021':
date = '2020' + date[4:]
return datetime.datetime.strptime(date[0:10], '%Y-%m-%d')
def write_date(date):
return date.strftime("%B %d")
def unlist(var):
if type(var) == list:
return var[0]
else:
return var
def dict_stringify(entities):
## Converts dictionary of lists/strings to a string separated by ,
## TODO: Simplify using json.dumps(entities)
str_ = ""
for key in sorted(entities.keys()) :
if len(entities[key]) > 0:
str_ += key + ":["
if type(entities[key]) == str:
str_ += entities[key]
elif type(entities[key]) == list:
str_ += ','.join(sorted(entities[key]))
str_ += "],"
return str_
def get_context_from_request(req, context_name):
context_id = req["session"] + "/contexts/"+context_name
context = {}
for item in req["queryResult"]["outputContexts"]:
if item['name'] == context_id:
context = item["parameters"] if "parameters" in item else {}
return context_id, context
def get_updated_context(params, context):
# TODO: Construct a list of history instead of replacing with new? Set expiry?
for param in params:
if params[param]:
context['ctx_' + param] = params[param]
return context
def clear_from_context(context, fields):
for field in fields:
if context_index[field] in context:
context[context_index[field]] = ''
return context
def read_entry(entities, context, field):
entry = None
if entities_index[field] in entities and len(entities[entities_index[field]]) > 0:
entry = entities[entities_index[field]]
elif context_index[field] in context and len(context[context_index[field]]) > 0:
entry = context[context_index[field]]
else:
entry = default_values[field]
return unlist(entry)
def read_entry_arr(entities, context, fields):
return [read_entry(entities, context, x) for x in fields]