def create_csv_file(h: Holiday, year: int) -> None: csv_file = Path(csv_folder, "{}-{}.csv".format(h.get_country_code(), year)) title_row = [] names = h.get_names() for name in names: title_row.append("name_{}".format(name)) title_row.append("date") title_row.append("public_holiday") title_row.append("common_holiday") with open(csv_file, "w") as f: writer = csv.writer(f) writer.writerow(title_row) for holiday in h.get_holidays(year): result_row = [] for name in names: if name in holiday["names"]: result_row.append(holiday["names"][name]) else: result_row.append("") result_row.append(holiday["date"]) result_row.append("yes" if "public" in holiday["tags"] else "no") result_row.append("yes" if "common" in holiday["tags"] else "no") writer.writerow(result_row) print("Created CSV file {}".format(csv_file))
def main(): parser = argparse.ArgumentParser() parser.add_argument("--json", action="store_true", help="Create JSON files") parser.add_argument("--csv", action="store_true", help="Create CSV files") parser.add_argument( "-c", "--country", help="Specify country (eg. FI, SE). Default: all countries are created", ) parser.add_argument( "-y", "--year", required=True, help="The year you want the date files for (eg. 2020, 2025)", ) args = parser.parse_args() year = int(args.year) if not year: print("No valid year specified.") exit(1) country_files = Holiday.get_available_country_files() if not args.json and not args.csv: print( "No creatable country files specified (see --help), these are available:" ) print_countries(country_files) exit(0) if args.country: country_filename = "{}.yml".format(args.country) print("Creating specified file(s) for country {}.".format( args.country)) if country_filename not in country_files: print( "Specified country {} does not exist in available country files. These are available:" ) print_countries(country_files) exit(1) h = Holiday(country_filename) if args.json: create_json_file(h, year) if args.csv: create_csv_file(h, year) else: print("Creating specified file(s) for all available countries.") for country_filename in country_files: h = Holiday(country_files[country_filename]) if args.json: create_json_file(h, year) if args.csv: create_csv_file(h, year)
def create_json_file(h: Holiday, year: int) -> None: json_file = Path(json_folder, "{}-{}.json".format(h.get_country_code(), year)) data = h.get_base_info() data["names"] = h.get_names() data["holidays"] = h.get_holidays(year) with open(json_file, "w") as f: json.dump(data, f, ensure_ascii=False, indent=4) print("Created JSON file {}".format(json_file))
def find_missing_data(self): if not os.path.exists('Data_Checks'): os.makedirs('Data_Checks') location = 'Data_Checks' missing_dates = [] holiday_list = Holiday(self.country) # Determine where to start dates = self.unchecked_data['price_date'] if self.start_date > self.unchecked_data['price_date'][0]: dates = dates.loc[ self.unchecked_data['price_date'] >= self.start_date] dates.index = range(0, len(dates)) date0 = dt.datetime.strptime(dates[0], '%Y-%m-%d').date() for index, date in enumerate(dates): if index == 0: continue date1 = dt.datetime.strptime(date, '%Y-%m-%d').date() # Find which day it should be, taking weekend into account day_inc = 1 if date0.weekday() == 4: day_inc = 3 date_shouldbe = date0 + dt.timedelta(days=day_inc) # date1 should be day after date0 if date1 != date_shouldbe: # Check if it's a holiday hlist = holiday_list.holidays(date_shouldbe.year) if date_shouldbe not in hlist: missing_dates.append(date_shouldbe) # Define new date0 date0 = date1 if len(missing_dates) != 0: with open( ''.join([ location, '/', self.ticker, '_', self.symbol_id, '_missing_dates.txt' ]), 'w') as file_miss: for item in missing_dates: print >> file_miss, item.isoformat() return missing_dates
def get_country_description(country: str) -> Dict[str, Any]: if not _check_country_existance(country): abort(404) info = Holiday.for_country(country.upper()) country_description = { "names": info.names, "country-code-alpha2": info.alpha2_country_code, "country-code-alpha3": info.alpha3_country_code, } return country_description
def get_holidays_in_year(country: str, year: int) -> Tuple[Dict[str, Any], int]: if not _check_country_existance(country): abort(404) holidays = Holiday.for_country(country.upper()).get_holidays(year) result = { "holidays": [], } for holiday in holidays: result["holidays"].append(_get_serializable_holiday(holiday)) return result, 200
def check_date_for_holidays(country: str, year: int, month: int, day: int) -> Tuple[Dict[str, Any], int]: if not _check_country_existance(country): abort(404) holidays = Holiday.for_country(country.upper()).get_holidays(year) result = { "holidays": [], } wanted_date = date(year, month, day) for holiday in holidays: if holiday.date == wanted_date: result["holidays"].append(_get_serializable_holiday(holiday)) return result, 200
def render_csv(f: TextIO, h: Holiday, year: int) -> None: title_row = [] languages = sorted(set(h.names)) for language in languages: title_row.append("name_{}".format(language)) title_row.append("date") title_row.append("public_holiday") title_row.append("common_holiday") writer = csv.writer(f) writer.writerow(title_row) for holiday in h.get_holidays(year): result_row = [] for language in languages: result_row.append(holiday.names.get(language, "")) result_row.append(holiday.date) result_row.append("yes" if "public" in holiday.tags else "no") result_row.append("yes" if "common" in holiday.tags else "no") writer.writerow(result_row)
def render_json(f: TextIO, h: Holiday, year: int) -> None: data = { "countrycode-alpha2": h.alpha2_country_code, "countrycode-alpha3": h.alpha3_country_code, "countrycode-numeric": h.numeric_country_code, "names": h.names, "holidays": [{ "names": bd.names, "tags": sorted(bd.tags), "date": bd.date } for bd in h.get_holidays(year)], } json.dump(data, f, ensure_ascii=False, indent=4, default=format_json_object)
def fi_holiday(): return Holiday.for_country("FI")