weather_v2.py 1.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#!/usr/bin/env python3
""" Find the day with the highest average temperature.
    Write a program that takes a filename on the command line and processes the
    CSV contents. The contents will be a CSV file with a month of weather data,
    one day per line.

    Determine which day had the highest average temperature where the average
    temperature is the average of the day's high and low temperatures. This is
    not normally how average temperature is computed, but it will work for our
    demonstration.

    The first line of the CSV file will be column headers:

        Day,MxT,MnT,AvT,AvDP,1HrP TPcn,PDir,AvSp,Dir,MxS,SkyC,MxR,Mn,R AvSLP

    The day number, max temperature, and min temperature are the first three
    columns.

    Write unit tests with Pytest to test your program.
"""
21
import csv
J
Jim Anderson 已提交
22

23

24 25 26 27
def get_day_and_avg(day_stats):
    day_number = int(day_stats["Day"])
    avg = (int(day_stats["MxT"]) + int(day_stats["MnT"])) / 2
    return day_number, avg
28 29


30 31 32
def get_next_day_and_avg(csv_file, func):
    for day_stats in csv.DictReader(csv_file):
        yield func(day_stats)
33

34 35 36 37 38 39 40

def get_max_avg(filename):
    with open(filename, "r", newline="") as csv_file:
        return max(
            get_next_day_and_avg(csv_file, get_day_and_avg),
            key=lambda item: item[1],
        )