#!/usr/bin/env python
class Time60(object):
"""Time60-track hours and minutes"""
def __init__(self,arg1=0,arg2=0):
"""Time60 constructor -takes hours and minutes"""
if isinstance(arg1,tuple):
self.hr=arg1[0]
self.min=arg1[1]
elif isinstance(arg1,dict):
self.hr=arg1['hr']
self.min=arg1['min']
elif isinstance(arg1,str):
self.hr=int(arg1.split(':')[0])
self.min=int(arg1.split(':')[1])
else:
self.hr=arg1
self.min=arg2
if self.min>=60:
self.hr += self.min//60
self.min=self.min%60
def __str__(self):
"""Time60 - string representation"""
return '%02d:%02d' % (self.hr, self.min)
def __repr__(self):
return "'"+"Time60(%s:%s)"%(str(self.hr),str(self.min))+"'"
def __add__(self, other):
"""Time60 - overloading the addition operator"""
nh = self.hr+other.hr
nm = self.min+other.min
no = self.__class__(nh, nm)
return no
def __radd__(self, other):
"""Time60 - overloading the right addition operator"""
print('using radd method')
nh = self.hr+other.hr
nm = self.min+other.min
return self.__class__(nh,nm)
def __iadd__(self, other):
"""Time60 - overloading in-place addition"""
self.hr += other.hr
self.min += other.min
return self
x = Time60((11,22))
y = Time60(33,44)
z = Time60({'min':55,'hr':66})
a = Time60()
b = Time60('77:88')
print(x)
print(y)
print(z)
print(a)
print(b)
class Test:
pass
t=Test()
t.hr=8
t.min=8
print(t+b)
print(repr(b))