[Tutor] Please help to debug this function.. it takes a date and num. of days and returns startdate and enddate

2006-11-06 Thread Asrarahmed Kadri
Hi Folks,

I have written a function that takes a date and an integer representing the number of days.

Please test it, comment onthe logic. 

Here is the code:
CODE BEGINS


 The function takes two arguments, date and number of days. It checks for the right format and if the format is okay, it calculates the end date. 
import stringimport datetimeimport traceback
def dateCheck(date1,num_days): flag = True startdate = None enddate = None  if num_days  0 or num_days  31: flag = False print The argument for -n has to be between 0 and 31
 return (flag,startdate,enddate)  else:  date_lst = string.split(date1,/) ln = len(date_lst) if ln != 3 :  flag = False
 print The argument for -D option has to be in the format: dd/mm/ return (flag,startdate,enddate) else: date_lst.reverse() print date_lst
 try: if int(date_lst[0])  2000: flag = False print The year cannot be earlier than 2000 and it should be in the format ''.
 return (flag,startdate,enddate) except ValueError: flag = False err_msg = traceback.format_exc() index = string.find(err_msg,'Value')
 print err_msg[index:] return (flag,startdate,enddate)   try: startdate = datetime.date(int(date_lst[0]),int(date_lst[1]),int(date_lst[2]))
 enddate = startdate + datetime.timedelta(days=num_days) print startdate, enddate 
 except ValueError:  flag = False startdate = None enddate = None err_msg = traceback.format_exc() index = 
string.find(err_msg,'Value') print err_msg[index:] return (flag,startdate,enddate)
 if enddate  datetime.date.today(): enddate = datetime.date.today() 
 if startdate  datetime.date.today(): flag = False startdate = None enddate = None print The -D option should have a date not later than today's date
   return (flag,startdate,enddate)
CODE ENDS





TIA.

Regards,
Asrarahmed



-- To HIM you shall return. 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Please help to debug this function.. it takes a date and num. of days and returns startdate and enddate

2006-11-06 Thread Danny Yoo


On Mon, 6 Nov 2006, Asrarahmed Kadri wrote:

 I have written a function that takes a date and an integer representing 
 the number of days.

 Please test it, comment on the logic .


Hi Asrarahmed,

You should be able to write your own tests too.  You might want to look 
at:

 http://www.diveintopython.org/unit_testing/index.html

for an example of how to write unit tests; the author there goes through 
an example with a Roman Numeral program written with unit tests.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Please help to debug this function.. it takes a date and num. of days and returns startdate and enddate (fwd)

2006-11-06 Thread Danny Yoo
[Forwarding to Tutor; busy at the moment]

-- Forwarded message --
Date: Mon, 6 Nov 2006 18:41:54 +
From: Asrarahmed Kadri [EMAIL PROTECTED]
To: Danny Yoo [EMAIL PROTECTED]
Subject: Re: [Tutor] Please help to debug this function.. it takes a date and
 num. of days and returns startdate and enddate

Hi Danny,

Can you please test it !!!
Also I would like to know if it can be coded in a better way...

TIA.

Regards,
Asrarahmed


On 11/6/06, Danny Yoo [EMAIL PROTECTED] wrote:



 On Mon, 6 Nov 2006, Asrarahmed Kadri wrote:

 I have written a function that takes a date and an integer representing
 the number of days.

 Please test it, comment on the logic .


 Hi Asrarahmed,

 You should be able to write your own tests too.  You might want to look
 at:

 http://www.diveintopython.org/unit_testing/index.html

 for an example of how to write unit tests; the author there goes through
 an example with a Roman Numeral program written with unit tests.




-- 
To HIM you shall return.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor