-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfast_api.py
102 lines (78 loc) · 2.22 KB
/
fast_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
96
97
98
99
100
101
102
from dataclasses import dataclass
import os
import asyncio
from functools import lru_cache
from faker import Faker
from pyinstrument import Profiler
from redis import Redis
from rq import Queue
import numpy
from process_ml_batch import process_ml_batch
fake = Faker()
q = Queue(connection=Redis())
@dataclass
class NameFacts:
name: str
unknown: bool
gender: str = "N/A"
meaning: str = "N/A"
popularity: int = 0
age_stats: dict = None
@lru_cache(maxsize=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, meaning, popularity = await asyncio.gather(
get_gender(name),
get_meaning(name),
get_popularity(name),
)
q.enqueue(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 numpy.random.randint(100, size=100)
def get_age_stats(name):
ages = numpy.array(get_ages(name))
return {
"mean": numpy.mean(ages),
"median": numpy.median(ages),
"most_common": numpy.bincount(ages).argmax(),
"oldest": numpy.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))