Created
May 4, 2016 13:37
-
-
Save vftools/521f043e2165237ca590dbcfc193d07f to your computer and use it in GitHub Desktop.
Find overlapping dates for Ascension Day and Liberation Day
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Import libraries | |
import operator | |
from workalendar.europe import Netherlands | |
# Settings | |
start_year = 1945 | |
end_year = 2016 | |
date_month = 5 | |
date_day = 5 | |
date_name = 'Liberation Day' | |
holiday_name = 'Ascension Thursday' | |
max_print_rows = 10 | |
def get_suffix(year_list): | |
if len(year_list) == 0: | |
suffix = '.' | |
else: | |
suffix = ': in {0}'.format(day_dict[date_string][1][0]) | |
count_year = 1 | |
while count_year < len(year_list) - 1: | |
suffix += ', ' + str(year_list[count_year]) | |
count_year += 1 | |
if len(year_list) > 1: | |
suffix += ' and ' + str(year_list[-1]) | |
suffix += '.' | |
return suffix | |
# Make calendar instance | |
cal = Netherlands() | |
# Initialise empty date dictionary for the dates of the holiday | |
day_dict = {} | |
# Set the range and date string | |
year_range = 1 + end_year - start_year | |
if date_month < 10: | |
date_string = '0' + str(date_month) + '-' | |
else: | |
date_string = str(date_month) + '-' | |
if date_day < 10: | |
date_string += '0' + str(date_day) | |
else: | |
date_string += str(date_day) | |
# Check whether the year range is valid | |
if year_range < 1: | |
raise Exception('end_year may not be smaller than start_year') | |
for year in range(start_year, end_year+1): | |
holidays = cal.holidays(year) | |
# Get the holiday for this year | |
holiday = next((holiday for holiday in holidays | |
if holiday[1].lower().strip()[:len(holiday_name)] == | |
holiday_name.lower()), None) | |
if holiday: | |
# Store the day into the date dictionary | |
holiday = holiday[0] | |
holiday_date = '0' + str(holiday.month) | |
if holiday.day > 9: | |
holiday_date += '-' + str(holiday.day) | |
else: | |
holiday_date += '-0' + str(holiday.day) | |
if holiday_date in day_dict: | |
day_dict[holiday_date][0] += 1 | |
day_dict[holiday_date][1].append(year) | |
else: | |
day_dict[holiday_date] = [1, [year]] | |
else: | |
raise Exception("No {0} in {1}".format(holiday_name, year)) | |
# Order the date dictionary on descending occurrences | |
ordered_day_dict = sorted(day_dict.items(), key=operator.itemgetter(1), | |
reverse=True) | |
# Print statistics | |
if date_string in day_dict: | |
overlap = day_dict[date_string][0] | |
suffix_year = get_suffix(day_dict[date_string][1]) | |
else: | |
overlap = 0 | |
suffix_year = '.' | |
if overlap == 1: | |
timing = 'time' | |
else: | |
timing = 'times' | |
print("Between (and including) {0} and {1}, {2} and {3} occurred {4} {5} on " | |
"the same date{6}\n".format(start_year, end_year, date_name, | |
holiday_name, overlap, timing, suffix_year)) | |
print("Most frequent dates of {0}:".format(holiday_name)) | |
count = 0 | |
for date_value in ordered_day_dict: | |
count += 1 | |
occurrence = date_value[0] | |
if occurrence == 1: | |
timing = 'time' | |
else: | |
timing = 'times' | |
print("{0}: {1} {2} ({3}%)".format(occurrence, date_value[1][0], timing, | |
round(100*date_value[1][0]/year_range, | |
2))) | |
if count >= max_print_rows: | |
break |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
workalendar~=0.4.3 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment