-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathslow_api.py
95 lines (72 loc) · 2.08 KB
/
slow_api.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
from dataclasses import dataclass
import os
import asyncio
import random
import statistics
from faker import Faker
from pyinstrument import Profiler
from process_ml_batch import process_ml_batch
fake = Faker()
@dataclass
class NameFacts:
name: str
unknown: bool
gender: str = "N/A"
meaning: str = "N/A"
popularity: int = 0
age_stats: dict = None
def get_known_names() -> [str]:
path = os.path.realpath(
os.path.join(os.getcwd(), os.path.dirname(__file__), "first_names.all.txt")
)
with open(path, encoding="utf-8") as f:
return [l.strip() for l in f.readlines()]
# noinspection PyUnusedLocal
async def get_gender(name):
await asyncio.sleep(0.01)
return "boy"
# noinspection PyUnusedLocal
async def get_meaning(name):
await asyncio.sleep(0.01)
return fake.paragraph(nb_sentences=3, variable_nb_sentences=True)
# noinspection PyUnusedLocal
async def get_popularity(name):
await asyncio.sleep(0.01)
return 100
async def get_name_facts(name):
name = name.lower()
known_names = get_known_names()
if name not in known_names:
return NameFacts(name=name, unknown=True)
gender = await get_gender(name)
meaning = await get_meaning(name)
popularity = await get_popularity(name)
process_ml_batch(name)
return NameFacts(
name=name,
unknown=False,
gender=gender,
meaning=meaning,
popularity=popularity,
age_stats=get_age_stats(name),
)
# noinspection PyUnusedLocal
def get_ages(name):
return [random.randint(1, 100) for _ in range(10000)]
def get_age_stats(name):
ages = get_ages(name)
return {
"mean": statistics.mean(ages),
"median": statistics.median(ages),
"most_common": statistics.mode(ages),
"oldest": max(ages),
}
async def main():
names = [await get_name_facts("Anis") for _ in range(50)]
print(names)
if __name__ == "__main__":
# profiler = Profiler()
# profiler.start()
asyncio.run(main())
# profiler.stop()
# print(profiler.output_text(unicode=True, color=True))