userlib.py 1.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. # -*- coding: utf-8 -*-
  2. """
  3. This module implements a basic User management library
  4. """
  5. class User(object):
  6. def __init__(
  7. self,
  8. id=0,
  9. first_name='first',
  10. last_name='last',
  11. email_address='',
  12. company=''
  13. ):
  14. self.id = id
  15. self.first_name = first_name
  16. self.last_name = last_name
  17. self.email_address = email_address
  18. self.company = company
  19. @property
  20. def display_name(self):
  21. return '{} {}'.format(self.first_name, self.last_name)
  22. class UserNotFound(Exception):
  23. pass
  24. class UserLib(object):
  25. """
  26. A dummy library to list, add and delete users
  27. """
  28. USERS = []
  29. def add_user(self, user: User) -> User:
  30. user.id = 1 + max(u.id for u in UserLib.USERS)
  31. UserLib.USERS.append(user)
  32. return user
  33. def del_user(self, user_id: int) -> None:
  34. try:
  35. UserLib.USERS.pop(user_id - 1)
  36. except:
  37. raise UserNotFound
  38. def get_user(self, user_id: int) -> User:
  39. try:
  40. return UserLib.USERS[user_id - 1]
  41. except:
  42. raise UserNotFound
  43. def get_users(self) -> [User]:
  44. return UserLib.USERS
  45. UserLib.USERS.append(
  46. User(**{
  47. 'id': 1,
  48. 'first_name': 'Damien',
  49. 'last_name': 'Accorsi',
  50. 'email_address': 'damien.accorsi@algoo.fr',
  51. 'company': 'Algoo',
  52. }),
  53. )