Skip to content

Instantly share code, notes, and snippets.

@qiuyujx
Created October 31, 2021 12:39
Show Gist options
  • Save qiuyujx/a2dbb144743dc3d233c60d6641d7bff6 to your computer and use it in GitHub Desktop.
Save qiuyujx/a2dbb144743dc3d233c60d6641d7bff6 to your computer and use it in GitHub Desktop.
import re
class Employee:
def __init__(self, name, year_joined):
self.name = name
self.year_joined = year_joined
@classmethod
def from_string(cls, string):
name = re.search('I am (.*?) ', string).group(1)
year_joined = int(re.search('joined in (\d+).', string).group(1))
return cls(name, year_joined)
@staticmethod
def calc_year(year_joined, year_now):
length = year_now - year_joined
if length > 0:
return f'{length} years'
else:
return 'less than one year'
def seniority(self):
n_years = self.calc_year(self.year_joined, datetime.now().year)
print(f'{self.name} has worked in our company for {n_years}')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment