[docs]defcurrent_time():"""Returns the current time in the format of ``YYYY-MM-DD HH:MM:SS``."""returndatetime.now().strftime("%Y-%m-%d %H:%M:%S")
[docs]classDatetimeDict(dict[str,str]):"""A dictionary that records the time when a key is added."""
[docs]defadd_only(self,eventname:str)->tuple[str,str]:"""Adds a key with the current time no matter the key does not exist. Args: eventname (str): The name of the event. Returns: tuple[str, str]: The name of the event and the time. """self[eventname]=current_time()returneventname,self[eventname]
[docs]defadd_serial(self,eventname:str)->tuple[str,str]:"""Adds a key with the current time and a serial number if the key exists. Args: eventname (str): The name of the event. Returns: tuple[str, str]: The name of the event and the time. """repeat_times_plus_one=1fordinself:ifd.startswith(eventname):repeat_times_plus_one+=1eventname_with_times=f"{eventname}."+f"{repeat_times_plus_one}".rjust(3,"0")self[eventname_with_times]=current_time()returneventname_with_times,self[eventname_with_times]
[docs]defloads(self,datetimes:dict[str,str]):"""Loads a dictionary of datetimes. Args: datetimes (dict[str, str]): A dictionary of datetimes. """fork,vindatetimes.items():self[k]=v
[docs]deflast_events(self,number:int=1)->list[tuple[str,str]]:"""Returns the last event and its time. Args: number (int): The number of the last event. Returns: list[tuple[str, str]]: The last event and its time. """returnlist(self.items())[-number:]