website-copy icon indicating copy to clipboard operation
website-copy copied to clipboard

A few suggestion for python - clock core exercise

Open GreatBahram opened this issue 5 years ago • 6 comments

I'd like to add a few suggestion to make the notes better:

  • I Think one possbile solution and also Pythonic (at the same time) could be this one:
MINUTES_PER_HOUR = 60
MINUTES_PER_DAY = 1440


class Clock:
    def __init__(self, hour, minute):
        self.hour, self.minute = divmod((hour * MINUTES_PER_HOUR + minute) % MINUTES_PER_DAY, 60)

    def __str__(self):
        return f"{self.hour:02}:{self.minute:02}"

    def __eq__(self, other):
        if isinstance(other, Clock):
            return (self.hour, self.minute) == (other.hour, other.minute)
        return NotImplemented

    def __add__(self, minutes):
        return Clock(self.hour, self.minute + minutes)

    def __sub__(self, minutes):
        return Clock(self.hour, self.minute - minutes)
  • It's not good idea to do this as the return statement:
return self.__class__(self.h, self.m - minutes) # Because Explicit is better than implicit!
# So, this is the something that you can find in python built-in modules.
return Clock(self.h, self.m - minutes) 
  • Secondly, this is the oldest way that someone could substitute string in python, why this one?, you cold string format method or even f-string
def __str__(self):
        return '%02d:%02d' % (self.h, self.m)
# Instead
return f"{self.hour:02}:{self.minute:02}"
  • No need to mention this is too mathematic:
self.h, self.m = divmod((hour * 60 + minute) % (24 * 60), 60)
  • And this one could much more simpler, like:
return self.h == other.h and self.m == other.m
# Instead
return (self.hour., self.minute) == (other.hour, other.minute) # I really don't get it why h and m? why not hour and minute?
# tuple comparison is well worth teaching.
  • Most people are using python 3, why you guys always subclass object:
class Clock(object):
# Instead
class Clock:
  • Again, no need to mention, divmod is here to solve this problem:
class Clock(object):
    def __init__(self, hour, minute):
        while minute >= 60:
            hour += 1
            minute -= 60
        while minute < 0:
            hour -= 1
            minute += 60
        self.minute = minute
        self.hour = hour % 24

Having said this, I saw this implementation which is far better but maybe hard for a newbie python developer to understand:

from datetime import date, datetime, time, timedelta


class Clock:
    def __init__(self, hours, minutes):
        start = datetime.combine(date.today(), time(0, 0))
        self.dt = start + timedelta(hours=hours, minutes=minutes)

    def __str__(self):
        return self.dt.strftime('%H:%M')

    def __eq__(self, other):
        if not isinstance(other, Clock):
            return NotImplemented
        return (self.dt.time()) == (other.dt.time())

    def __add__(self, minutes):
        self.dt += timedelta(minutes=minutes)
        return self

    def __sub__(self, minutes):
        self.dt -= timedelta(minutes=minutes)
        return self

GreatBahram avatar Jun 21 '19 12:06 GreatBahram