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


Groups > comp.lang.python > #98739

Re: What is wrong in this example code?

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Nathan Hilterbrand <nhilterbrand@gmail.com>
Newsgroups comp.lang.python
Subject Re: What is wrong in this example code?
Date Thu, 12 Nov 2015 19:24:23 -0500
Lines 77
Message-ID <mailman.287.1447419693.16136.python-list@python.org> (permalink)
References <1b204966-46c6-4890-a5a5-8addd20343a1@googlegroups.com> <383401eb-f8be-4f7a-bd63-3520eedc069e@googlegroups.com> <BJednVGxdfl7utjLnZ2dnUU7-K-dnZ2d@giganews.com>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8
X-Trace news.uni-berlin.de scItU1qUjoSCEb+xRjM5fAgKQ8qVP9WR910f2I3nANfA==
Return-Path <nhilterbrand@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; '"__main__":': 0.07; '__name__': 0.07; 'formatting': 0.07; 'rest,': 0.07; 'subject:code': 0.07; 'broke': 0.09; 'method:': 0.09; 'pointers': 0.09; 'similar,': 0.09; 'tuple': 0.09; 'email addr:python.org&gt;': 0.10; 'python': 0.10; 'def': 0.13; 'ignore': 0.14; 'suggest': 0.15; 'thu,': 0.15; '(x/y,': 0.16; '60,': 0.16; 'division,': 0.16; 'email addr:python.org>': 0.16; 'hh:mm:ss': 0.16; 'parameters,': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'seconds,': 0.16; 'shorten': 0.16; 'wrote:': 0.16; 'any,': 0.18; 'language': 0.19; '2015': 0.20; '(the': 0.22; 'hours,': 0.22; 'sets': 0.23; 'this:': 0.23; 'second': 0.24; 'header:In- Reply-To:1': 0.24; 'message-id:@mail.gmail.com': 0.27; 'actual': 0.28; 'division': 0.29; 'larry': 0.29; 'convert': 0.29; "i'm": 0.30; 'url:mailman': 0.30; 'seconds': 0.31; 'probably': 0.31; 'skip:s 30': 0.31; 'post': 0.31; 'skip:_ 10': 0.32; 'posting': 0.32; 'useful': 0.33; 'class': 0.33; 'url:python': 0.33; 'url:listinfo': 0.34; 'skip:d 20': 0.34; 'skip:& 20': 0.35; 'received:google.com': 0.35; 'nov': 0.35; 'question,': 0.35; 'something': 0.35; 'but': 0.36; 'too': 0.36; 'url:org': 0.36; 'received:209.85': 0.36; 'skip:_ 40': 0.36; 'skip:{ 10': 0.36; 'to:addr:python-list': 0.36; 'subject:?': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'skip:& 10': 0.37; 'method': 0.37; '12,': 0.37; 'suggestion': 0.37; 'thought': 0.37; 'received:209': 0.38; 'skip:p 20': 0.38; 'does': 0.39; 'received:209.85.214': 0.39; 'url:mail': 0.40; 'to:addr:python.org': 0.40; 'some': 0.40; 'your': 0.60; 'leading': 0.61; 'above,': 0.63; 'leaving': 0.63; 'more': 0.63; 'minutes': 0.64; 'beat': 0.66; 'python-list': 0.66; 'email name:python-list': 0.67; 'skip:\xc2 10': 0.67; '8bit%:100': 0.70; 'confusing': 0.84; 'nathan': 0.84; 'subject:this': 0.85; 'explanation:': 0.91
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=FgigNTogQP+xJQaai1mF6BBPY6gsQJqZRdaYZclMDrY=; b=0u6k8tuJk+l244EwI1Afv6volLp6xIWdBuPV4JS8ZOMrY2ORoPPl9KF07wM7MbIlEy nhA5pNpVS/oCYr5HC5Ja5cFoDijso5drzDbCivOMaEgCxjCHrM/0VFp4CmHWzdfBdp69 YujW3soLOKvY1YlM/tCns4uKseHKJp6iLrSU8WxN1jFW7+btW9PVwZ/UFZQ+ER4JOZtu P3Mia9841BqSD3uOr7ZpqZcXLIt5C9epX43iyDAB5lpeYPF8QK0naK4dDjzFbtxFr9Ti c4Gpm6o7BTUfXklbJzXgcQ6JfpYPYal863XfV+cHCIkh4c1p/Dv4tbWUfHwYPaaXS23k aqsg==
X-Received by 10.182.236.101 with SMTP id ut5mr11074520obc.73.1447374263967; Thu, 12 Nov 2015 16:24:23 -0800 (PST)
In-Reply-To <BJednVGxdfl7utjLnZ2dnUU7-K-dnZ2d@giganews.com>
X-Mailman-Approved-At Fri, 13 Nov 2015 08:01:31 -0500
X-Content-Filtered-By Mailman/MimeDel 2.1.20+
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
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>
Xref csiph.com comp.lang.python:98739

Show key headers only | View raw


On Thu, Nov 12, 2015 at 6:59 PM, Larry Hudson via Python-list <
python-list@python.org> wrote:

Nothing to do with your original question, just a trivial suggestion which
> you are free to ignore.  You can shorten this tick() method by using the
> divmod() function.  It does a division and returns both the quotient AND
> the remainder.  IOW, given divmod(x, y) it returns the tuple (x/y, x%y).
> It is a very useful function.  Try this:
>
> def tick(self):
>     xtra, self._seconds = divmod(self._seconds + 1, 60)
>     xtra, self._minutes = divmod(self._minutes + xtra, 60)
>     self._hours += xtra
>
> Explanation:  (to shorten this, I'm leaving off the leading "self._" from
> the actual code.)
> The first divmod() sets xtra = (seconds+1) / 60, which is the 'overflow'
> if any, from the division by 60, and seconds is the updated seconds limited
> to 0-59 (the result of (seconds+1) % 60).  The second divmod() does the
> same thing to update minutes (if xtra != 0) and xtra is set to the
> 'overflow' from that division, which is then added to hours.
>
> More confusing perhaps, but definitely shorter.
> As I said above, use it if you want or ignore it if it's too confusing.
>
>      -=- Larry -=-
>
> --
> https://mail.python.org/mailman/listinfo/python-list
>

Beat me to the punch.  I was about to suggest something similar, but I
thought that since one using this class would probably be "tick()-ing" more
than "str()-ing", It might also be better to store the value in seconds,
and convert to HH:MM:SS upon stringification; again using divmod:

class Clock(object):

    def __init__(self,hours=0,minutes=0,seconds=0):
        self.set(hours,minutes,seconds)

    def tick(self):
        self.__secs+=1

    def set(self,hours, minutes, seconds):
        self.__secs = seconds + (minutes*60) + (hours*60*60)

    def __str__(self):
        rest, seconds = divmod(self.__secs, 60)
        hours, minutes = divmod(rest, 60)

        return "%02d:%02d:%02d" % (hours, minutes, seconds)

    def display(self):
        print(str(self))


if __name__ == "__main__":

    x = Clock()
    print("Default construction, no parameters, Before ticks: {}".format(x))
    for i in range(10000):
        x.tick()
    print("After ticks: {}".format(x))

    x = Clock(hours=2, minutes=20, seconds=5)
    print("\nConstructor with hours=2, minutes=20, seconds=5: {}".format(x))

    print("Test of display() method: ",end=' ')
    x.display()


This is my first post here, and I am a Python n00b (coming from that
four-letter word language "p***"), so if I broke some top/bottom posting or
formatting rules, I apologize, and would appreciate any posting pointers

Nathan H.

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


Thread

What is wrong in this example code? fl <rxjwg98@gmail.com> - 2015-11-12 05:57 -0800
  Re: What is wrong in this example code? fl <rxjwg98@gmail.com> - 2015-11-12 06:07 -0800
    Re: What is wrong in this example code? John Wong <gokoproject@gmail.com> - 2015-11-12 10:10 -0500
    Re: What is wrong in this example code? Larry Hudson <orgnut@yahoo.com> - 2015-11-12 15:59 -0800
      Re: What is wrong in this example code? Nathan Hilterbrand <nhilterbrand@gmail.com> - 2015-11-12 19:24 -0500

csiph-web