import re from typing import Dict from million.model.message import Message _memoization: Dict[Message, int] = {} 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: # TODO WIP - DNS to resolve audio, gif, pictures with counts value = _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