[docs]classKeywordStore(defaultdict):"""Stores the keywords output by an actor. Parameters ---------- actor The actor to which this store is attached to. filter A list of keyword names to filter. If provided, only those keywords will be tracked. """def__init__(self,actor:BaseActor,filter:list[str]|None=None):self.actor=actorself.name=self.actor.nameself.filter=filterdefaultdict.__init__(self,list)
[docs]defadd_reply(self,reply:Reply):"""Processes a reply and adds new entries to the store. Parameters ---------- reply The `.Reply` object containing the keywords output in a message from the actor. """forkeyword,valueinreply.message.items():ifself.filterisnotNoneandkeywordnotinself.filter:continuekey_out=KeywordOutput(keyword,reply.message_code,datetime.now(),value)ifkeywordinself:self[keyword].append(key_out)else:self[keyword]=[key_out]
[docs]defhead(self,keyword:str,n:int=1):"""Returns the first N output values of a keyword. Parameters ---------- keyword The name of the keyword to search for. n Return the first ``n`` times the keyword was output. """returnself[keyword][:n]
[docs]deftail(self,keyword:str,n:int=1):"""Returns the last N output values of a keyword. Parameters ---------- keyword The name of the keyword to search for. n Return the last ``n`` times the keyword was output. """returnself[keyword][-n:]
[docs]@dataclassclassKeywordOutput:"""Records a single output of a keyword. Parameters ---------- name The name of the keyword. message_code The message code with which the keyword was output. date A `.datetime` object with the date-time at which the keyword was output this time. value The value of the keyword when it was output. """name:strmessage_code:Anydate:datetimevalue:Any