Reputation:
Could someone explain to me how to correctly use classmethod with inheritance in Python? I need to create two classmethod methods:
to makefull-timeime employee.
to make a part-time employee in Employee class which is inhering from BaseEmployee.
Seem that I fully don't understand a concept:(
Ok, so the question is how to properly create a classmethod and then how to create a fulltime employee?
Thanks
from datetime import datetime
class Error(Exception):
"""Base class for exception"""
pass
class InvalidDateOfEmployment(Error):
"""Except bigger date then today and seniority more than 50 years"""
pass
class Application:
@staticmethod
def main(self):
name = input('Name: ')
last_name = input('Last name: ')
date_of_employement = datetime.strptime(input('Date of Employement (2022-03-02): '), '%Y.%m.%d')
if Application.date_of_employment_validation(datetime.today(), date_of_employement):
raise InvalidDateOfEmployment
employee = BaseEmployee(name, last_name, date_of_employement)
@staticmethod
def date_of_employment_validation(today: datetime, date: datetime):
diff = today - date
diff_in_years = round(diff.days / 365.25)
return 50 > diff_in_years > 0 #diff_in_years < 50 and diff_in_years > 0
class BaseEmployee:
def __init__(self, name, last_name, date_of_employement):
self.name = name
self.last_name = last_name
self.date_of_employement = date_of_employement
self.now = datetime.now()
@property
def employment_time(self):
return (self.now - self.date_of_employement).days
def __lt__(self, other):
return self.employment_time < other.employment_time
def __repr__(self):
return self.name, self.last_name, self.date_of_employement
class Employee(BaseEmployee):
def __init__(self, bonus, type_of_employment, hour_pay_rate, name, last_name, date_of_employement):
super().__init__(name, last_name, date_of_employement)
self.bonus = bonus
self.type_of_employment = type_of_employment
self.hour_pay_rate = hour_pay_rate
@classmethod
def create_fulltime(cls, bonus, type_of_employment, hour_pay_rate):
return cls(bonus, 160, hour_pay_rate)
# @classmethod
# def create_partime(cls, name, last_name, date_of_employement, bonus, hour_pay_rate):
# return cls(name, last_name, date_of_employement, bonus, hour_pay_rate, 80)
def calculate_sallary(self):
return self.hour_pay_rate * self.type_of_employment + self.bonus
if __name__ == '__main__':
Application.main()
def test_sort_employees():
#given
a = BaseEmployee('A', 'A', datetime(2020, 12, 10))
b = BaseEmployee('B', 'B', datetime(2020, 10, 10))
employees = [a, b]
#when
sorted_employees = sorted(employees)
#then
assert sorted_employees[0] == a
Upvotes: 0
Views: 801
Reputation: 42
For example:
from datetime import datetime
class Employee:
bonus = 1
base_hpr = 300
@classmethod
@property
def salary_bonus_multiplier(cls):
return cls.bonus
def __init__(self, name, last_name, date_of_employement, hour_pay_rate = base_hpr):
self.name = name
self.last_name = last_name
self.date_of_employement = date_of_employement
self.hour_pay_rate = hour_pay_rate
self.now = datetime.now()
@property
def employment_time(self):
return (self.now - self.date_of_employement).days
def calculate_sallary(self):
return self.employment_time * self.hour_pay_rate * self.salary_bonus_multiplier
def __lt__(self, other):
return self.employment_time < other.employment_time
def __repr__(self):
return self.name, self.last_name, self.date_of_employement
class HalfTimeEmployee(Employee):
bonus = 0.5
class FullTimeEmployee(Employee):
pass
if __name__ == '__main__':
a = HalfTimeEmployee('A', 'A', datetime(2020, 10, 10))
b = FullTimeEmployee('B', 'B', datetime(2020, 10, 10))
print(a.calculate_sallary(), b.calculate_sallary())
classmethod mean to use only class avaluable variables.
Inheritance helps to change them in a particular case.
Upvotes: 0