This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Restaurants(object): | |
def __init__(self, wk1): | |
self.wk1 = wk1 | |
def __getattr__(self, item): | |
raise AttributeError('available attributes are .wk1') | |
wk1_city_1 = Restaurants([10, 20, 30, 40, 50]) | |
print(wk1_city_1.wk1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Restaurants(object): | |
def __init__(self, wk1): | |
self._wk1 = wk1 | |
def __setattr__(self, key, value): | |
if not isinstance(value, list): | |
raise TypeError('please enter weekly takings a list') | |
for val in value: | |
if not isinstance(val, (int, float)): | |
raise ValueError('weeking takings must be integers or floats') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def __setattr__(self, name, value): | |
self.name = value | |
# Do not do this | |
# since every time an attribute is assigned, __setattr__() is called, this | |
# is recursion. | |
# so this really means self.__setattr__('name', value). Since the method | |
# keeps calling itself, the recursion goes on forever causing a crash | |
def __setattr__(self, name, value): | |
self.__dict__[name] = value |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Restaurants(object): | |
def __init__(self, wk1): | |
self._wk1 = wk1 | |
def __ge__(self, other): | |
return sum(self._wk1) >= sum(other._wk1) | |
def __le__(self, other): | |
return sum(self._wk1) <= sum(other._wk1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Restaurants(object): | |
def __init__(self, wk1): | |
self._wk1 = wk1 | |
def __getitem__(self, item): | |
weekday_dict = {'Monday': 0, 'Tuesday': 1, 'Wednesday' : 2, 'Thursday': 3, 'Friday': 4} | |
if item in weekday_dict: | |
return self._wk1[weekday_dict[item]] | |
else: | |
return self._wk1[item] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Restaurants(object): | |
def __init__(self, wk1): | |
self._wk1 = wk1 | |
def __iadd__(self, other): | |
retval = sum(self._wk1) + sum(other._wk1) | |
return retval | |
wk1_city = Restaurants([10, 20, 30, 40, 50]) | |
wk1_same_city = Restaurants([20, 40, 60, 80, 100]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Restaurants(object): | |
def __init__(self, wk1): | |
self._wk1 = wk1 | |
def __add__(self, other): | |
retval = [x + y for x, y in zip(self._wk1, other._wk1)] | |
return Restaurants(retval) | |
def __str__(self): | |
return str(self._wk1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Employees(object): | |
def __init__(self, name, age, service_length): | |
self._name = name | |
if 18 <= age <= 100: | |
self._age = age | |
else: | |
raise AttributeError('The age range must be between 18 and 100') | |
self._service_length = service_length | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Employees(object): | |
def __init__(self, name, age, service_length): | |
self.name = name | |
self.age = age | |
self.service_length = service_length | |
@property | |
def name(self): | |
return self._name.title() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def attr_check(method): | |
def inner(ref, name, age, service): | |
for attr in [age, service]: | |
if not isinstance(attr, int): | |
raise TypeError('age and service must be of type int') | |
return method(ref, name, age, service) | |
return inner | |
class Employees(object): | |
@attr_check |