Advertisement
UniQuet0p1

Untitled

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