-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcython_fast_api.pyx
110 lines (87 loc) · 2.53 KB
/
cython_fast_api.pyx
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
103
104
105
106
107
108
109
110
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())
cdef class NameFacts:
def __init__(self,
name,
unknown,
gender,
meaning,
popularity,
age_stats, ):
self.name = name
self.unknown = unknown
self.gender = gender
self.meaning = meaning
self.popularity = popularity
self.age_stats = age_stats
def __str__(self):
return f"""
name : {self.name}
unknown : {self.unknown}
gender : {self.gender}
meaning : {self.meaning}
popularity : {self.popularity}
age_stats : {self.age_stats}
"""
@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)