diff options
| author | Charles Cabergs <me@cacharle.xyz> | 2020-10-01 11:49:53 +0200 |
|---|---|---|
| committer | Charles Cabergs <me@cacharle.xyz> | 2020-10-01 11:49:53 +0200 |
| commit | 1f18e740539aed751865ecff9d0f3cba44230e54 (patch) | |
| tree | e23254751cc5a3be551233efb979a00571f40dc6 /src/philo/log.py | |
| parent | 763f02a8b1e69c0e26a088824981d23ba1e5386d (diff) | |
| download | philosophers_test-1f18e740539aed751865ecff9d0f3cba44230e54.tar.gz philosophers_test-1f18e740539aed751865ecff9d0f3cba44230e54.tar.bz2 philosophers_test-1f18e740539aed751865ecff9d0f3cba44230e54.zip | |
Refactoring file structure, Added summary
Diffstat (limited to 'src/philo/log.py')
| -rw-r--r-- | src/philo/log.py | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/src/philo/log.py b/src/philo/log.py new file mode 100644 index 0000000..f5817d0 --- /dev/null +++ b/src/philo/log.py @@ -0,0 +1,47 @@ +# ############################################################################ # +# # +# ::: :::::::: # +# log.py :+: :+: :+: # +# +:+ +:+ +:+ # +# By: cacharle <me@cacharle.xyz> +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2020/10/01 10:51:39 by cacharle #+# #+# # +# Updated: 2020/10/01 11:30:53 by cacharle ### ########.fr # +# # +# ############################################################################ # + +import re + +from .event import Event +from . import error + +class Log: + def __init__(self, line: str, philo_num, start_time, end_time): + match = re.match( + r"^(?P<timestamp>\d+) " + r"(?P<id>\d+) " + r"(?P<event>is thinking|is eating|is sleeping|died|has taken fork)$", + line + ) + if match is None: + raise error.Format(line, "wrong format") + + self._line = line + self.id = self._parse_ranged_int(match.group("id"), 1, philo_num) + self.timestamp = self._parse_ranged_int( + match.group("timestamp"), start_time, end_time) + + self.event = Event.from_string(match.group('event')) + + def _parse_ranged_int(self, s, lo, hi): + try: + value = int(s) + if not (lo <= value <= hi): + raise error.Format(self._line, + "{} should be between {} - {}".format(s, lo, hi)) + except ValueError: + raise error.Format(self._line, "{} sould be an integer".format(s)) + return value + + def __repr__(self): + return "{}ms #{} {}".format(self.timestamp, self.id, self.event) |
