Complete in Python.
Attached are the two Python files given.
Attached is parseTempsDemo.py:
#! /usr/bin/env python3
import sys
from parse_temps import (parse_raw_temps)
def main():
"""
This main function serves as the driver for the demo. Such functions
are not required in Python. However, we want to prevent unnecessary module
level (i.e., global) variables.
"""
input_temps = sys.argv[1]
with open(input_temps, 'r') as temps_file:
# -----
# Output raw structure
# ------
for temps_as_floats in parse_raw_temps(temps_file):
print(temps_as_floats)
with open(input_temps, 'r') as temps_file:
# ------
# Split data
# ----
for temps_as_floats in parse_raw_temps(temps_file):
time, core_data = temps_as_floats
print(f"{time = } | {core_data = }")
with open(input_temps, 'r') as temps_file:
# --------
# Split Data
# --------
times = []
core_0_data = []
core_1_data = []
core_2_data = []
core_3_data = []
for time, core_data in parse_raw_temps(temps_file):
times.append(time)
core_0_data.append(core_data[0])
core_1_data.append(core_data[1])
core_2_data.append(core_data[2])
core_3_data.append(core_data[3])
print(f"{times[:4] = }")
print(f"{core_0_data[:4] = }")
for time, *temps in list(zip(times, core_0_data, core_1_data, core_2_data, core_3_data))[4:]:
print(f"{time=} {temps=}")
with open(input_temps, 'r') as temps_file:
# ------
# Split Data, but Better!
# -----------
times = []
core_data = [[] for _ in range(0, 4)]
for time, raw_core_data in parse_raw_temps(temps_file):
times.append(time)
for core_idx, reading in enumerate(raw_core_data):
core_data[core_idx].append(reading)
for time, *temps in list(zip(times, *core_data))[4:]:
print(f"{time=} {temps=}")
if __name__ == "__main__":
main()
Attached is parse_temps.py:
#! /usr/bin/env python3
"""
This module is a collection of input helpers for the CPU Temperatures Project.
All code may be used freely in the semester project, iff it is imported using
``import parse_temps`` or ``from parse_temps import {...}`` where ``{...}``
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) -> Iterator[Tuple[float, List[float]]]:
"""
Take an input file and time-step size and parse all core temps.
Args:
original_temps: an input file
step_size: time-step in seconds
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)
"""
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]
Your program must accept an input filename as the first command line argument. Your program must NOT prompt the user for a filename.
All code must be properly and fully documented using a language appropriate comment style. All functions (including parameters and return types) must be documented.
Input Format:
Data takes the form of temperatures in a .txt file. All data points are whitespace delimited.
Attached is the input file “input_temps”:
61.0 63.0 50.0 58.0
80.0 81.0 68.0 77.0
62.0 63.0 52.0 60.0
83.0 82.0 70.0 79.0
68.0 69.0 58.0 65.0
Each line represents temperature readings from 4 processor cores. Readings are taken every 30 seconds. In this example:
• line 1 is 0 sec.
• line 2 is 30 sec.
• line 3 is 60 sec.
• line 4 is 90 sec.
• line 5 is 120 sec.
We need to look at each of the four cores independently. This means that each input file is really four (4) sets of temperature data.
Output Format:
All output must be written to text files (one file per core). Each line must take the form:
xk <= x < xk+1 ; yi = c0 + c1x ; type
where
• xk and xk+1 are the domain in which yk is applicable
• yk is the k^th function
• type is interpolation
For the example data from the input file would generate 4 output files.
• {basename}-core-0.txt,{basename}-core-1.txt, {basename}-core-2.txt, {basename}-core-3.txt
where {basename} is the input file name without the extension (e.g., without the .txt or .dat).
Expected Output:
Given the five-line input file “input_temps”:
we would end up with four output files...
input temps-core-0.txt 0<=x<= 30y= 61.0000 + 0.6333 x ; interpolation 30<=x<= 60 ; y= 98.0000 + -0.6000 x ; interpolation 60<=x<= 90 : y= 20.0000 + 0.7000 x ; interpolation 90<=x<= 120 : y = 128.0000 + -0.5000 x ; interpolation input temps-core-1.txt => X => 0 30;y= 63.0000 + 0.6000 x ; interpolation 30<=x<= 60 : y= 99.0000 + -0.6000 x ; interpolation 60<=x<= 90 : y= 25.0000 + 0.6333 x ; interpolation 90<=x<= 120 : y = 121.0000 + -0.4333 x ; interpolation input_temps-core-2.txt 0<=x<= 30:y= 50.0000 + 0.6000 x ; interpolation 30<=x<= 60 : y= 84.0000 + -0.5333 x ; interpolation 60<=x<= 90 y= 16.0000 + 0.6000 x ; interpolation 90<=x<= 120 : y = 106.0000 + -0.4000 x ; interpolation 0<=x<= 120 y = 56.0000 + 0.0600 x ; least-squares input_temps-core-3.txt 0<=x<= 30;y= 58.0000 + 0.6333 x ; interpolation 30<=x<= 60 : y= 94.0000 + -0.5667 x ; interpolation 60<=x<= 90 : y= 22.0000 + 0.6333 x ; interpolation 90<= x<= 120 : y = 121.0000 + -0.4667 x ; interpolation
Note that the extra padding zeros for the core numbers are optional