Advertisement
UniQuet0p1

Untitled

Oct 12th, 2020
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.22 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. most = 0
  106. message = ""
  107. for each in chat.messages:
  108. if each.reactions > most:
  109. message = each.content
  110. most = each.reactions
  111. else:
  112. pass
  113. return message
  114.  
  115.  
  116. def count_reactions_in_chat(chat: Chat) -> int:
  117. """
  118. Count all reactions in chat.
  119.  
  120. :param chat: Said chat.
  121. :return: The amount of reactions.
  122. """
  123. count = 0
  124. for each in chat.messages:
  125. count += each.reactions
  126. return count
  127.  
  128.  
  129. def count_reactions_by_chat(chats: list) -> dict:
  130. """
  131. Count reactions in every chat.
  132.  
  133. The function should return a dict where the key is the name of a chat and the value is the amount of reactions.
  134.  
  135. :param chats: The chats in question.
  136. :return: A dictionary as described.
  137. """
  138. pass
  139.  
  140.  
  141. if __name__ == '__main__':
  142. user1 = User("Alma")
  143. user2 = User("Ago")
  144. chat = Chat("Python 2020", [user1, user2])
  145.  
  146. write_message(user1, chat, "Parim kohvipiim")
  147. write_message(user2, chat, "Eestimaa farmidest")
  148. write_message(user2, chat, "Piim")
  149. write_message(user1, chat, "Farmi")
  150. for message in chat.messages:
  151. print(f"{message.user.name}: {message.content}")
  152. # Alma: Parim kohvipiim
  153. # Ago: Eestimaa farmidest
  154. # Ago: Piim
  155. # Alma: Farmi
  156.  
  157. to_be_deleted = get_messages_by_user(user2, chat)
  158. for message in to_be_deleted:
  159. delete_message(chat, message)
  160. for message in chat.messages:
  161. print(f"{message.user.name}: {message.content}")
  162. # Alma: Parim kohvipiim
  163. # Alma: Farmi
  164.  
  165. react_to_last_message(chat)
  166. print(chat.messages[0].reactions) # 0
  167. print(chat.messages[-1].reactions) # 1
  168.  
  169. most_reacted = find_most_reacted_message(chat)
  170. print(f"{most_reacted.content}: {most_reacted.reactions}") # Farmi: 1
  171.  
  172. print(count_reactions_in_chat(chat)) # 1
  173. print(count_reactions_by_chat([chat])) # {"Python 2020": 1}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement