On 11/12/2015 06:07 AM, fl wrote:
On Thursday, November 12, 2015 at 8:58:33 AM UTC-5, fl wrote:
Hi,
<snip>
def tick(self):
""" Time will be advanced by one second """
if self.__seconds == 59:
self.__seconds = 0
if (self.__minutes == 59):
self.__minutes = 0
self.__hours = 0 if self.__hours==23 else self.__hours+1
else:
self.__minutes += 1;
else:
self.__seconds += 1;
<snip>
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