Advertisement
UniQuet0p1

Untitled

Oct 12th, 2020
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.01 KB | None | 0 0
  1. """Messenger."""
  2.  
  3. class User:
  4. """User class."""
  5.  
  6. def __init__(self, name):
  7. """
  8. User constructor.
  9.  
  10. :param name: Name of the user.
  11. """
  12. self.name = name
  13.  
  14.  
  15. class Chat:
  16. """Chat class."""
  17.  
  18. def __init__(self, name, users):
  19. """
  20. Chat constructor.
  21.  
  22. :param name: Name of the chat.
  23. :param users: Users in the chat.
  24. """
  25. self.name = name
  26. self.users = users
  27. self.messages = []
  28.  
  29.  
  30. class Message:
  31. """Message class."""
  32.  
  33. def __init__(self, user, content):
  34. """
  35. Message constructor.
  36.  
  37. :param user: Author of the message.
  38. :param content: Content of the message.
  39. """
  40. self.user = user
  41. self.content = content
  42. self.reactions = 0
  43.  
  44.  
  45. def write_message(user: User, chat: Chat, content: str) -> None:
  46. """
  47. Write a message to given chat.
  48.  
  49. Create a message with given values and then add it to the chat's messages.
  50.  
  51. :param user: Author of the message.
  52. :param chat: Chat to write the message to.
  53. :param content: Content of the message.
  54. """
  55. message = Message(user, content)
  56. if message.user in chat.users:
  57. chat.messages.append(message)
  58. else:
  59. pass
  60. pass
  61.  
  62.  
  63. def delete_message(chat: Chat, message: Message) -> None:
  64. """
  65. Delete message from chat.
  66.  
  67. :param chat: Chat to delete the message from.
  68. :param message: Message to delete from chat.
  69. """
  70. if message in chat.messages:
  71. chat.messages.remove(message)
  72. else:
  73. pass
  74. pass
  75.  
  76.  
  77. def get_messages_by_user(user: User, chat: Chat) -> list:
  78. """
  79. Get messages by user in chat.
  80.  
  81. :param user: User whose messages to get.
  82. :param chat: Chat from where to get user's messages.
  83. :return: A list of messages.
  84. """
  85. pass
  86.  
  87.  
  88. def react_to_last_message(chat: Chat) -> None:
  89. """
  90. Add reaction to last message in chat.
  91.  
  92. :param chat: Chat in which the message is.
  93. """
  94. chat.messages[-1].reactions += 1
  95. pass
  96.  
  97.  
  98. def find_most_reacted_message(chat: Chat) -> Message:
  99. """
  100. Find the most reacted message in chat.
  101.  
  102. :param chat: Chat to get the message from.
  103. :return: Most reacted message.
  104. """
  105. pass
  106.  
  107.  
  108. def count_reactions_in_chat(chat: Chat) -> int:
  109. """
  110. Count all reactions in chat.
  111.  
  112. :param chat: Said chat.
  113. :return: The amount of reactions.
  114. """
  115. count = 0
  116. for each in chat.messages:
  117. count += each.reactions
  118. return count
  119.  
  120.  
  121. def count_reactions_by_chat(chats: list) -> dict:
  122. """
  123. Count reactions in every chat.
  124.  
  125. The function should return a dict where the key is the name of a chat and the value is the amount of reactions.
  126.  
  127. :param chats: The chats in question.
  128. :return: A dictionary as described.
  129. """
  130. pass
  131.  
  132.  
  133. if __name__ == '__main__':
  134. user1 = User("Alma")
  135. user2 = User("Ago")
  136. chat = Chat("Python 2020", [user1, user2])
  137.  
  138. write_message(user1, chat, "Parim kohvipiim")
  139. write_message(user2, chat, "Eestimaa farmidest")
  140. write_message(user2, chat, "Piim")
  141. write_message(user1, chat, "Farmi")
  142. for message in chat.messages:
  143. print(f"{message.user.name}: {message.content}")
  144. # Alma: Parim kohvipiim
  145. # Ago: Eestimaa farmidest
  146. # Ago: Piim
  147. # Alma: Farmi
  148.  
  149. to_be_deleted = get_messages_by_user(user2, chat)
  150. for message in to_be_deleted:
  151. delete_message(chat, message)
  152. for message in chat.messages:
  153. print(f"{message.user.name}: {message.content}")
  154. # Alma: Parim kohvipiim
  155. # Alma: Farmi
  156.  
  157. react_to_last_message(chat)
  158. print(chat.messages[0].reactions) # 0
  159. print(chat.messages[-1].reactions) # 1
  160.  
  161. most_reacted = find_most_reacted_message(chat)
  162. print(f"{most_reacted.content}: {most_reacted.reactions}") # Farmi: 1
  163.  
  164. print(count_reactions_in_chat(chat)) # 1
  165. print(count_reactions_by_chat([chat])) # {"Python 2020": 1}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement