"Asrarahmed Kadri" <[EMAIL PROTECTED]> wrote

> Please test it, comment on the logic .

I haven't tested it but here are some comments:

> import string
> import datetime
> import 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:

You don't need the else since the if block returns.
Thus you only reach the followiong code if the test is false.
This reduces the anumber of levels of indenting by one which
improves readability and thus maintenance.

>        date_lst = string.split(date1,"/")

better to use the string methods. ie date1.split('/')

>        ln = len(date_lst)
>        if ln != 3 :

you could have combined those lines into one since you never
use 'ln' except in the test.

if len(date1.split('/') != 3:


>            flag = False
>            print "The argument for -D option has to be in the 
> format:
> dd/mm/yyyy"
>            return (flag,startdate,enddate)
>        else:

Same as above, the else is just adding levels of indentation.

>            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 'yyyy'."
>                    return (flag,startdate,enddate)
>            except ValueError:
>                flag = False
>                err_msg = traceback.format_exc()
>                index = string.find(err_msg,'Value')

Again use the string method:

index = err_msg.find('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)

Since this block is essentially identical to the except block above
you could package them both as a function which would shorten
the code a little.

>            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"

Since this is a function and not part of the user interface there
shouldn't really be any knowledge of the -D option in this code.
What if you want to call it from another module that uses
different options?

It would be better to raise a ValueError which can be caught
by the external program:

 raise ValueEerror ("startdate later than today")


>            return (flag,startdate,enddate)
> <CODE ENDS>

HTH,

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld 


_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to