diff options
| author | Charles Cabergs <me@cacharle.xyz> | 2020-09-27 16:58:35 +0200 |
|---|---|---|
| committer | Charles Cabergs <me@cacharle.xyz> | 2020-09-27 16:58:35 +0200 |
| commit | 20e0b28ec3c8e9e0a63759116a32438c18941a59 (patch) | |
| tree | ee426189825867a0d98358154da3660c48ca5957 /src/philo/philo.py | |
| parent | 861621b9bdbdc7336183597b3ffd4ee161be19f3 (diff) | |
| download | philosophers_test-20e0b28ec3c8e9e0a63759116a32438c18941a59.tar.gz philosophers_test-20e0b28ec3c8e9e0a63759116a32438c18941a59.tar.bz2 philosophers_test-20e0b28ec3c8e9e0a63759116a32438c18941a59.zip | |
Added log checking
Diffstat (limited to 'src/philo/philo.py')
| -rw-r--r-- | src/philo/philo.py | 50 |
1 files changed, 50 insertions, 0 deletions
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 <me@cacharle.xyz> +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# 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 |
