def sm_handler(cmd): id = int(cmd[1]) if id not in message_dic: raise Exc.MessageIdNotFoundExc(id) message = message_dic[id] type = message['type'] socialValue = message['socialValue'] if type == 0: person1_id = message['person1']['id'] person2_id = message['person2']['id'] if not graph.has_edge(person1_id, person2_id): raise Exc.RelationNotFoundExc(person1_id, person2_id) graph.nodes[person1_id]['socialValue'] += socialValue graph.nodes[person2_id]['socialValue'] += socialValue graph.nodes[person2_id]['messages'].append(message) if type == 1: group = message['group'] person_id = message['person1']['id'] if person_id not in group: raise Exc.PersonIdNotFoundExc(person_id) for dst_id in group: graph.nodes[dst_id]['socialValue'] += socialValue del message_dic[id] print("Ok")
def qv_handler(cmd): id1 = int(cmd[1]) id2 = int(cmd[2]) if id1 not in graph: raise Exc.PersonIdNotFoundExc(id1) if id2 not in graph: raise Exc.PersonIdNotFoundExc(id2) if not graph.has_edge(id1, id2): raise Exc.RelationNotFoundExc(id1, id2) print(graph[id1][id2]['value'])
def qci_handler(cmd): id1 = int(cmd[1]) id2 = int(cmd[2]) if id1 not in graph: raise Exc.PersonIdNotFoundExc(id1) if id2 not in graph: raise Exc.PersonIdNotFoundExc(id2) if graph.has_edge(id1, id2): print(1) else: print(0)
def dfg_handler(cmd): person_id = int(cmd[1]) group_id = int(cmd[2]) if group_id not in group_dic: raise Exc.GroupIdNotFoundExc(group_id) if person_id not in graph: raise Exc.PersonIdNotFoundExc(person_id) group = group_dic[group_id] if person_id not in group: raise Exc.EqualPersonIdExc(person_id) del group[person_id] print("Ok")
def ar_handler(cmd): id1 = int(cmd[1]) id2 = int(cmd[2]) value = int(cmd[3]) if id1 not in graph: raise Exc.PersonIdNotFoundExc(id1) if id2 not in graph: raise Exc.PersonIdNotFoundExc(id2) if graph.has_edge(id1, id2): raise Exc.EqualRelationExc(id1, id2) graph.add_edge(id1, id2, value=value) print("Ok")
def atg_handler(cmd): person_id = int(cmd[1]) group_id = int(cmd[2]) if group_id not in group_dic: raise Exc.GroupIdNotFoundExc(group_id) if person_id not in graph: raise Exc.PersonIdNotFoundExc(person_id) if person_id in group_dic[group_id]: raise Exc.EqualPersonIdExc(person_id) group_dic[group_id][person_id] = {} group_dic[group_id][person_id] = graph.nodes[person_id] print("Ok")
def cn_handler(cmd): id1 = int(cmd[1]) id2 = int(cmd[2]) if id1 not in graph: raise Exc.PersonIdNotFoundExc(id1) if id2 not in graph: raise Exc.PersonIdNotFoundExc(id2) name1 = graph.nodes[id1]['name'] name2 = graph.nodes[id2]['name'] if name1 > name2: print(">") elif name1 < name2: print("<") else: print("=")
def ap_handler(cmd): id = int(cmd[1]) name = cmd[2] age = int(cmd[3]) if id in graph: raise Exc.EqualPersonIdExc(id) graph.add_node(id, id=id, name=name, age=age, socialValue=0, messages=[]) graph.add_edge(id, id, value=0) print("Ok")
def qgvs_handler(cmd): ans = 0 id = int(cmd[1]) if id not in group_dic: raise Exc.GroupIdNotFoundExc(id) for id1 in group_dic: for id2 in group_dic: if graph.has_edge(id1, id2): ans += graph[id1][id2]['value'] print(ans)
def qnr_handler(cmd): ans = 1 id = int(cmd[1]) if id not in graph: raise Exc.PersonIdNotFoundExc(id) name = graph.nodes[id]['name'] dic = graph._node for node in dic.values(): if name > node['name']: ans += 1 print(ans)
def qgam_handler(cmd): ans = 0 id = int(cmd[1]) if id not in group_dic: raise Exc.GroupIdNotFoundExc(id) if len(group_dic[id]) == 0: print(0) else: for person in group_dic[id].values(): ans += person['age'] ans //= len(group_dic[id]) print(ans)
def am_handler(cmd): message_id = int(cmd[1]) socialValue = int(cmd[2]) type = int(cmd[3]) id1 = int(cmd[4]) id2 = int(cmd[5]) if type == 0: if id1 not in graph or id2 not in graph: print("The person with this number does not exist") return else: if id2 not in group_dic: print("Group does not exist") return if id1 not in graph: print("The person with this number does not exist") return if message_id in message_dic: raise Exc.EqualMessageIdExc(message_id) if type == 0 and id1 == id2: raise Exc.EqualPersonIdExc(id1) message_dic[message_id] = {} if type == 0: message_dic[message_id]['id'] = message_id message_dic[message_id]['person1'] = graph.nodes[id1] message_dic[message_id]['person2'] = graph.nodes[id2] message_dic[message_id]['type'] = type message_dic[message_id]['socialValue'] = socialValue else: message_dic[message_id]['id'] = message_id group = group_dic[id2] message_dic[message_id]['person1'] = graph.nodes[id1] message_dic[message_id]['group'] = group message_dic[message_id]['type'] = type message_dic[message_id]['socialValue'] = socialValue print("Ok")
def qrm_handler(cmd): id = int(cmd[1]) if id not in graph: raise Exc.PersonIdNotFoundExc(id) messages = graph.nodes[id]['messages'] if len(messages) == 0: print("None") return else: for i in range(min(len(messages), 4)): if i == min(len(messages), 4) - 1: print("Ordinary message", end='') else: print("Ordinary message", end='; ') print()
async def dequeue_task(self): while True: try: m: PurpleMessage = await self.queue_unprocessed.get() m.increment_attempt_account() m_str = m.get_discord_string(title=self.title, timestamp=self.timestamp, mention=self.mention, strip_mention=self.strip_mention) log_m_str = m_str.replace("\n", "\\n") try: if await self.can_message(): if not await self.passes_spam_control(m): continue await self.discord_channel_obj.send(content=m_str) self.lg.debug( "Relayed to channel id: {} Message: \"{}\"".format( self.channel_id, log_m_str)) m.set_posted() async with self.posted_messages_lock: self.posted_messages.append(m) else: raise Exc.PermissionCannotText(self.channel_id) except Exception as ex: self.lg.warning( "Error when relaying message to channel id: {} - {}". format(self.channel_id, str(ex))) if m.max_retries_exceeded(): self.lg.warning( "Discarded as max retry attempts exceeded on channel id: {} Message: \"{}\"" .format(self.channel_id, log_m_str)) else: self.lg.warning( "Requeue Attempt: {} of {} on channel id: {} " "Message: \"{}\"".format(m.get_send_attempts(), m.get_max_send_attempts(), self.channel_id, log_m_str)) asyncio.get_event_loop().create_task( self.requeue_failed_message(m)) except Exception as ex: print(ex) traceback.print_exc() await asyncio.sleep(5) finally: await asyncio.sleep(1)
def qgav_handler(cmd): ans = 0 id = int(cmd[1]) if id not in group_dic: raise Exc.GroupIdNotFoundExc(id) if len(group_dic[id]) == 0: print(0) else: mean = 0 for person in group_dic[id].values(): mean += person['age'] mean //= len(group_dic[id]) for person in group_dic[id].values(): age = person['age'] ans += (age - mean)**2 ans //= len(group_dic[id]) print(ans)
def mase(self, mase): if not(mase > 0): raise Exc.reiksme_per_maza("Lokomotyvo mase turi buti didesne uz 0") self._mase = mase
def vezamo_krovinio_mase(self, vezamo_krovinio_mase): if not(vezamo_krovinio_mase >= 0): raise Esc.reiksme_per_maza("Vezamo krovinio mase negali buti mazesne uz 0") if (vezamo_krovinio_mase > self.maksimali_krovinio_mase): raise Exc.reiksme_per_didele("Vezamo krovinio mase negali buti didesne uz maksimalia") self._vezamo_krovinio_mase = vezamo_krovinio_mase
def savitoji_mase(self, savitoji_mase): if not(savitoji_mase > 0): raise Exc.reiksme_per_maza("Savitoji mase turi buti didesne uz nuli") self._savitoji_mase = savitoji_mase
def maksimali_krovinio_mase(self, maksimali_krovinio_mase): if not(maksimali_krovinio_mase >= 0): raise Exc.reiksme_per_maza("Maksimali krovinio mase turi buti ne mazasne uz 0") self._maksimali_krovinio_mase = maksimali_krovinio_mase
def ag_handler(cmd): id = int(cmd[1]) if id in group_dic: raise Exc.EqualGroupIdExc(id) group_dic[id] = {} print("Ok")
def qgps_handler(cmd): id = int(cmd[1]) if id not in group_dic: raise Exc.GroupIdNotFoundExc(id) print(len(group_dic[id]))
def qsv_handler(cmd): id = int(cmd[1]) if not id in graph: raise Exc.PersonIdNotFoundExc(id) print(graph.nodes[id]['socialValue'])
def maksimali_tempimo_mase(self, maksimali_tempimo_mase): if not (maksimali_tempimo_mase > 0): raise Exc.reiksme_per_maza("Maksimali tempimo mase turi buti didesne uz nuli!") self._maksimali_tempimo_mase = maksimali_tempimo_mase