From d9ca07e208a67c7f86dcb36b0dc48ddbaac74501 Mon Sep 17 00:00:00 2001 From: "Thomas J. Kennedy" <tkennedy@cs.odu.edu> Date: Tue, 2 Jun 2020 14:52:01 -0400 Subject: [PATCH] Update Python Temperature Input library to use a regex --- .../python3/parseTempsDemo.py | 5 +---- .../python3/parse_temps.py | 19 +++++++------------ 2 files changed, 8 insertions(+), 16 deletions(-) diff --git a/SemesterProject-CPU-Temps/python3/parseTempsDemo.py b/SemesterProject-CPU-Temps/python3/parseTempsDemo.py index 9f1f6bb..31ee534 100755 --- a/SemesterProject-CPU-Temps/python3/parseTempsDemo.py +++ b/SemesterProject-CPU-Temps/python3/parseTempsDemo.py @@ -13,12 +13,9 @@ def main(): """ input_temps = sys.argv[1] - includes_units = sys.argv[2] == "yes" # set to False for files without units - - print(includes_units) with open(input_temps, 'r') as temps_file: - for temps_as_floats in parse_raw_temps(temps_file, units=includes_units): + for temps_as_floats in parse_raw_temps(temps_file): print(temps_as_floats) diff --git a/SemesterProject-CPU-Temps/python3/parse_temps.py b/SemesterProject-CPU-Temps/python3/parse_temps.py index 07d7189..1954636 100755 --- a/SemesterProject-CPU-Temps/python3/parse_temps.py +++ b/SemesterProject-CPU-Temps/python3/parse_temps.py @@ -7,13 +7,12 @@ All code may be used freely in the semester project, iff it is imported using represents one or more functions. """ - +import re from typing import (TextIO, Iterator, List, Tuple) def parse_raw_temps(original_temps: TextIO, - step_size: int = 30, - units: bool = True) -> Iterator[Tuple[float, List[float]]]: + step_size: int = 30) -> Iterator[Tuple[float, List[float]]]: """ Take an input file and time-step size and parse all core temps. @@ -22,17 +21,13 @@ def parse_raw_temps(original_temps: TextIO, step_size: time-step in seconds - units: True if the input file includes units and False if the file - includes only raw readings (no units) - Yields: A tuple containing the next time step and a List containing _n_ core temps as floating point values (where _n_ is the number of CPU cores) """ - if units: - for step, line in enumerate(original_temps): - yield (step * step_size), [float(entry[:-2]) for entry in line.split()] - else: - for step, line in enumerate(original_temps): - yield (step * step_size), [float(entry) for entry in line.split()] + split_re = re.compile(r"[^0-9]*\s+|[^0-9]*$") + + for step, line in enumerate(original_temps): + yield (step * step_size), \ + [float(entry) for entry in split_re.split(line) if len(entry) > 0] -- GitLab