[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Re: datelib pythonification

alex goretoy

2/21/2010 1:38:00 AM

hello all,
since I posted this last time, I've added a new function dates_diff and
modified the dates_dict function to set timedelta values returned by
dates_diff in the returned dict

def dates_dict(self,*targs,**dargs):
"""
dates_dict() - takes params same as prefs()
returns dict key/value pair of formatted/calculated dates
key is a date, value is timedelta enddate-startdate

"""
self.prefs(*targs,**dargs)
d={}
try:
if self.format!="":
d[self.startdate.strftime(self.format)]=self.dates_diff()
else:
d[self.startdate]=self.dates_diff()
except ValueError:
d["%s"%self.startdate]=self.dates_diff()
while self.startdate<self.enddate:
a=self.calc(*targs,**dargs)[0]
d[a]=self.dates_diff()
self.reset_dates()
return d
def dates_diff(self,*targs):
"""
dates_diff - return timedelta difference between startdata and
enddate
takes nothing, a tuple, a list or 2 date objects as params
return enddate - startdate timedelta

"""
start,end=(self.startdate,self.enddate)
if targs:
if(len(targs)==1):
if(type(targs[0])==type(()) or targs[0] == type([])):
if(len(targs[0])==2):
start,end=(targs[0][0],targs[0][1])
elif(len(targs)== 2):
if(type(targs[0])==type(date.today()) and targs[1] ==
type(date.today())):
start,end=(targs[0],targs[1])
return end-start

-Alex Goretoy
1 Answer

John Machin

2/21/2010 9:16:00 AM

0

On Feb 21, 12:37 pm, alex goretoy <agore...@gmail.com> wrote:
> hello all,
>     since I posted this last time, I've added a new function dates_diff and

[SNIP]

I'm rather unsure of the context of this posting ... I'm assuming that
the subject "datelib pythonification" refers to trying to make
"datelib" more "pythonic", with which you appear to need help.

Looking just at the new "function" (looks like a method to me)
dates_diff, problems include:

1. Mostly ignores PEP-8 about spaces after commas, around operators
2. Checks types
3. Checks types using type(x) == type(y)
4. Inconsistent type checking: checks types in case of
dates_diff(date1, date2) but not in case of dates_diff([date1, date2])
5. Doesn't check for 3 or more args.
6. The 0-arg case is for what purpose?
7. The one-arg case is overkill -- if the caller has the two values in
alist, all you are saving them from is the * in dates_diff(*alist)
8. Calling type(date.today()) once per 2-arg call would be a gross
extravagance; calling it twice per 2-arg call is mind-boggling.
9. start,end=(targs[0][0],targs[0][1]) ... multiple constant
subscripts is a code smell; this one is pongier than usual because it
could easily be replaced by start, end = targs[0]

Untested fix of problems 1, 3, 4, 5, 8, 9:

DATE_TYPE = type(date.today())

def dates_diff(self, *targs):
nargs = len(targs)
if nargs == 0:
return self.enddate - self.startdate
if nargs == 1:
arg = targs[0]
if not isinstance(arg, (list, tuple)) or len(arg) != 2:
raise Exception(
"single arg must be list or tuple of length 2")
start, end = arg
elif nargs == 2:
start, end = targs
else:
raise Exception("expected 0,1, or 2 args; found %d" % nargs)
if isinstance(start, DATE_TYPE) and isinstance(end, DATE_TYPE):
return end - start
raise Exception("both values must be of type DATE_TYPE")

HTH,

John