Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

dns_solver.py 946B

123456789101112131415161718192021222324252627282930313233343536
  1. from typing import Dict
  2. from pydantic import BaseModel, PrivateAttr
  3. from million.model.message import Message
  4. _default_file_path = 'data/DNS'
  5. class DNS_solver(BaseModel):
  6. file_path:str = _default_file_path
  7. _bank: Dict[str, int] | None = PrivateAttr(None)
  8. def solve(self, msg: Message) -> int:
  9. if self._bank == None:
  10. self._bank = self.load(self.file_path)
  11. k = self._get_key(msg)
  12. if k and k in self._bank: return self._bank[k]
  13. return None
  14. def load(self, file_name: str) -> Dict[str, int]:
  15. result = {}
  16. with open(file_name, 'r') as f:
  17. for line in f:
  18. a,b = line.split()
  19. result[a] = int(b)
  20. return result
  21. def _get_key(self, msg: Message) -> str:
  22. # look into msg attributes
  23. # find uri
  24. return (msg.share or None) and msg.share.link or \
  25. (msg.gifs or None) and msg.gifs[0].uri