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


Groups > comp.lang.python > #73849

Re: OOP with MyTime

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed4a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'example:': 0.03; 'else:': 0.03; 'great.': 0.07; 'arguments,': 0.09; 'falls': 0.09; 'objects,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'def': 0.12; '"""return': 0.16; '__lt__(self,': 0.16; 'accepts': 0.16; 'boolean': 0.16; 'function?': 0.16; 'parameter:': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'scientist': 0.16; 'subject:OOP': 0.16; 'trying': 0.19; 'help.': 0.21; '>>>': 0.22; 'import': 0.22; 'header:User-Agent:1': 0.23; 'question': 0.24; "i've": 0.25; 'equivalent': 0.26; 'header:X -Complaints-To:1': 0.27; 'function': 0.29; "i'm": 0.30; 'writes:': 0.31; 'class': 0.32; 'skip:m 30': 0.32; 'url:python': 0.33; 'skip:_ 10': 0.34; 'subject:with': 0.35; 'objects': 0.35; 'false': 0.36; 'method': 0.36; 'url:org': 0.36; 'two': 0.37; 'message- id:@gmail.com': 0.38; 'url:library': 0.38; 'to:addr:python-list': 0.38; 'itself': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'how': 0.40; 'url:3': 0.61; 'first': 0.61; 'email addr:gmail.com': 0.63; 'book,': 0.68; 'gotten': 0.74; 'mins': 0.84; 'received:89': 0.85
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Akira Li <4kir4.1i@gmail.com>
Subject Re: OOP with MyTime
Date Wed, 02 Jul 2014 23:45:28 +0400
References <7020a5d8-96a9-4fa4-8e69-4c7593dedee3@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain
X-Gmane-NNTP-Posting-Host 89.169.229.68
User-Agent Gnus/5.13 (Gnus v5.13) Emacs/24.3.50 (gnu/linux)
Cancel-Lock sha1:DkYqMjKbGQS9F3O9Af0K0abPKlE=
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.11417.1404330343.18130.python-list@python.org> (permalink)
Lines 57
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1404330343 news.xs4all.nl 2917 [2001:888:2000:d::a6]:43247
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:73849

Show key headers only | View raw


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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web