[Tutor] New keyword ?
Michele Alzetta
michele.alzetta at aliceposta.it
Fri Jun 18 08:29:56 EDT 2004
I'm still fiddling with timespans - only I've just found out that
WxPython has something with the same name but fundamentally different
( WxPythons' is an abstract periods of time, mine is a period with a
precise beginning and end, from which of course the abstract period can
be obtained) .. so I've started calling my class Period.
Besides the _contains_ function, I've added an olaps function which
returns true not only if a period is entirely contained in another, but
also if it overlaps. Here is the code:
-----------------------------------------------------------------
from datetime import datetime, date, timedelta
class Period(object):
def __init__(self, startimetuple, endtimetuple):
self.starttime = datetime(*startimetuple)
self.endtime = datetime(*endtimetuple)
self.isPeriod = True
def __contains__(self, period):
try:
if period.isPeriod:
return (self.starttime <= period.starttime) and \
(self.endtime >= period.endtime)
except AttributeError:
moment = datetime(*period)
return (self.starttime <= moment <= self.endtime)
def length(self):
return self.endtime - self.starttime
def olaps(self, period):
try:
if period.isPeriod:
return (( self.starttime < period.starttime \
< self.endtime) \
or ( self.starttime < period.endtime < self.endtime ))
except AttributeError:
moment = datetime(*period)
return (self.starttime <= moment \
<= self.endtime)
As I understand it, the 'in' keyword calls my __contains__ function,
e.g.
> week1 in fortnight1
True
Whereas to call my olaps function I have to do this:
> periodinstance.olaps(otherinstance)
True
Is it possible to create a new keyword (say, 'ovlps') to call my
function like this:
> week1 ovlps fortnight
If it is possible, would it be desirable ?
--
Michele Alzetta <michele.alzetta at aliceposta.it>
More information about the Tutor
mailing list