Submit 3.11 lab

This commit is contained in:
Joe S 2021-01-23 16:56:48 -05:00
parent 247b241041
commit f5ef14dee8
1 changed files with 29 additions and 20 deletions

View File

@ -1,44 +1,53 @@
from datetime import datetime from datetime import datetime
import logging import logging
logging.basicConfig(level=logging.ERROR) logging.basicConfig(level=logging.DEBUG)
season_dict = { input_month = input('Input a month to analyse: ')
} input_day = int(input('Input a day of that month: '))
def convert_doy_to_season(doy):
if not isinstance(doy, int):
return 'Invalid'
#input_month = input('Input a month to analyse: ') if 79 <= doy <= 171:
#input_day = int(input('Input a day of that month: ')) return 'Spring'
if 172 <= doy <= 264:
return 'Summer'
if 265 <= doy <= 354:
return 'Autumn'
if 355 <= doy <= 365 or 1 <= doy <= 78:
return 'Winter'
def convert_month_to_num(month_name): def convert_month_to_num(month_name):
try: try:
_date = datetime.strptime(month_name, "%B") _date = datetime.strptime(month_name, "%B")
logging.debug(_date.month)
return int(_date.month) return int(_date.month)
except ValueError: except ValueError:
logging.warning('Was unable to convert from full month name, trying with shortname.') logging.warning('Was unable to convert from full month name, trying with shortname.')
try: try:
_date = datetime.strptime(month_name, "%b") _date = datetime.strptime(month_name, "%b")
logging.debug(_date.month)
return int(_date.month) return int(_date.month)
except ValueError: except ValueError:
logging.error('Was unable to convert the month {0}! Tried long name and short name.'.format(month_name)) logging.error('Was unable to convert the month {0}! Tried long name and short name.'.format(month_name))
return None return None
def day_of_year(month,day): def day_of_year(month,day):
# Cannot handle leap years!!! try:
result = int((275 * month) / 9.0) - 2 * int((month + 9) / 12.0) + day - 30 # Cannot handle leap years!!!
return result if day > 30 or day <= 0:
raise OverflowError
result = int((275 * month) / 9.0) - 2 * int((month + 9) / 12.0) + day - 30
logging.debug(result)
return result
except TypeError:
return None
except OverflowError:
return None
#print(convert_month_to_num(input_month)) print(convert_doy_to_season(day_of_year(convert_month_to_num(input_month), input_day)))
#print(day_of_year(convert_month_to_num(input_month), input_day))
print(day_of_year(convert_month_to_num('March'), 20))
print(day_of_year(convert_month_to_num('June'), 20))
print(day_of_year(convert_month_to_num('June'), 21))
print(day_of_year(convert_month_to_num('Sep'), 21))
print(day_of_year(convert_month_to_num('September'), 22))
print(day_of_year(convert_month_to_num('December'), 20))
print(day_of_year(convert_month_to_num('December'), 21))
print(day_of_year(convert_month_to_num('March'), 19))