Herdcat, second version

Jul 26, 2007 08:43

I'm dog-sitting my landlady's puppy, a half-spaniel/half-coton named Lola. Lola is so young she couldn't even walk a few days ago. In the Python programming language, the object "lola" is in the class Dog and has methods such as "lola.eat" and "lola.white". Bruce Webber explained this to me last night when we continued my instruction in Python. We completed all the steps I had planned out for what to do in my program Herdcat. Then he taught me the basics of Functions and Objects.


#!/usr/bin/python
import datetime

class Contact:
def __init__(self, name, projects, interval, reminder):
self.name = name
self.projects = projects
self.interval = datetime.timedelta(int(interval))
datepart = reminder.split('/')
self.reminder = datetime.date(int(datepart[2]), int(datepart[0]), int(datepart[1]))
def isdue(self):
if self.reminder < datetime.date.today():
return True
else:
return False

def readrolodex():
#'r' stands for 'read'
herdcatfile = file('herdcat.txt','r')
rolodex = []
source = herdcatfile.readlines()
herdcatfile.close()
for row in source:
contactpart = row.strip().split('|')
rolodex.append(Contact(contactpart[0], contactpart[1], contactpart[2], contactpart[3]))
return rolodex

def writerolodex(rolodex):
#'w' stands for 'write'
herdcatfile = file('herdcat.txt', 'w')
for contact in rolodex:
herdcatfile.write('%s|%s|%s|%s\n' % (contact.name,
contact.projects, contact.interval.days, contact.reminder.strftime('%m/%d/%Y')))
herdcatfile.close()

rolodex = readrolodex()
for contact in rolodex:
if contact.isdue():
print 'Has %s responded to you about %s since the last Reminder?' % (contact.name, contact.projects)
smartass = True
while smartass:
response = raw_input('Y/N:')
if response.upper() == 'Y':
#int() stands for 'make this string an integer'
contact.interval = contact.interval + datetime.timedelta(2)
smartass = False
elif response.upper() == 'N':
contact.interval = contact.interval - datetime.timedelta(1)
smartass = False
else:
print 'You did not type Y or N, please try again:'
contact.reminder = contact.reminder + contact.interval
print 'Name: %s\nProjects: %s\nInterval: %s\nReminder: %s' % (contact.name,
contact.projects, contact.interval, contact.reminder.strftime('%m/%d/%Y'))
writerolodex(rolodex)
The current contents of herdcat.txt:

Bruce Webber|Python, Lojban, Penguicon, hanging out|7|07/17/07
Tim Schmidt|Penguicon, Stepmania rig, RepRap|7|07/18/07
Gerald Gentry|Subgenius at Penguicon, Budget for Stepmania, next SMOS Party|7|07/18/07
Chris DiBona|Inviting software guests|7|07/16/07
Frank Hayes|Getting my article published in a Linux magazine|7|04/20/07

hacking

Previous post Next post
Up