-
Notifications
You must be signed in to change notification settings - Fork 0
/
rescuedog.py
326 lines (308 loc) · 11.9 KB
/
rescuedog.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
import asyncio
import time, random
from datetime import datetime
async def custom_sleep(x):
# print('Sleep', datetime.now())
await asyncio.sleep(x)
async def empty_space():
print('this is empty. A waste of time.')
conditions_descriptions = {'unhealthy':'healing','untrimmed':'grooming','hungry':'feeding'}
# Dog Object that holds a dog
# includes name, age, breed
# the last element was state... but I think I'm going to include multiples
# hair: trimmed/untrimmed
# health: healthy/unhealthy
# hunger: fed/hungry
# or condition = ['untrimmed','unhealthy','hungry'] then... either remove conditions or replace them with trimmed,healthy,fed
# a dog that is trimmed, healthy and hungry is either ready for sale? or moved from the Pen
class Dog:
def __init__(self,name,age,breed,condition=['untrimmed','unhealthy','hungry']):
self.name = name
self.age = age
self.breed = breed
self.condition = condition
def __repr__(self):
# return {'name:':self.name, 'age:':self.age, 'breed:':self.breed,'cond:':self.condition}
return f"Name: {self.name}, Age: {self.age}, Breed: {self.breed}, Cond:{self.condition}"
def groom(self):
if 'untrimmed' in self.condition:
self.condition.remove('untrimmed')
def heal(self):
if 'unhealthy' in self.condition:
self.condition.remove('unhealthy')
def feed(self):
if 'hungry' in self.condition:
self.condition.remove('hungry')
# A Pen
# holds dogs
# It can either be a list object that holds the dogs or
# it can be something more interesting that calls the controller object to alert them of incoming dogs
# does the pen work autonomously from the controller? or does the controller field new dogs and pen them?
class employee:
async def routine(self,clinic):
cycle = 1
while clinic.running:
sleep_time = 0
try: #if self.repair in clinic.conditions['unhealthy']:
if clinic.conditions[self.repair] > 0:
dog = clinic.get_dog(self.repair)
clinic.rem_dog_pen(dog)
print(f'{self.name} is {conditions_descriptions[self.repair]} {dog.name}')
await custom_sleep(self.repair_time)
if self.repair in dog.condition:
dog.condition.remove(self.repair)
clinic.add_dog_pen(dog)
else:
cycle += 1
if cycle > 10:
sleep_time = cycle//10
elif cycle > 100:
sleep_time = 10
else:
sleep_time = 1
if cycle%10 == 0:
print(f'{self.name} sleeping for {sleep_time} seconds.')
await custom_sleep(sleep_time)
except:
print(f'An exception occured for {self.name}.')
await custom_sleep(1)
# Vet - heals the dog
class Vet(employee):
population = 0
def __init__(self):
Vet.population += 1
self.name = "Vet #" + str(Vet.population)
self.repair = 'unhealthy'
self.repair_time = 10
print('Acquireing {}'.format(self.name))
def __repr__(self):
return self.name
async def treat_dog(self,dog):
if 'unhealthy' in dog.condition:
dog.heal()
await custom_sleep(2)
# groomer - grooms dog
class Groomer(employee):
population = 0
def __init__(self):
Groomer.population += 1
self.name = "Groomer #" + str(Groomer.population)
self.repair = 'untrimmed'
self.repair_time = 2
def __repr__(self):
return self.name
async def groom_dog(self, dog):
if 'untrimmed' in dog.condition:
dog.groom()
await custom_sleep(2)
# trainer - feeds dog
class Trainer(employee):
population = 0
def __init__(self):
Trainer.population += 1
self.name = "Trainer #" + str(Trainer.population)
self.repair = 'hungry'
self.repair_time = 2
def __repr__(self):
return self.name
async def feed_dog(self, dog):
if 'hungry' in dog.condition:
dog.feed()
await custom_sleep(2)
class Valet:
population = 0
def __init__(self):
Valet.population += 1
self.name = "Valet #" + str(Valet.population)
def __repr__(self):
return self.name
async def enter_dog(self,dog,clinic):
await custom_sleep(1)
# clinic.rem_dog_entry(dog)
clinic.add_dog_pen(dog)
def exit_dog(self,dog,clinic):
# await custom_sleep(1)
clinic.rem_dog_pen(dog)
clinic.add_dog_exit(dog)
async def routine(self,clinic):
cycle = 0
while clinic.running:
try:
sleep_time = 0
if len(clinic.entry_queue)>0:
dog = clinic.entry_queue.pop()
await self.enter_dog(dog,clinic)
print(f'{self.name} transporting {dog.name} to pen.')
elif 'Ready for home' in clinic.conditions and clinic.conditions['Ready for home'] > 0:
dog = clinic.get_dog('Ready for home')
clinic.rem_dog_pen(dog)
print(f'{self.name} returning {dog.name} to customer.')
await custom_sleep(10)
clinic.add_dog_exit(dog)
else:
cycle += 1
if cycle > 10:
sleep_time = cycle // 10
elif cycle > 100:
sleep_time = 10
else:
sleep_time = 1
if cycle % 10 == 0:
print(f'{self.name} sleeping.')
await custom_sleep(sleep_time)
except:
print('exception in clinic.routine()')
''' clinic class will hold the employees and the animals
entry_queue simulates the clinic waiting room. Dogs are brought in and sent to the pen
the employees gather them from the pen and return them when finished
the dog_return_que simulates dogs that have been returned'''
class Clinic:
def __init__(self, dogqueue=[], veterinarians=[], groomers=[], trainers=[], valets=[]):
self.entry_queue = dogqueue
self.pen = []
self.exit_queue = []
self.veterinarians = veterinarians
self.groomers = groomers
self.trainers = trainers
self.valets = valets
self.running = True
self.dogs_to_treat = 0
self.conditions = {}
def generate_clinic(self,no_dogs,no_vets,no_groomers,no_trainers,no_valets):
''' This will setup the main handler with animals and employees'''
breeds = ['Retriever','Terrier','Dane','Mutt','Poodle','Bulldog','Wolf','Sheepdog']
for i in range(no_dogs):
name = 'Dog #' + str(i+1)
age = int((random.randint(1,15) + random.randint(1,15))/2)
condition = ['untrimmed','unhealthy','hungry']
breed = random.choice(breeds)
dog = Dog(name,age,breed,condition)
self.entry_queue.append(dog)
self.dogs_to_treat += 1
for i in range(no_vets):
vet = Vet()
self.veterinarians.append(vet)
for i in range(no_groomers):
groomer = Groomer()
self.groomers.append(groomer)
for i in range(no_trainers):
trainer = Trainer()
self.trainers.append(trainer)
for i in range(no_valets):
valet = Valet()
self.valets.append(valet)
def add_dog_pen(self, dog):
if len(dog.condition) == 0:
dog.condition.append('Ready for home')
for cond in dog.condition:
if cond in self.conditions:
self.conditions[cond] += 1
else:
self.conditions[cond] = 1
self.pen.append(dog)
def rem_dog_pen(self, dog):
for cond in dog.condition:
if cond in self.conditions:
self.conditions[cond] -= 1
else:
print('error in rem_dog_pen')
self.pen.remove(dog)
def rem_dog_entry(self, dog):
self.entry_queue.remove(dog)
def add_dog_entry(self,dog):
self.entry_queue.append(dog)
def add_dog_exit(self,dog):
self.exit_queue.append(dog)
def rem_dog_exit(self,dog):
self.exit_queue.remove(dog)
def get_dog(self,condition):
for dog in self.pen:
if condition in dog.condition:
return dog
print(f'get_dog failed to find a dog with the condition: {condition}')
def dog_count_entry(self):
x = len(self.entry_queue)
return x
def dog_count_pen(self):
x = len(self.pen)
return x
def vet_count(self):
return len(self.veterinarians)
def groom_count(self):
return len(self.groomers)
def train_count(self):
return len(self.trainers)
def valet_count(self):
return len(self.valets)
async def routine(self):
try:
while self.running:
print(f"{clinic.conditions}")
print(f"Dogs Entry: {len(clinic.entry_queue)} Dogs Pen: {len(clinic.pen)} Dogs Exit: {len(clinic.exit_queue)}")
await custom_sleep(10)
if self.dogs_to_treat == len(self.exit_queue):
print(f'Clinic Routine exit {self.dogs_to_treat} and exit_queue {len(self.exit_queue)}')
self.running = False
except:
print('Error in clinic.routine()')
if __name__ == "__main__":
# doglist = [['jake',8,'terrier',['untrimmed','unhealthy','hungry']]]
# for animal in doglist:
# start_time = datetime.now()
# dog = Dog(doglist[0][0],doglist[0][1],doglist[0][2],doglist[0][3])
# print(dog.name, dog.condition)
# vet = Vet()
# groomer = Groomer()
# trainer = Trainer()
# loop = asyncio.get_event_loop()
# tasks = [vet.treat_dog(dog),empty_space(),groomer.groom_dog(dog),trainer.feed_dog(dog)]
# # vet.treat_dog(dog)
# loop.run_until_complete(asyncio.wait(tasks))
# loop.close()
# print(dog.name,dog.condition)
# finish_time = datetime.now()
# print(finish_time-start_time)
clinic = Clinic()
clinic.generate_clinic(5,2,2,2,2)
print(clinic.dog_count_entry(), clinic.entry_queue)
print(clinic.vet_count(), clinic.veterinarians)
print(clinic.groom_count(),clinic.groomers)
print(clinic.train_count(),clinic.trainers)
print(clinic.valet_count(),clinic.valets)
# for dog in clinic.entry_queue[:]:
# valet = clinic.valets[0]
# valet.enter_dog(dog,clinic)
# for groomer in clinic.groomers:
# print(groomer)
# for vet in clinic.veterinarians:
# print(vet)
# for valet in clinic.valets:
# print(valet)
# for dog in clinic.entry_queue[:]:
print("entry queue",clinic.dog_count_entry(),clinic.entry_queue)
print('Pen: ', clinic.pen)
print(clinic.conditions)
print("moving dogs...")
loop = asyncio.get_event_loop()
tasks = []
employees = []
for valet in clinic.valets:
tasks.append(valet.routine(clinic))
for vet in clinic.veterinarians:
tasks.append(vet.routine(clinic))
for groomer in clinic.groomers:
tasks.append(groomer.routine(clinic))
for trainer in clinic.trainers:
tasks.append(trainer.routine(clinic))
tasks.append(clinic.routine())
# employees.append(trainer)
# tasks = [employees[0].routine()]
# vet.treat_dog(dog)
loop.run_until_complete(asyncio.wait(tasks))
loop.close()
# for dog in clinic.pen[:]:
# valet.exit_dog(dog,clinic)
# # clinic.rem_dog_pen(dog)
print('Pen: ',clinic.pen)
print(clinic.conditions)
print('Exit_queue: ', clinic.exit_queue)