Created
August 1, 2016 18:50
-
-
Save abhijitmamarde/272025967e1668ae224ab47c14f6e29d to your computer and use it in GitHub Desktop.
Get days remaining or passed for particular date. Can add dates/events in json file
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 python | |
# -*- encoding: utf-8 -*- | |
import time | |
import string | |
from datetime import datetime | |
from os.path import expanduser, join | |
import json | |
json_data_file_name = "days_for.json" | |
json_data_file_path = expanduser("~") | |
def is_valid_event_name(event): | |
if event[0] in string.digits: | |
print "Error: invalid event name '%s'" | |
return False | |
return True | |
def is_date_valid(eday): | |
try: | |
day, month, year = [int(x) for x in eday.split('/')] | |
if year < 1900: | |
raise InvalidInputException | |
ed = datetime(year=year, month=month, day=day) | |
return True | |
except: | |
print "Error: Incorret input: '%s'. Enter valid date in dd/mm/yyyy format." % (eday) | |
return False | |
def get_days_for(eday, from_day=datetime.now()): | |
if not is_date_valid: | |
return | |
day, month, year = [int(x) for x in eday.split('/')] | |
ed = datetime(year=year, month=month, day=day) | |
if ed > from_day: | |
diff_date = ed - from_day | |
if diff_date.days > 0: | |
print "%d day(s) remaining" % (diff_date.days) | |
else: | |
print "<1 day remaining" | |
else: | |
diff_date = from_day - ed | |
print "%d day(s) ago" % (diff_date.days) | |
def read_data(): | |
return json.load(open( join(json_data_file_path, json_data_file_name) )) | |
def show_data(): | |
l = read_data() | |
for e in l.keys(): | |
show_data_for(e, l) | |
def add_days_for(eday): | |
if not is_date_valid(eday): | |
return | |
event = raw_input("Event: ") | |
if not is_valid_event_name(event): | |
return | |
description = raw_input("Description: ") | |
data = { | |
"date": eday, | |
"description": description, | |
} | |
l = read_data() | |
l[event] = data | |
ofile = open(join(json_data_file_path, json_data_file_name), "w") | |
print json.dump(l, ofile) | |
def test_all(): | |
idays = [ | |
"26/07/2016", | |
"26/12/2015", | |
"26/13/2015", | |
"31/12/2016", | |
] | |
for d in idays: | |
get_days_for(d) | |
def show_data_for(event, data=None): | |
if not data: | |
data = read_data() | |
if event not in data.keys(): | |
print "Error: event '%s' not found!" % (event) | |
return | |
print "%s '%s': %s" % ( | |
data[event]['date'], | |
event, | |
data[event]['description'], | |
) | |
get_days_for(data[event]['date']) | |
if __name__ == "__main__": | |
import sys | |
if len(sys.argv) > 1: | |
if (sys.argv[1][0] in string.digits) and is_date_valid(sys.argv[1]): | |
add_days_for(sys.argv[1]) | |
show_data() | |
else: | |
show_data_for(sys.argv[1]) | |
else: | |
show_data() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment