From 20e0b28ec3c8e9e0a63759116a32438c18941a59 Mon Sep 17 00:00:00 2001 From: Charles Cabergs Date: Sun, 27 Sep 2020 16:58:35 +0200 Subject: Added log checking --- src/philo/philo.py | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 src/philo/philo.py (limited to 'src/philo/philo.py') diff --git a/src/philo/philo.py b/src/philo/philo.py new file mode 100644 index 0000000..e7e2e1d --- /dev/null +++ b/src/philo/philo.py @@ -0,0 +1,50 @@ +# ############################################################################ # +# # +# ::: :::::::: # +# philo.py :+: :+: :+: # +# +:+ +:+ +:+ # +# By: charles +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2020/09/27 12:54:12 by charles #+# #+# # +# Updated: 2020/09/27 16:50:22 by charles ### ########.fr # +# # +# ############################################################################ # + +import itertools +from philo.event import Event + +class Philo: + def __init__(self, id_: int, meal_num: int = 1): + self._logs = [] + self.id = id_ + self.meal_num = meal_num + + def add_log(self, log): + self._logs.append(log) + + def check(self): + grouped = [(e, list(g)) for e, g in itertools.groupby(self._logs, (lambda x: x.event))] + for e, g in grouped: + if e is Event.EATING: + if len(g) != self.meal_num: + raise RuntimeError("lala") + else: + if len(g) != 1: + raise RuntimeError("1lala") + + events = [e for e, _ in grouped] + for e1, e2 in zip(events, events[1:]): + if e2 is Event.DIED: + break + if e1 is Event.THINKING and e2 is not Event.EATING: + raise RuntimeError("2lala") + elif e1 is Event.EATING and e2 is not Event.SLEEPING: + raise RuntimeError("2lala") + elif e1 is Event.SLEEPING and e2 is not Event.EATING: + raise RuntimeError("2lala") + + @property + def last_event(self): + if len(self._logs) == 0: + return Event.NONE + return self._logs[-1].event -- cgit