OOP with MyTime
kjakupak at gmail.com
kjakupak at gmail.com
Thu Jul 3 11:21:29 EDT 2014
On Thursday, July 3, 2014 9:11:49 AM UTC-4, MRAB wrote:
> On 2014-07-03 13:51, kjakupak at gmail.com wrote:
>
> > On Wednesday, July 2, 2014 4:02:00 PM UTC-4, MRAB wrote:
>
> >> >
>
> >>
>
> >> If you want 'between' to be an instance method of the MyTime class, it
>
> >>
>
> >> needs 'self' as well as the 2 arguments 't1' and 't2'.
>
> >>
>
> >>
>
> >>
>
> >> You can then compare the hours, minutes and seconds of self against
>
> >>
>
> >> those of t1 and t2:
>
> >>
>
> >>
>
> >>
>
> >> def between(self, t1, t2):
>
> >>
>
> >> return (t1.hours, t1.minutes, t1.seconds) <= (self.hours,
>
> >>
>
> >> self.minutes, self.seconds) and (self.hours, self.minutes, self.seconds)
>
> >>
>
> >> <= (t2.hours, t2.minutes, t2.seconds)
>
> >>
>
> >>
>
> >>
>
> >> That could be shortened further using chained comparisons.
>
> >>
>
> >>
>
> >>
>
> >> Note that the code assumes that the times t1 and t2 are ordered, i.e.
>
> >>
>
> >> that time t1 is not later/greater than time t2.
>
> >
>
> > So I've now gotten this:
>
> > class MyTime:
>
> >
>
> > def __init__(self, hrs=0, mins=0, secs=0):
>
> > self.hours = hrs
>
> > self.minutes = mins
>
> > self.seconds = secs
>
> >
>
> > if self.seconds >= 60:
>
> > self.minutes += self.seconds // 60
>
> > self.seconds = self.seconds % 60
>
> >
>
> > if self.minutes >= 60:
>
> > self.hours += self.minutes // 60
>
> > self.minutes = self.minutes % 60
>
> >
>
> > if self.hours >= 24:
>
> > self.hours = self.hours % 24
>
> >
>
> > def get_sec(self):
>
> > return (self.hours * 60 + self.minutes) * 60 + self.seconds
>
> >
>
> > def __str__(self):
>
> > return "{:02d}:{:02d}:{:02d}".\
>
> > format(self.hours, self.minutes, self.seconds)
>
> >
>
> > def between(self, t1, t2):
>
> > return (t1.hours, t1.minutes, t1.seconds) <= (self.hours, self.minutes, self.seconds) and (self.hours, self.minutes, self.seconds) <= (t2.hours, t2.minutes, t2.seconds)
>
> >
>
> >
>
> > t1 = MyTime(9, 59, 59)
>
> > print("t1 =", t1)
>
> >
>
> > t2 = MyTime(10, 0, 1)
>
> > print("t2 =", t2)
>
> >
>
> > t3 = MyTime(10, 0, 0)
>
> > print("t3 =", t3)
>
> >
>
> > print("between(t2, t3, t1) =", between(t2, t3, t1))
>
> > print("between(t1, t3, t2) =", between(t1, t3, t2))
>
> > print("between(t3, t1, t2) =", between(t3, t1, t2))
>
> > print("between(t1, t2, t3) =", between(t1, t2, t3))
>
> >
>
> > Am I on the right track or? Not sure where to go from here
>
> >
>
> You need to decide whether you want 'between' to be a method of the
>
> MyTime class or a separate function.
>
>
>
> In the above code it's defined as a method, so you can say:
>
>
>
> t2.between(t3, t1)
>
>
>
> which means "is t2 between t3 and t1?".
>
>
>
> That would return False because t3 is greater than t1, but:
>
>
>
> t2.between(t1, t3)
>
>
>
> would return True.
>
>
>
> (I _did_ say that it assumes that the times are ordered.)
>
>
>
> BTW, gmail is messing up your messages. This will tell you how to fix
>
> it:
>
>
>
> https://wiki.python.org/moin/GoogleGroupsPython
I keep getting an invalid syntax on the t1 = (9, 59, 59) line, not sure why?
t1 = (9, 59, 59)
print("t1 =", t1)
t2 = (10, 0, 1)
print("t2 =", t2)
t3 = (10, 0, 0)
print("t3 =", t3)
print("between(t2, t3, t1) =", t2.between(t3, t1))
More information about the Python-list
mailing list