[Tutor] working with time spans ?
Michele Alzetta
michele.alzetta at aliceposta.it
Fri Jun 4 19:47:22 EDT 2004
Il sab, 2004-06-05 alle 01:30, Tim Peters ha scritto:
> > shiftperiod = TimeSpan(periodend,periodstart)
>
> The order of the arguments is backwards there, isn't it?
copied wrongly, of course
> This does the same thing (but where I swapped the order of arguments as
> noted above):
>
> class TimeSpan(object):
> def __init__(self, startimetuple, endtimetuple):
> self.starttime = startimetuple
> self.endtime = endtimetuple
>
> def __contains__(self, timespan):
> return (self.starttime <= timespan.starttime and
> self.endtime >= timespan.endtime)
Yes, but this can contain any tuple, which means I can't extract
self.startime and feed it in to one of the time functions if I ever
wanted to.
> You'll eventually want to think about using the datetime module. Doing so
> will check that the dates passed in are sane, and provide a strong base to
> build fancier stuff on. For example, just replace the first 4 lines above
> with:
>
> from datetime import datetime
>
> class TimeSpan(object):
> def __init__(self, startimetuple, endtimetuple):
> self.starttime = datetime(*startimetuple)
> self.endtime = datetime(*endtimetuple)
>
> Everything else works the same then, and not even the __contains__ method
> needs to change. You can easily add other interesting methods to your
> TimeSpan class then; e.g., add
>
> def length(self):
> return self.endtime - self.starttime
>
> and then
>
> print shiftperiod.length()
> print shift1.length()
> print shift2.length()
>
> prints
>
> 30 days, 0:00:00
> 12:00:00
> 12:00:00
That's really great.
Er ... what on earth does the "*" in *startimetuple mean ?
--
Michele
More information about the Tutor
mailing list