-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresponses.py
30 lines (27 loc) · 943 Bytes
/
responses.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
from json import load, dump
from random import choice, choices
namesRepo = {}
def get_response(num_middle_names: int) -> str:
first: str = choice(namesRepo["first"])
first.rstrip('\n')
first.strip()
middle_names_list: list[str] = choices(namesRepo["middle"], k=num_middle_names)
middle: str = " ".join(middle_names_list)
last: str = choice(namesRepo["last"])
last.rstrip('\n')
last.strip()
if len(middle_names_list) < 1:
return first + " " + last
return first + " " + middle + " " + last
def load_json() -> None:
global namesRepo
try:
with open('first.json', 'r') as f:
namesRepo["first"] = load(f)
with open('middle.json', 'r') as f:
namesRepo["middle"] = load(f)
with open('last.json', 'r') as f:
namesRepo["last"] = load(f)
except Exception as e:
print("Could not load json, see stack trace")
print(e)