Last active
January 16, 2021 07:59
-
-
Save manudatta/3e5242918ff07b5d711789339ee2ce2a to your computer and use it in GitHub Desktop.
parsing and printing guarantees
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
#!/usr/bin/env python3 | |
import csv | |
import sys | |
from collections import namedtuple | |
from datetime import datetime | |
Guarantee = namedtuple("Guarantee", "end_date address") | |
DEFAULT_CALCULATION_DATE = datetime(2020, 5, 2) | |
DEFULAT_FILE_NAME = "./guarantees_end.csv" | |
def print_expired_guarantees(guarantees_days_left, pre="\t"): | |
for guarantee, days_left in guarantees_days_left: | |
if days_left < 0: | |
print(f"{pre}{guarantee.address}") | |
def print_expiry_details(guarantees_days_left, pre="\t"): | |
for guarantee, days_left in guarantees_days_left: | |
report_entry = None | |
if days_left < 0: | |
report_entry = f"has expired {-days_left} ago" | |
elif days_left == 0: | |
report_entry = f"will expire today" | |
else: | |
report_entry = f"will expire in {days_left} days" | |
print(f"{pre}{guarantee.address} {report_entry}") | |
def days_left(gurantee, from_date): | |
return (gurantee.end_date - from_date).days | |
def date_from_str(date_str): | |
date_parts = map(int, date_str.split("-")) | |
return datetime(*date_parts) | |
def parse_file_to_gurantees(csvfile): | |
guarantees = [] | |
with open(csvfile) as csvfile: | |
guarantees = [ | |
Guarantee(date_from_str(end_date), address) | |
for [end_date, address] in csv.reader(csvfile) | |
] | |
return guarantees | |
def main(file_name, calculation_date): | |
guarantees = parse_file_to_gurantees(file_name) | |
guarantees_days_left = [(g, days_left(g, calculation_date)) for g in guarantees] | |
print(f"\n1. Guarantee has ended for following addresses on: {calculation_date}") | |
print_expired_guarantees(guarantees_days_left) | |
print(f"\n2. Expiration report for {file_name} for date {calculation_date}") | |
print_expiry_details(guarantees_days_left) | |
""" | |
main.py (author: [email protected]) | |
usage: ./main.py <csv file> <date of calculation> | |
output: | |
1. report of already expired guarantees | |
2. report of each guarantee with days remaining | |
notes: | |
1. No error handling on input (csv or date format) | |
2. config parser can be used to make command line | |
parameter handling more robust and standard | |
""" | |
if __name__ == "__main__": | |
file_name, calculation_date = None, None | |
try: | |
file_name = sys.argv[1] | |
calculation_date = date_from_str(sys.argv[2]) | |
except IndexError: | |
file_name = file_name if file_name is not None else DEFULAT_FILE_NAME | |
calculation_date = ( | |
calculation_date | |
if calculation_date is not None | |
else DEFAULT_CALCULATION_DATE | |
) | |
main(file_name, calculation_date) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment