Advertisement
fkudinov

14. Every Person is Unique / Вирішуємо задачі на Python CheckIO Українською

Oct 8th, 2023 (edited)
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.60 KB | Source Code | 0 0
  1. # --------------------  tasks/unique_person.py ------------
  2.  
  3. from datetime import datetime
  4.  
  5. CURRENT_DATE = datetime(2018, 1, 1)
  6.  
  7.  
  8. class Person:
  9.     def __init__(self, first_name, last_name, birth_date,
  10.                  job, working_years, salary, country, city, gender='unknown'):
  11.  
  12.         self.first_name = first_name
  13.         self.last_name = last_name
  14.         self.birth_date = datetime.strptime(birth_date, "%d.%m.%Y")
  15.         self.job = job
  16.         self.working_years = working_years
  17.         self.salary = salary
  18.         self.country = country
  19.         self.city = city
  20.         self.gender = gender
  21.  
  22.     def name(self) -> str:
  23.         # return self.first_name + " " + self.last_name
  24.         # return "%s %s" % (self.first_name, self.last_name)
  25.         # return "{} {}".format(self.first_name, self.last_name)
  26.         # return f"{self.first_name} {self.last_name}"
  27.         # pattern = "{} {}"
  28.         # return pattern.format(self.first_name, self.last_name)
  29.         pattern = "{name} {last}"
  30.         return pattern.format(last=self.last_name, name=self.first_name)
  31.  
  32.     def age(self) -> int:
  33.         years_delta = CURRENT_DATE.year - self.birth_date.year
  34.         if CURRENT_DATE.month < self.birth_date.month or \
  35.                 CURRENT_DATE.day < self.birth_date.day:
  36.             years_delta -= 1
  37.         return years_delta
  38.  
  39.     def money(self) -> str:
  40.         # return "{:_}".format(self.salary * self.working_years * 12).replace("_", " ")
  41.         return format(self.salary * self.working_years * 12, "_").replace("_", " ")
  42.  
  43.     def work(self) -> str:
  44.         prefix = "Is"
  45.         if self.gender == "male":
  46.             prefix = "He is"
  47.         elif self.gender == "female":
  48.             prefix = "She is"
  49.         return "{} a {}".format(prefix, self.job)
  50.  
  51.     def home(self) -> str:
  52.         return "Lives in {city}, {country}".format(city=self.city, country=self.country)
  53.  
  54.    
  55.    
  56. # ------------     tests/test_unique_person.py  ------------------
  57.  
  58. from datetime import datetime
  59.  
  60. import pytest
  61.  
  62. from tasks.unique_person import Person
  63.  
  64.  
  65. def test_person_date():
  66.     p1 = Person("John", "Smith", "19.09.1979", "welder", 15, 3600,
  67.                 "Canada", "Vancouver", "male")
  68.     assert p1.birth_date == datetime(1979, 9, 19)
  69.  
  70.  
  71. def test_person_name():
  72.     p1 = Person("John", "Smith", "19.09.1979", "welder", 15, 3600,
  73.                 "Canada", "Vancouver", "male")
  74.     assert p1.name() == "John Smith"
  75.  
  76.  
  77. @pytest.mark.parametrize(["birth_date", "age"], [
  78.     ["01.01.2010", 8],
  79.     ["02.01.2010", 7],
  80.     ["31.12.2009", 8],
  81. ])
  82. def test_person_age(birth_date, age):
  83.     p1 = Person("John", "Smith", birth_date, "welder", 15, 3600,
  84.                 "Canada", "Vancouver", "male")
  85.     assert p1.age() == age
  86.  
  87.  
  88. def test_person_1():
  89.     p1 = Person("John", "Smith", "19.09.1979", "welder", 15, 3600,
  90.                 "Canada", "Vancouver", "male")
  91.  
  92.     assert p1.name() == "John Smith", "Name"
  93.     assert p1.age() == 38, "Age"
  94.     assert p1.money() == "648 000", "Money"
  95.     assert p1.work() == "He is a welder", "Job"
  96.  
  97.  
  98. def test_person_2():
  99.     p2 = Person("Hanna Rose", "May", "05.12.1995", "designer", 2.2, 2150,
  100.                 "Austria", "Vienna")
  101.  
  102.     assert p2.work() == "Is a designer", "Job"
  103.     assert p2.home() == "Lives in Vienna, Austria", "Home"
  104.  
  105.  
  106. def test_person_3():
  107.     p1 = Person("Ann", "Smith", "19.09.1979", "welder", 15, 3600,
  108.                 "Canada", "Vancouver", "female")
  109.  
  110.     assert p1.name() == "Ann Smith", "Name"
  111.     assert p1.age() == 38, "Age"
  112.     assert p1.money() == "648 000", "Money"
  113.     assert p1.work() == "She is a welder", "Job"
  114.  
  115.  
Tags: checkio
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement