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/log.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/log.py')
| -rw-r--r-- | src/philo/log.py | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/src/philo/log.py b/src/philo/log.py new file mode 100644 index 0000000..cc4347e --- /dev/null +++ b/src/philo/log.py @@ -0,0 +1,53 @@ +# ############################################################################ # +# # +# ::: :::::::: # +# log.py :+: :+: :+: # +# +:+ +:+ +:+ # +# By: charles <me@cacharle.xyz> +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2020/09/27 16:04:18 by charles #+# #+# # +# Updated: 2020/09/27 16:05:21 by charles ### ########.fr # +# # +# ############################################################################ # + +import re +import time + +import philo + + +class Log: + def __init__(self, log, philo_num): + match = re.match( + "^(?P<timestamp>\d+) " + "(?P<id>\d+) " + "(?P<event>is thinking|is eating|is sleeping|died)$", + log + ) + if match is None: + raise ValueError("Bad line format |{}|".format(log)) + + curr = int(time.time() * 1000) + self.timestamp = Log._parse_ranged_int(match.group("timestamp"), curr - 100, curr + 100) + self.id = Log._parse_ranged_int(match.group("id"), 1, philo_num) + + self.event = { + "is thinking": philo.Event.THINKING, + "is eating": philo.Event.EATING, + "is sleeping": philo.Event.SLEEPING, + "died": philo.Event.DIED, + }[match.group('event')] + + @staticmethod + def _parse_ranged_int(s, lo, hi): + try: + value = int(s) + if not (lo <= value <= hi): + raise ValueError("Invalid value range {}".format(s)) + except ValueError: + raise ValueError("Invalid value {}".format(s)) + return value + + + def __repr__(self): + return "{} {} {}".format(self.timestamp, self.id, self.event) |
