Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.005 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'messages.': 0.05; 'mrab': 0.05; 'class,': 0.07; 'wednesday,': 0.07; 'arguments': 0.09; 'messing': 0.09; 'method,': 0.09; 'def': 0.12; '"is': 0.16; 'chained': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'message- id:@mrabarnett.plus.com': 0.16; 'received:192.168.1.4': 0.16; 'subject:OOP': 0.16; 'fix': 0.17; 'wrote:': 0.18; 'separate': 0.22; 'header:User-Agent:1': 0.23; 'hours,': 0.24; 'url:moin': 0.24; 'decide': 0.24; "i've": 0.25; 'compare': 0.26; 'this:': 0.26; 'defined': 0.27; 'skip:" 20': 0.27; 'header:In-Reply-To:1': 0.27; 'code': 0.31; 'url:wiki': 0.31; 'assumes': 0.31; 'class': 0.32; 'url:python': 0.33; 'skip:_ 10': 0.34; 'could': 0.34; 'subject:with': 0.35; 'false': 0.36; 'i.e.': 0.36; 'method': 0.36; 'url:org': 0.36; 'seconds': 0.37; 'gmail': 0.38; 'to:addr:python- list': 0.38; 'track': 0.38; 'sure': 0.39; 'to:addr:python.org': 0.39; 'how': 0.40; 'tell': 0.60; 'further': 0.61; 'times': 0.62; 'email addr:gmail.com': 0.63; 'july': 0.63; 'here': 0.66; 'minutes': 0.67; 'between': 0.67; 'gotten': 0.74; "'between'": 0.84; 'mins': 0.84 X-CM-Score: 0.00 X-CNFS-Analysis: v=2.1 cv=OZcWD3jY c=1 sm=1 tr=0 a=0nF1XD0wxitMEM03M9B4ZQ==:117 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=0Bzu9jTXAAAA:8 a=u9EReRu7m0cA:10 a=ihvODaAuJD4A:10 a=IkcTkHD0fZMA:10 a=EBOSESyhAAAA:8 a=pGLkceISAAAA:8 a=8AHkEIZyAAAA:8 a=Xb8tJRaNXG66fyw1exoA:9 a=QEXdDO2ut3YA:10 a=MSl-tDqOz04A:10 X-AUTH: mrabarnett:2500 Date: Thu, 03 Jul 2014 14:11:49 +0100 From: MRAB User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.6.0 MIME-Version: 1.0 To: python-list@python.org Subject: Re: OOP with MyTime References: <7020a5d8-96a9-4fa4-8e69-4c7593dedee3@googlegroups.com> In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 102 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1404393113 news.xs4all.nl 2955 [2001:888:2000:d::a6]:46107 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:73903 On 2014-07-03 13:51, kjakupak@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