-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqualifier.py
53 lines (42 loc) · 1.8 KB
/
qualifier.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
import random
import typing
from dataclasses import dataclass
@dataclass(frozen=True)
class Request:
scope: typing.Mapping[str, typing.Any]
receive: typing.Callable[[], typing.Awaitable[object]]
send: typing.Callable[[object], typing.Awaitable[None]]
class RestaurantManager:
def __init__(self):
"""Instantiate the restaurant manager.
This is called at the start of each day before any staff get on
duty or any orders come in. You should do any setup necessary
to get the system working before the day starts here; we have
already defined a staff dictionary.
"""
self.staff = {}
async def __call__(self, request: Request):
"""Handle a request received.
This is called for each request received by your application.
In here is where most of the code for your system should go.
:param request: request object
Request object containing information about the sent
request to your application.
"""
if request.scope['type'] == 'staff.offduty':
self.staff.pop(request.scope['id'])
elif request.scope['type'] == 'staff.onduty':
self.staff[request.scope['id']] = request
elif request.scope['type'] == 'order':
found = self.assign_staff(request)
full_order = await request.receive()
await found.send(full_order)
result = await found.receive()
await request.send(result)
def assign_staff(self, request):
for key in self.staff:
speciality = self.staff[key].scope['speciality']
for x in speciality:
if x == request.scope['speciality']:
return self.staff[key]
return self.staff[random.choice(list(self.staff.keys()))]