import re from typing import Dict from million.model.message import Message import million.analyze.dns_solver as dns _memoization: Dict[Message, int] = {} _dns_solver: dns.DNS_solver = dns.DNS_solver() def get(msg: Message) -> int: """ Returns the estimated value counted in this message """ return _memoization.get(msg, _compute(msg)) def reset(msg: Message) -> None: """ Drop memorized value of this Message """ if msg in _memoization: _memoization.pop(msg) def reset() -> None: """ Drop every memorized message value """ _memoization.clear() def _compute(msg: Message) -> int: value = _dns_solver.solve(msg) or \ _computeContent(msg) or \ None _memoization[msg] = value return value def _computeContent(msg: Message) -> int: # TODO parse potential math expressions in content match = msg.content and re.search(r"\d+", msg.content) if match: value = int(match.group()) else: value = None return value