#John Bible 12/10/21
#What I learned is script environment, parsing command-line arguments, pydoc, docstrings, global and local variables
#What I had trouble with was task 6 as I could not tell what I was being asked to do or go about doing what I could make out. I did not overcome task 6
# %cd command_line
# Note:
#Above was the original code given by Microsoft.
# This will work the first time, but if you re-run these cells
# you may encounter errors so we changed it to...
# ************** DO NOT MODIFY OR ADD ANYTHING TO THIS CODE SEGMENT ***********
# ************** Click Edit> Clear All Outputs ***********
# ************** This code segment must be run before attempting any of the tasks or examples in this lesson ********************
# ************** This cell is a reset cell to remove (if they exist) the parent_dir and any children or leaves
# It prepares the directories and files necessary to run the examples and complete the tasks.
# This cell can be run first then all others cells in this notebook can be run, then this cell can be rerun
# This is the reset. It uses the import shutil (or shell utility) which you will learn later in the course
import os, shutil # shutil = shell utility has an important method called rmtree()
# linux does not have an equivalent command!
# Navigate to `library` directory (if not already in it)
current_path = os.getcwd()
print(current_path)
if ("content" in current_path):
#JGif ("library" in current_path):
nb_path = current_path.split("content")[0] # nb_path = the 1st item in the split list
else:
nb_path = current_path
print("Changing working dir to directory above parent_dir")
os.chdir(os.path.join(nb_path,"content"))
current_path = os.getcwd()
print("Current working directory:", current_path)
if (os.path.exists('parent_dir') == True):
shutil.rmtree('parent_dir')
os.mkdir('parent_dir')
os.chdir('parent_dir')
os.mkdir('child1_dir')
os.chdir('../')
current_path = os.getcwd()
print("Current working directory:", current_path)#
%mkdir command_line
%cd command_line
!pwd
# [ ] The following program contains a few functions to count the number of: vowels, consonants, and digits in a sentence.
# Modify the program so it only run the test case when the program is executed directly.
# In other words, when the program is imported as a module for another program, it shouldn't display the test cases.
# Hint: this involves main and name and an if statement
#This will Modify the program so it only run the test case when the program is executed directly
def count_vowels(sentence):
"""Count the number of vowels in sentence."""
vowels = 0
for c in sentence:
if c.lower() in "aeiouy":
vowels = vowels + 1
return vowels
def count_consonants(sentence):
"""Count the number of consonants in sentence."""
consonants = 0
for c in sentence:
if c.isalpha():
if c.lower() not in "aeiouy":
consonants = consonants + 1
return consonants
def count_digits(sentence):
"""Count the number of digits in sentence."""
digits = 0
for c in sentence:
if c.isdigit():
digits = digits + 1
return digits
test_sentence = "Plan 2 is not working!"
print("Number of vowels = {:d}".format(count_vowels(test_sentence)))
print("Number of consonants = {:d}".format(count_consonants(test_sentence)))
print("Number of digits = {:d}".format(count_digits(test_sentence)))
if __name__=="__main__":
print(count_vowels("Plan 1 is not working"))
print(count_consonants("Plan 1 is not working"))
print(count_digits("Plan 1 is not working"))
%cd command_line
%%writefile stairs.py
# [ ] The following program generates a staircase character art
# The `size` variable controls the number of steps
# The `base_shape` defines the characters used to generate the art
# Modify the program so the `size` is set as a positional command line argument, and base_shape as an optional
# command line argument with a default value of `[]`
#This will Modify the program so the `size` is set as a positional command line argument, and base_shape as an optional
def gen_stairs(steps, base_shape):
for row in range(steps):
for col in range(steps):
if(col <= row):
print(base_shape, end = "")
print()
# Generate a staircase with 6 steps using '[]` as a base shape
gen_stairs(6, '[]')
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('size', action="store", type = int, help = 'Max size of image')
parser.add_argument('-b', '--baseshape', action = 'store_true', help = 'Base shape')
parser.add_argument('shape', action="store", type = str, default="[]", help = 'shape to be used')
args = parser.parse_args()
if args.baseshape:
print(gen_stairs(args.size, args.shape))
%%bash
python3 stairs.py 6 -b '^'
%%writefile day_counter.py
# [ ] Write a program that reads a date from the command line as numbers (month then day then year),
# if the date entered is in the past, a message saying "The date has passed" should be printed
# if the date is in the future the program should display the number of days remaining from today till that date,
# there should be an optional command line flag that displays the results in total number of seconds instead of days
#This will Write a program that reads a date from the command line as numbers (month then day then year), if the date entered is in the past, a message saying "The date has passed" should be printed if the date is in the future the program should display the number of days remaining from today till that date, there should be an optional command line flag that displays the results in total number of seconds instead of days
# help message should look like:
'''
usage: day_counter.py [-h] [-s] month day year
positional arguments:
month Month as a number (1, 12)
day Day as a number (1, 31) depending on the month
year Year as a 4 digits number (2018)
optional arguments:
-h, --help show this help message and exit
-s, --total_seconds Show the time difference in total number of seconds
'''
import argparse
from datetime import datetime
parser = argparse.ArgumentParser()
parser.add_argument('month', action = 'store', type = int, help = 'Month as a number (1, 12)')
parser.add_argument('day', action = 'store', type = int, help = 'Day as a number (1, 31) depending on the month')
parser.add_argument('year', action = 'store', type = int, help = 'Year as a 4 digits number (2018)')
parser.add_argument('-s', '--total_seconds', action = 'store_true', help = 'Show the time difference in total number of seconds')
args = parser.parse_args()
date1=datetime(args.year,args.month,args.day).strftime("%d %m %Y")
now=datetime.now().strftime("%d %m %Y")
date2=datetime(args.year,args.month,args.day)
now2=datetime.now()
difference=(date2-now2)
if args.total_seconds:
if date2<now2:
print("Date has been passed")
else:
print("Date has not been passed. Seconds until date:",difference.total_seconds())
if date2<now2:
print("Date has been passed")
else:
print("Date has not been passed. Days until date:",difference)
%%bash
python3 day_counter.py 3 2 2018 -s
%%writefile adder.py
# [ ] Write a program that reads an unspecified number of integers from the command line,
# then prints out the sum of all the numbers
# the program should also have an optional argument to show the product of the numbers (in addition to the sum)
#This will Write a program that reads an unspecified number of integers from the command line, then prints out the sum of all the numbers the program should also have an optional argument to show the product of the numbers
# help message should look like:
'''
usage: adder.py [-h] [-p] [numbers [numbers ...]]
positional arguments:
numbers numbers to be added (or multiplied)
optional arguments:
-h, --help show this help message and exit
-p, --product show the product of the numbers (in addition to the displayed
sum)
'''
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('numbers', action="store", type = int, nargs=4, help = 'numbers to be added (or multiplied)')
parser.add_argument('-p', '--product', action = 'store_true', help = 'show the product of the numbers (in addition to the displayed sum)')
args = parser.parse_args()
if args.product:
print(sum(args.numbers))
y=1
for x in args.numbers:
y*=x
print(sum(args.numbers),y)
else:
print(sum(args.numbers))
%%bash
python3 adder.py 2 3 5 10 -p
# The following program displays the double of a number
# Fix the error/s in the program so the correct answer is displayed
#This will Fix the error/s in the program so the correct answer is displayed
def double(num):
answer = num * 2
return answer
double(6)
print(double(6))
# [ ] The program below converts a length from Centimeters to Inches, Feet, Yards, and Miles
# Complete the functions cent2inches, cent2feet, cent2yards, cent2miles so they all return the expected result
#This will Complete the functions cent2inches, cent2feet, cent2yards, cent2miles so they all return the expected result
def cent2inches(length):
"""
Convert length from Centimeters to Inches.
1 Centimeter = 0.393701 Inches
args:
length: in Centimeter (float)
results:
result: equivalent length in Inches (float)
"""
#TODO: Your code goes here
result=length*0.393701
return result
def cent2feet(length):
"""
Convert length from Centimeters to Feet.
1 Centimeter = 0.0328084 Feet
args:
length: in Centimeter (float)
results:
result: equivalent length in Feet (float)
"""
#TODO: Your code goes here
result=length*0.0328084
return result
def cent2yards(length):
"""
Convert length from Centimeters to Yards.
1 Centimeter = 0.0109361 Yards
args:
length: in Centimeter (float)
results:
result: equivalent length in Yards (float)
"""
#TODO: Your code goes here
result=length*0.0109361
return result
def cent2miles(length):
"""
Convert length from Centimeters to Miles.
1 Centimeter = 6.2137e-6 Miles
args:
length: in Centimeter (float)
results:
result: equivalent length in Miles (float)
"""
#TODO: Your code goes here
result=length*6.2137
return result
def main():
length = float(input("Enter length in Centimeters: "))
# In Inches
inches = cent2inches(length)
# In Feet
feet = cent2feet(length)
# In Yards
yards = cent2yards(length)
# In Miles
miles = cent2miles(length)
print('{:.2f} [cm] = {:.2f} [inches]", {:.2f} [feet], {:.2f} [yards], {:.2e} [miles]'.format(length, inches, feet, yards, miles))
if __name__ == '__main__':
main()
# [ ] The program below converts a length from Centimeters to Feet without using direct conversion
# Complete the functions cent2inches, inches2feet, and cent2feet so they return the expected result
# You should use cent2inches and inchest2feet in cent2feet
#This will Complete the functions cent2inches, inches2feet, and cent2feet so they return the expected result
def cent2inches(length):
"""
Convert length from Centimeters to Inches.
1 Centimeter = 0.393701 Inches
args:
length: in Centimeter (float)
results:
result: equivalent length in Inches (float)
"""
#TODO: Your code goes here
result=length*0.393701
return result
def inches2feet(length):
"""
Convert length from Inches to Feet.
1 Inch = 0.0833333 Feet
args:
length: in Inches (float)
results:
result: equivalent length in Feet (float)
"""
#TODO: Your code goes here
result=cent2inches(length)*0.0833333
return result
def cent2feet(length):
"""
Convert length from Centimeters to Yards.
The conversion rate is unknown, you have to use cent2inches and inches2feet
args:
length: in Centimeter (float)
results:
result: equivalent length in Yards (float)
"""
#TODO: Your code goes here
result=inches2feet(length)
return result
def main():
length = float(input("Enter length in Centimeters: "))
# In Feet
feet = cent2feet(length)
print('{:.2f} [cm] = {:.2f} [feet]'.format(length, feet))
if __name__ == '__main__':
main()
# [ ] Complete the function `formatted_phone()` so it displays the PHONE number as 777-248-3940
#This will make it displays the PHONE number as 777-248-3940
PHONE = "7895550153"
def formatted_phone(n):
#TODO: Your code goes here
return format(int(n[:-1]),",").replace(",","-")+n[-1]
pass
formatted_phone(PHONE)
# [ ] In the following program, the function change_x should change the value stored in x from 5 to 7
# modify the function so the value of x is changed from within the function
#This will modify the function so the value of x is changed from within the function
x = 5
def change_x():
global x
x = 7
return x
print("Before change x =", x) # should be 5
change_x()
print("After change x =", x) # should be 7
# [ ] The function upper_case should change the NAME to upper case
# correct the function so that NAME is changed permanently
#This will correct the function so that NAME is changed permanently
# HINT: Use the string function str.upper()
NAME = "Skye Homsi"
def upper_case():
global NAME
NAME=NAME.upper()
return NAME
print("Current name case:", NAME)
upper_case()
print("Updated name case:", NAME)
# [ ] The function `average` computes the average of a list (or tuple) T
# Document the function using a one-line docstring
#This will Document the function using a one-line docstring
def average(T):
"""Get average from list/tuple"""
s = 0
for num in T:
s = s + num
return s / len(T)
# [ ] The following functions compute the area and average of a circle with radius r
# Document both function using a one-line docstring
#This will Document both function using a one-line docstring
from math import pi
def circle_area(r):
"""Get circle area"""
return pi * r ** 2
def circle_circumference(r):
"""Get circle circumference"""
return 2 * pi * r
# [ ] As you saw before, the function gen_stairs generates a number of stairs using (base_shape)
# Document the function using a multi-line docstring
#This will document the function using a multi-line docstring
def gen_stairs(steps, base_shape):
"""
Make stairs.
args:
range: list of available numbers
steps: amount of times the image moves up/down
base_shape: shape being used
"""
for row in range(steps):
for col in range(steps):
if(col <= row):
print(base_shape, end = "")
print()
# Generate a staircase with 6 steps using '[]` as a base shape
gen_stairs(6, '[]')
# [ ] The following function returns the minimum and maximum of a tuple (or list) T
# # Document the function using a multi-line docstring
#This will returns the minimum and maximum
def min_max(T):
"""
Return minimum and maximum value of T.
args:
T: list/tuple of elements
returns:
min: smallest number in T
max: largest number in T
"""
return (min(T), max(T))
T=(19,5)
min_max(T)
%%writefile weather.py
# [ ] In this project, you will read a CSV file containing about 25K lines of weather information
# and store the information in a Python dictionary. You will then use the dictionary to find out
# the hottest, coldest, rainiest years and so on...
# You will see that the dictionary's search optimization algorithms will allow you to explore
# this large dataset without any noticeable delays.
# The logic of the program is in the `main` function, read it before writing any code.
# Use the description and examples under each of the following functions to implement them:
# 1) convert_file(file_path)
# 2) add_rainy(weather)
# 3) consolidate(weather)
# 4) year_info(year, yearly_weather)
# 5) hottest(yearly_weather)
# 6) coldest(yearly_weather)
# 7) rainiest_days(yearly_weather)
# 8) highest_prcp(yearly_weather)
#This will store weather data in a dictionary that will be used to determine the extremes
from datetime import date, datetime
def convert_file(file_path):
"""
Convert CSV file to a Python dictionary.
The CSV file contains daily weather information arranged in rows,
the rows contain (date, precipitation (inches), maximum temperature (F), minimum temperature (F)) in order.
First line in CSV file is a header (DATE, PRCP, TMAX, TMIN), the rest contain the weather data.
The function should read the data in the file and generate a dictionary where:
1) keys are date objects (from the datetime module), using the daily date info in the file
2) values are lists containing [TMAX, TMIN, PRCP] in order
args:
file_path: path to the CSV file
returns:
weather: the generated dictionary using date objects as keys and lists of weather info as values
examples:
Input CSV file:
DATE,PRCP,TMAX,TMIN
12/10/2017,0,49,34
12/11/2017,0,49,29
12/12/2017,0,46,32
12/13/2017,0,48,34
12/14/2017,0,50,36
12/15/2017,0.06,43,37
12/16/2017,0.14,45,37
12/17/2017,0.03,50,42
12/18/2017,0.7,49,41
12/19/2017,1,50,40
12/20/2017,0.13,49,32
12/21/2017,0.01,41,29
12/22/2017,0.09,40,35
12/23/2017,0,38,29
12/24/2017,0.12,38,28
Output dictionary (weather):
{datetime.date(2017, 12, 10): [49, 34, 0.0],
datetime.date(2017, 12, 11): [49, 29, 0.0],
datetime.date(2017, 12, 12): [46, 32, 0.0],
datetime.date(2017, 12, 13): [48, 34, 0.0],
datetime.date(2017, 12, 14): [50, 36, 0.0],
datetime.date(2017, 12, 15): [43, 37, 0.06],
datetime.date(2017, 12, 16): [45, 37, 0.14],
datetime.date(2017, 12, 17): [50, 42, 0.03],
datetime.date(2017, 12, 18): [49, 41, 0.7],
datetime.date(2017, 12, 19): [50, 40, 1.0],
datetime.date(2017, 12, 20): [49, 32, 0.13],
datetime.date(2017, 12, 21): [41, 29, 0.01],
datetime.date(2017, 12, 22): [40, 35, 0.09],
datetime.date(2017, 12, 23): [38, 29, 0.0],
datetime.date(2017, 12, 24): [38, 28, 0.12]}
"""
#TODO: Your code goes here, see hints below
# open the file for reading (HINT: use `with` statement)
with open(file_path,"r") as st:
sta = convert_file(st)
# ignore the first header line (DATE, PRCP, TMAX, TMIN)
# remove newline characters from end of each line (HINT: use str.rstrip())
sta.rstrip("\n")
# split line into a list (HINT: use str.split(','))
sta.split(",")
# create the date object from the first list element (HINT: use the date_creater(date_string) function provided below)
# read the weather related variables
# precipitation
# maximum temperature
# minimum temperature
# create dictionary key:value pair
pass
def add_rainy(weather):
"""
Emphasize rainy days using Boolean values.
Modify the weather dictionary by adding another Boolean element to the values' lists.
If precipitation is observed on a day (more than 0"), the associated list is appended by True.
If precipitation is not observed (0"), the associated list is appended by False.
args:
weather: dictionary, keys are date objects and values are lists [TMAX, TMIN, PRCP]
returns:
None: the weather dictionary is modified directly, keys are date objects and values are lists [TMAX, TMIN, PRCP, RAINY_DAY]
examples:
Input weather dictionary:
{datetime.date(2017, 12, 10): [49, 34, 0.0],
datetime.date(2017, 12, 11): [49, 29, 0.0],
datetime.date(2017, 12, 12): [46, 32, 0.0],
datetime.date(2017, 12, 13): [48, 34, 0.0],
datetime.date(2017, 12, 14): [50, 36, 0.0],
datetime.date(2017, 12, 15): [43, 37, 0.06],
datetime.date(2017, 12, 16): [45, 37, 0.14],
datetime.date(2017, 12, 17): [50, 42, 0.03],
datetime.date(2017, 12, 18): [49, 41, 0.7],
datetime.date(2017, 12, 19): [50, 40, 1.0],
datetime.date(2017, 12, 20): [49, 32, 0.13],
datetime.date(2017, 12, 21): [41, 29, 0.01],
datetime.date(2017, 12, 22): [40, 35, 0.09],
datetime.date(2017, 12, 23): [38, 29, 0.0],
datetime.date(2017, 12, 24): [38, 28, 0.12]}
Updated weather dictionary with Boolean values
{datetime.date(2017, 12, 10): [49, 34, 0.0, False],
datetime.date(2017, 12, 11): [49, 29, 0.0, False],
datetime.date(2017, 12, 12): [46, 32, 0.0, False],
datetime.date(2017, 12, 13): [48, 34, 0.0, False],
datetime.date(2017, 12, 14): [50, 36, 0.0, False],
datetime.date(2017, 12, 15): [43, 37, 0.06, True],
datetime.date(2017, 12, 16): [45, 37, 0.14, True],
datetime.date(2017, 12, 17): [50, 42, 0.03, True],
datetime.date(2017, 12, 18): [49, 41, 0.7, True],
datetime.date(2017, 12, 19): [50, 40, 1.0, True],
datetime.date(2017, 12, 20): [49, 32, 0.13, True],
datetime.date(2017, 12, 21): [41, 29, 0.01, True],
datetime.date(2017, 12, 22): [40, 35, 0.09, True],
datetime.date(2017, 12, 23): [38, 29, 0.0, False],
datetime.date(2017, 12, 24): [38, 28, 0.12, True]}
"""
#TODO: Your code goes here
pass
def consolidate(weather):
"""
Consolidate the daily weather information by year.
Use the weather dictionary to generate a new consolidated dictionary (yearly_weather).
The new dictionary uses years as keys, and the associated values are lists containing (in order):
1) The average of the highest recorded temperatures in the year (AVG_TMAX)
2) The average of the lowest recorded temperatures in the year (AVG_TMIN)
3) The total recorded precipitation in the year (TOTAL_PRCP)
4) The total number of rainy days in the year (TOTAL_RAINY_DAYS)
5) The number of recorded days (TOTAL_DAYS).
This element is necessary to account for days where the station breaks (missing recordings),
or if the year hasn't finished yet.
args:
weather: dictionary, keys are date objects and values are lists [TMAX, TMIN, PRCP, RAINY_DAY]
returns:
yearly_weather: consolidated dictionary, keys are years (int), values are lists
[AVG_TMAX, AVG_TMIN, TOTAL_PRCP, TOTAL_RAINY_DAYS, TOTAL_DAYS]
examples:
Input weather dictionary:
{datetime.date(2017, 12, 10): [49, 34, 0.0, False],
datetime.date(2017, 12, 11): [49, 29, 0.0, False],
datetime.date(2017, 12, 12): [46, 32, 0.0, False],
datetime.date(2017, 12, 13): [48, 34, 0.0, False],
datetime.date(2017, 12, 14): [50, 36, 0.0, False],
datetime.date(2017, 12, 15): [43, 37, 0.06, True],
datetime.date(2017, 12, 16): [45, 37, 0.14, True],
datetime.date(2017, 12, 17): [50, 42, 0.03, True],
datetime.date(2017, 12, 18): [49, 41, 0.7, True],
datetime.date(2017, 12, 19): [50, 40, 1.0, True],
datetime.date(2017, 12, 20): [49, 32, 0.13, True],
datetime.date(2017, 12, 21): [41, 29, 0.01, True],
datetime.date(2017, 12, 22): [40, 35, 0.09, True],
datetime.date(2017, 12, 23): [38, 29, 0.0, False],
datetime.date(2017, 12, 24): [38, 28, 0.12, True]}
Output yearly_weather dictionary:
{2017: [45.666666666666664, 34.333333333333336, 2.28, 9, 15]}
"""
#TODO: Your code goes here
pass
def year_info(year, yearly_weather):
"""
Convert the year's weather information to a formatted string.
Look for the weather information of `year` in the `yearly_weather` dictionary.
If it exists, convert the information list into a formatted string:
YEAR | AVG_TMAX | AVG_TMIN | TOTAL_PRCP | TOTAL_RAINY_DAYS | TOTAL_DAYS
If it does not exist, the formatted string should be:
N/A | N/A | N/A | N/A | N/A | N/A
args:
year: int value to look for in the yearly_weather dictionary
yearly_weather: consolidated dictionary, keys are years (int), values are lists
[AVG_TMAX, AVG_TMIN, TOTAL_PRCP, TOTAL_RAINY_DAYS, TOTAL_DAYS]
returns:
formatted_string: containing the year's weather information
examples:
Input yearly weather dictionary:
{2017: [45.666666666666664, 34.333333333333336, 2.28, 9, 15]}
Output formatted string:
== year_info(2017, yearly_weather) == (contained in the dictionary)
' 2017 | 45.67 | 34.33 | 2.28" | 9 | 15 '
== year_info(2055, yearly_weather) == (not contained in the dictionary)
' N/A | N/A | N/A | N/A | N/A | N/A '
"""
#TODO: Your code goes here
pass
def hottest(yearly_weather):
"""
Find the hottest year in yearly_weather.
Look through all the years in the yearly_weather dictionary and return the year with
the highest average maximum temperature (highest AVG_TMAX)
args:
yearly_weather: consolidated dictionary, keys are years (int), values are lists
[AVG_TMAX, AVG_TMIN, TOTAL_PRCP, TOTAL_RAINY_DAYS, TOTAL_DAYS]
returns:
hottest_year: the year with the highest maximum temperature average (AVG_TMAX)
"""
#TODO: Your code goes here
pass
def coldest(yearly_weather):
"""
Find the coldest year in yearly_weather.
Look through all the years in the yearly_weather dictionary and return the year with
the lowest average minimum temperature (lowest AVG_TMIN)
args:
yearly_weather: consolidated dictionary, keys are years (int), values are lists
[AVG_TMAX, AVG_TMIN, TOTAL_PRCP, TOTAL_RAINY_DAYS, TOTAL_DAYS]
returns:
coldest_year: the year with the lowest minimum temperature average (AVG_TMIN)
"""
#TODO: Your code goes here
pass
def rainiest_days(yearly_weather):
"""
Find the year with the largest number of rainy days in yearly_weather.
Look through all the years in the yearly_weather dictionary and return the year with
the largest TOTAL_RAINY_DAYS
args:
yearly_weather: consolidated dictionary, keys are years (int), values are lists
[AVG_TMAX, AVG_TMIN, TOTAL_PRCP, TOTAL_RAINY_DAYS, TOTAL_DAYS]
returns:
rainiest_year: the year with the largest number of rainy days
"""
#TODO: Your code goes here
pass
def highest_prcp(yearly_weather):
"""
Find the year with the highest total precipitation in yearly_weather.
Look through all the years in the yearly_weather dictionary and return the year with
the largest TOTAL_PRCP
args:
yearly_weather: consolidated dictionary, keys are years (int), values are lists
[AVG_TMAX, AVG_TMIN, TOTAL_PRCP, TOTAL_RAINY_DAYS, TOTAL_DAYS]
returns:
rainiest_year: the year with the highest total precipitation
"""
#TODO: Your code goes here
pass
def date_creater(date_string):
"""Convert the date_string (formatted as m/d/yyyy) to a date object."""
d = datetime.strptime(date_string, "%m/%d/%Y")
return d.date()
def dashes(count):
"""Print a fancy line of `count` dashes"""
print("o" + count *'-' + "o")
def page_header(title):
"""Print a page header with a title surrounded by dashes"""
dashes(78)
print("|{:^78s}|".format(title))
dashes(78)
def table_header():
"""Print the first row in a table (header row)"""
print()
print(' {0:^4s} | {1:^13s} | {2:^12s} | {3:^7s} | {4:^12s} | {5:^14s} '.format("Year", "Avg High Temp", "Avg Low Temp", "Percip", "# Rainy days", "# Recorded days"))
dashes(78)
def display(title, years, yearly_weather):
"""Print a page with a header, title, and the weather information of all years as found in yearly_weather"""
clear_output()
page_header(title)
table_header()
# if years contain a single int, convert to a single item list
if type(years) is not list: years = [years]
# print weather information for all years
for year in years:
print(year_info(year, yearly_weather))
# display this page till you go back to the main menu
while True:
m = input("Return to main menu [y/n]?")
if m in 'yesYesYES':
break
def main():
# convert the data in the (csv) file into a Python dictionary
weather = convert_file("data/seattle_weather.csv")
# highlight rainy days by adding a Boolean entry to the dictionary's values
add_rainy(weather)
# consolidate the weather data by year then store the result in a new dictionary
yearly_weather = consolidate(weather)
# earliest and latest years on record
min_year = min(yearly_weather)
max_year = max(yearly_weather)
# menus functionality
while True:
clear_output()
# display header
page_header("Weather Records")
# display main menu
print()
print("Main Menu (choose an option to display):\n")
print("1. Summary of a certain year")
print("2. All years summary table")
print("3. Hottest year on record")
print("4. Coldest year on record")
print("5. Year with most rainy days")
print("6. Year with the highest precipitation")
print("7. Quit")
print()
# display footer with user input message
dashes(78)
while True:
try:
option = input("Select an option (1, 7): ")
option = int(option)
if 1 <= option <= 7:
break # break the user input loop only, main loop does NOT break
except ValueError:
print("Cannot convert {:} to int".format(type(option)))
# execute relevant function
# 1. Summary of a certain year
if option == 1:
# ask the user for a valid year
while True:
try:
year = input("Enter a year ({} - {})".format(min_year, max_year))
year = int(year)
if min_year <= year <= max_year:
break
except ValueError:
print("Cannot convert {:} to int".format(type(option)))
display("Year Summary", year, yearly_weather)
# 2. All years summary table
elif option == 2:
years = list(sorted(yearly_weather))
display("Tabular Summary", years, yearly_weather)
# 3. Hottest year on record
elif option == 3:
year = hottest(yearly_weather)
display("Hottest year on record", year, yearly_weather)
# 4. Coldest year on record
elif option == 4:
year = coldest(yearly_weather)
display("Coldest year on record", year, yearly_weather)
# 5. Year with most rainy days
elif option == 5:
year = rainiest_days(yearly_weather)
display("Year with most rainy days", year, yearly_weather)
# 6. Year with the highest precipitation
elif option == 6:
year = highest_prcp(yearly_weather)
display("Year with the highest precipitation", year, yearly_weather)
# 7. Quit
elif option == 7:
clear_output()
break
%%bash
pydoc weather