Advertisement
gruntfutuk

employee_class

Oct 30th, 2022
819
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.35 KB | None | 1 0
  1. from dataclasses import dataclass
  2.  
  3. from password_func import DEFAULT_PASSWORD, hash_password, password_bad
  4.  
  5.  
  6. @dataclass
  7. class Employee:
  8.     first: str
  9.     last: str
  10.     other: str
  11.     grade: str
  12.     salary: int
  13.     position: str = ""
  14.     password: str = DEFAULT_PASSWORD
  15.  
  16.     def __post_init__(self):  # just validating salary
  17.         if not isinstance(self.salary, int):
  18.             try:
  19.                 self.salary = int(self.salary)
  20.             except ValueError:
  21.                 raise ValueError('Salary was not a number')
  22.         self.password = hash_password(self.password)
  23.  
  24.     def __str__(self):  # for printing a record
  25.         bad_password = password_bad(self.password)
  26.         return (f"Employee: {self.last}, {self.first}.\n"
  27.                 f"\tGrade: {self.grade}, Position: {self.position}\n"
  28.                 f"\tSalary: £{self.salary}"
  29.                 f"\tDefault/empty/bad password: {bad_password}"
  30.                 )
  31.  
  32.     def __repr__(self):  # for saving to csv format
  33.         return f"{self.first},{self.last},{self.other},{self.grade},{self.salary},{self.position},{self.password}"
  34.  
  35.     def row(self):  # convert string of employee record to list for csv
  36.         return repr(self).split(',')
  37.  
  38.     def key(self):
  39.         """create a unique key for each employee based on all name fields """
  40.         return f"{self.last}_{self.first}_{self.other}".casefold()
  41.  
Tags: class employee
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement