[Tutor] date formatter

Christopher Spears cspears2002 at yahoo.com
Fri Aug 8 07:34:05 CEST 2008


Ok, here is the working version of my program.  Thanks for all of the advice:

#!/usr/bin/python

import time

class date_format(object):
	def __init__(self, month, day, year):
		month_dict = {("jan","january") : 1,
		              ("feb","february") :2,
			      ("mar", "march") : 3,
			      ("apr", "april") : 4,
			      ("may",) : 5,
			      ("jun", "june") : 6,
			      ("jul", "july") : 7,
			      ("aug", "august") : 8,
			      ("sep", "september") : 9,
			      ("oct", "october"): 10,
			      ("nov", "november"): 11,
			      ("dec", "december"): 12
			      }
		
		try:
			month_number = int(month)
		except ValueError:
			for eachKey in month_dict.keys():
				if month.lower() in eachKey:
					month_number = month_dict[eachKey]
					break
				else:
					month_number = 0
		
		if month_number==0 or day=="" or year=="":
			self.date =  time.localtime()
		else:
			self.date = (int(year), month_number, int(day), 0, 0, 0, 0, 1, -1)
	
	def display(self, format_indicator = None):
		if format_indicator == 'MDY':
			print time.strftime("%m/%d/%y",self.date)
		elif format_indicator == 'MDYY':
			print time.strftime("%m/%d/%Y",self.date)
		elif format_indicator == 'DMY':
			print time.strftime("%d/%m/%y",self.date)
		elif format_indicator == 'DMYY':
			print time.strftime("%d/%m/%Y",self.date)
		elif format_indicator == 'MODYY':
			print time.strftime("%b %d, %Y",self.date)
		else:
			print time.strftime("%a %b %d %Y",self.date)
		
			
if __name__ == "__main__":
	print "Welcome to the Date Formatter!"
	month = raw_input("Please enter a month (as a number): ")
	day = raw_input("Please enter a day: ")
	year = raw_input("Please enter a year: ")
	date_obj = date_format(month, day, year)
	format_indicator = raw_input("Enter a format indicator: ")
	date_obj.display(format_indicator.upper())


--- On Wed, 8/6/08, wesley chun <wescpy at gmail.com> wrote:

> From: wesley chun <wescpy at gmail.com>
> Subject: Re: [Tutor] date formatter
> To: cspears2002 at yahoo.com
> Cc: tutor at python.org
> Date: Wednesday, August 6, 2008, 11:39 PM
> >                try:
> >                        month = int(month)
> >                except ValueError:
> >                        for eachKey in
> month_dict.keys():
> >                                if month.lower() in
> eachKey:
> >                                        month =
> month_dict[eachKey]
> >                                else:
> >                                        month =
> ""
> >        :
> > I am having trouble dealing with the case where the
> user actually types in a month's name instead of the
> number:
> > io at io-station-1 ./chap13 180> python
> date_format_v01.py
> >        :
> >    if month.lower() in eachKey:
> > AttributeError: 'int' object has no attribute
> 'lower'
> >
> > Any suggestions?
> 
> 
> chris,
> 
> good 1st attempt.  all the problems are in the code snippet
> above. you
> only have a few flaws in your program preventing it from
> working:
> 
> a. you "wipe out" the originally entered month
> (by continually
> reassigning the month var)
> 
> b. you don't break out of the inner for-loop when u
> find a match and
> end up wiping out the correct match
> 
> c. the cause of the exception: the entry for May is a
> string and not a
> 1-item tuple. you need a trailing comma "," after
> "may" to make it a
> tuple.  the reason why the error is triggered is because
> you set a
> blank to month for every non-match. so when it gets to may,
> it makes
> this comparison:
> 
> >>> "" in "may"
> True
> 
> because of this, it sets...
> 
> month = month_dict["may"]  # so month == 5
> 
> so in the next loop iteration, your're trying to do...
> 
> 5.lower() in eachKey
> 
> which of course, fails. fix these problems, and you have a
> working solution!
> 
> best of luck!
> -- wesley
> - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
> "Python Web Development with Django", Addison
> Wesley, (c) 2008
> http://withdjango.com
> 
> wesley.j.chun :: wescpy-at-gmail.com
> python training and technical consulting
> cyberweb.consulting : silicon valley, ca
> http://cyberwebconsulting.com


      


More information about the Tutor mailing list