
For a challenge I’ve tried to teach myself the Python programming language. It’s a big language with many features which I, as an ex-programmer from many decades ago, am unfamiliar with. But I’ve managed to write and test the code below, though I’m not sure I want to take this much further. I’ll see…..
import csv
from datetime import datetime
from operator import attrgetter
#======================================================
# A re-creation of my Reminder program from several decades ago!! My first Python program!
#
# Version 0.1 04-Mar-2021 In the beginning
# Version 0.2 07-Mar-2021 In the beginning
# Version 0.3 07-Mar-2021 I'm finally happy!
#======================================================
class Reminder:
def __init__(self, myRec):
self.Date = myRec[0]
self.DateTimeConversion = datetime.strptime(myRec[0],"%d-%b-%Y")
self.Message = myRec[1]
#======================================================
# Function GetReminderData to get the reminder data
#
#
# Read each record in the file
# Ignore any blank lines
# Add each record to the list myReminders
#
def GetReminderData(myFile,myReminders) :
with open(myFile) as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',')
line_count = 0
for rec in csv_reader:
if rec == []:
pass
else:
line_count += 1
p = Reminder(rec)
myReminders.append(p)
csv_file.close()
#====================================================
# Function HowManyDaysDifference to get the number of days between today's date and a text date
# This is my weird code - what a palaver!
def HowManyDaysDifference(TextDate):
today_object = datetime.now()
mydate_in_datetime = datetime.strptime(TextDate,"%d-%b-%Y")
tdiff = mydate_in_datetime - today_object
diff_in_days = tdiff.days
if diff_in_days >= 0:
diff_in_days += 1
else:
tdiff = today_object - mydate_in_datetime
diff_in_days = -tdiff.days
return diff_in_days
#==================================================
# Function PrintList to nicely print the data
def PrintList ():
mySectionSeparator = "-" * 75
tChange = True
print ("\n" * 10 )
print (" Welcome to my Python Reminder program!")
print (mySectionSeparator)
for i in sorted(myReminders, key = attrgetter('DateTimeConversion')):
nDays = HowManyDaysDifference(i.Date)
if tChange and nDays > 0:
tChange = False
print (mySectionSeparator)
if nDays == 0:
t1 = " Today "
elif nDays == -1:
t1 = " Yesterday "
elif nDays == 1:
t1 = " Tomorrow "
elif nDays < 0:
t1 = '{:4d}'.format(-nDays) + " days since"
else:
t1 = '{:4d}'.format(nDays) + " days until"
t2 = i.Date +t1
print (t2,i.Message)
print (mySectionSeparator)
#=================================================
# This is the MAIN program
#
myFile = 'C:/Users/Mike/Documents/Documents/MyPythonCode/Reminder.dat'
myReminders = []
GetReminderData(myFile, myReminders)
PrintList()
And the output is….
