Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #73848 > unrolled thread

OOP with MyTime

Started bykjakupak@gmail.com
First post2014-07-02 12:20 -0700
Last post2014-07-03 17:23 +0100
Articles 11 — 5 participants

Back to article view | Back to comp.lang.python


Contents

  OOP with MyTime kjakupak@gmail.com - 2014-07-02 12:20 -0700
    Re: OOP with MyTime Akira Li <4kir4.1i@gmail.com> - 2014-07-02 23:45 +0400
    Re: OOP with MyTime MRAB <python@mrabarnett.plus.com> - 2014-07-02 21:02 +0100
      Re: OOP with MyTime kjakupak@gmail.com - 2014-07-03 05:51 -0700
        Re: OOP with MyTime Chris Angelico <rosuav@gmail.com> - 2014-07-03 23:01 +1000
          Re: OOP with MyTime kjakupak@gmail.com - 2014-07-03 06:08 -0700
            Re: OOP with MyTime Chris Angelico <rosuav@gmail.com> - 2014-07-03 23:17 +1000
        Re: OOP with MyTime MRAB <python@mrabarnett.plus.com> - 2014-07-03 14:11 +0100
          Re: OOP with MyTime kjakupak@gmail.com - 2014-07-03 08:21 -0700
            Re: OOP with MyTime Chris Angelico <rosuav@gmail.com> - 2014-07-04 01:35 +1000
            Re: OOP with MyTime Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-07-03 17:23 +0100

#73848 — OOP with MyTime

Fromkjakupak@gmail.com
Date2014-07-02 12:20 -0700
SubjectOOP with MyTime
Message-ID<7020a5d8-96a9-4fa4-8e69-4c7593dedee3@googlegroups.com>
I'm trying to write a boolean function that takes two Mytime objects, t1 and t2 as arguments, and returns True if the object falls inbetween the two times.

This is a question from the How to Think Like a Computer Scientist book, and I need help.

What I've gotten so far:

class MyTime:
    def __init__(self, hrs=0, mins=0, secs=0):
        self.hours = hrs
        self.minutes = mins
        self.seconds = secs
    def between(t1, t2):
        if float(t1 <= t3) and float(t3 < t2):
            return True
        else:
            return False

I just don't understand how to make a function that uses MyTime objects into the boolean function? Any help would be great.

[toc] | [next] | [standalone]


#73849

FromAkira Li <4kir4.1i@gmail.com>
Date2014-07-02 23:45 +0400
Message-ID<mailman.11417.1404330343.18130.python-list@python.org>
In reply to#73848
kjakupak@gmail.com writes:

> I'm trying to write a boolean function that takes two Mytime objects, t1 and t2 as arguments, and returns True if the object falls inbetween the two times.
>
> This is a question from the How to Think Like a Computer Scientist book, and I need help.
>
> What I've gotten so far:
>
> class MyTime:
>     def __init__(self, hrs=0, mins=0, secs=0):
>         self.hours = hrs
>         self.minutes = mins
>         self.seconds = secs
>     def between(t1, t2):
>         if float(t1 <= t3) and float(t3 < t2):
>             return True
>         else:
>             return False
>
> I just don't understand how to make a function that uses MyTime objects into the boolean function? Any help would be great.

A method accepts the object itself as the first parameter:

  import functools

  @functools.total_ordering
  class MyTime:
    ...
    def inbetween(self, t1, t2):
        """Return whether `self` is in [t1, t2) right-open range."""
        return t1 <= self < t2 # `self` is the object itself
    def _astuple(self):
        return (self.hours, self.minutes, self.seconds)
    def __lt__(self, other):
        """Return whether `self < other`."""
        return self._astuple() < other._astuple()
    def __eq__(self, other):
        """Return whether `self == other`."""
        return self._astuple() == other._astuple()

See
https://docs.python.org/3/library/functools.html#functools.total_ordering

Example:

  >>> MyTime(1).inbetween(MyTime(0), MyTime(2))
  True

It is equivalent to:

  >>> MyTime(0) <= MyTime(1) < MyTime(2)
  True


--
Akira

[toc] | [prev] | [next] | [standalone]


#73851

FromMRAB <python@mrabarnett.plus.com>
Date2014-07-02 21:02 +0100
Message-ID<mailman.11418.1404331330.18130.python-list@python.org>
In reply to#73848
On 2014-07-02 20:20, kjakupak@gmail.com wrote:
> I'm trying to write a boolean function that takes two Mytime objects, t1 and t2 as arguments, and returns True if the object falls inbetween the two times.
>
> This is a question from the How to Think Like a Computer Scientist book, and I need help.
>
> What I've gotten so far:
>
> class MyTime:
>      def __init__(self, hrs=0, mins=0, secs=0):
>          self.hours = hrs
>          self.minutes = mins
>          self.seconds = secs
>      def between(t1, t2):
>          if float(t1 <= t3) and float(t3 < t2):
>              return True
>          else:
>              return False
>
> I just don't understand how to make a function that uses MyTime objects into the boolean function? Any help would be great.
>
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.

[toc] | [prev] | [next] | [standalone]


#73900

Fromkjakupak@gmail.com
Date2014-07-03 05:51 -0700
Message-ID<fd30a5f9-c446-435a-9f0a-74dd2d608c5c@googlegroups.com>
In reply to#73851
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

[toc] | [prev] | [next] | [standalone]


#73901

FromChris Angelico <rosuav@gmail.com>
Date2014-07-03 23:01 +1000
Message-ID<mailman.11457.1404392478.18130.python-list@python.org>
In reply to#73900
On Thu, Jul 3, 2014 at 10:51 PM,  <kjakupak@gmail.com> wrote:
> So I've now gotten this:
> class MyTime:
>     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)
> print("between(t2, t3, t1) =", between(t2, t3, t1))

And what happens when you run this code? A NameError, I would expect.
Do you understand how to define and call methods?

ChrisA

[toc] | [prev] | [next] | [standalone]


#73902

Fromkjakupak@gmail.com
Date2014-07-03 06:08 -0700
Message-ID<51a84c60-f1c0-4a30-94de-e047f1b553bc@googlegroups.com>
In reply to#73901
On Thursday, July 3, 2014 9:01:09 AM UTC-4, Chris Angelico wrote:
> 
> 
> 
> And what happens when you run this code? A NameError, I would expect.
> 
> Do you understand how to define and call methods?
> 
> 
> 
> ChrisA

Altered the code. But yes a nameerror came up

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.get_sec() <= (self.get_sec()) and (self.get_sec()) <= (t2.get_sec())

s = MyTime()
t1 = s(9, 59, 59)
print("t1 =", t1)

t2 = s(10, 0, 1)
print("t2 =", t2)

t3 = s(10, 0, 0)
print("t3 =", t3)

print("between(t2, t3, t1) =", s.between(t2, t3, t1))
print("between(t1, t3, t2) =", s.between(t1, t3, t2))
print("between(t3, t1, t2) =", s.between(t3, t1, t2))

[toc] | [prev] | [next] | [standalone]


#73904

FromChris Angelico <rosuav@gmail.com>
Date2014-07-03 23:17 +1000
Message-ID<mailman.11459.1404393472.18130.python-list@python.org>
In reply to#73902
On Thu, Jul 3, 2014 at 11:08 PM,  <kjakupak@gmail.com> wrote:
> Altered the code. But yes a nameerror came up

When that sort of thing happens, you have three basic approaches to
solving the problem.

1) Read the traceback, look at the line of code it points to, and see
if you can figure out what it means.
2) Post here with the full traceback so we have a chance of being able
to help you
3) Play 20 questions with us, while making the gathering of
information as hard as pulling teeth.

Suggestion: The third option is the least effective. :)

ChrisA

[toc] | [prev] | [next] | [standalone]


#73903

FromMRAB <python@mrabarnett.plus.com>
Date2014-07-03 14:11 +0100
Message-ID<mailman.11458.1404393113.18130.python-list@python.org>
In reply to#73900
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

[toc] | [prev] | [next] | [standalone]


#73905

Fromkjakupak@gmail.com
Date2014-07-03 08:21 -0700
Message-ID<d3e593c1-4d61-4c33-b879-d16e700bc42f@googlegroups.com>
In reply to#73903
On Thursday, July 3, 2014 9:11:49 AM UTC-4, MRAB wrote:
> 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

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))

[toc] | [prev] | [next] | [standalone]


#73906

FromChris Angelico <rosuav@gmail.com>
Date2014-07-04 01:35 +1000
Message-ID<mailman.11460.1404401715.18130.python-list@python.org>
In reply to#73905
On Fri, Jul 4, 2014 at 1:21 AM,  <kjakupak@gmail.com> wrote:
> I keep getting an invalid syntax on the t1 = (9, 59, 59) line, not sure why?
>
> t1 = (9, 59, 59)

Two points. Firstly, as I said before, posting the entire exception
helps us enormously. Secondly, with most computerized parsers, the
file is processed top-down, left-to-right, so it's possible for a
syntax error to be discovered a bit after where it actually happens -
but not before. So when you get parsing errors, check the immediately
preceding line; sometimes it's there. Since you haven't shown us that
line, we have no idea what's happening. Showing us the code below the
highlighted error line is of no value; showing us the code before that
line is helpful.

Also: Please don't use Google Groups, or if you must, please clean up
its messes.

ChrisA

[toc] | [prev] | [next] | [standalone]


#73907

FromMark Lawrence <breamoreboy@yahoo.co.uk>
Date2014-07-03 17:23 +0100
Message-ID<mailman.11461.1404404641.18130.python-list@python.org>
In reply to#73905
On 03/07/2014 16:21, kjakupak@gmail.com wrote:
> On Thursday, July 3, 2014 9:11:49 AM UTC-4, MRAB wrote:

I'm pleased to see that you have answers.  In return would you please 
use the mailing list 
https://mail.python.org/mailman/listinfo/python-list or read and action 
this https://wiki.python.org/moin/GoogleGroupsPython to prevent us 
seeing double line spacing and single line paragraphs, thanks.

-- 
My fellow Pythonistas, ask not what our language can do for you, ask 
what you can do for our language.

Mark Lawrence

---
This email is free from viruses and malware because avast! Antivirus protection is active.
http://www.avast.com

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web