[docs]classModelNames:"""Class generating model names from unique configuration string"""def__init__(self,prefix:str='Model'):self.prefix=prefixself.dict_of_names={}self.current_number=0def__call__(self,the_id:str)->str:"""Get a short model name from a unique ID :param the_id: id of the model :type the_id: str (or anything that can be used as a key for a dict) """the_name=self.dict_of_names.get(the_id)ifthe_nameisNone:the_name=f'{self.prefix}_{self.current_number:06d}'self.current_number+=1self.dict_of_names[the_id]=the_namereturnthe_name
[docs]defgenerate_unique_ids(list_of_ids:list[str])->dict[str,str]:"""If there are duplicates in the list, a new list is generated where there are renamed to obtain a list with unique IDs. :param list_of_ids: list of ids :type list_of_ids: list[str] :return: a dict that maps the unique names with the original name """counts=defaultdict(int)forthe_idinlist_of_ids:counts[the_id]+=1results={}forname,countincounts.items():ifcount==1:results[name]=nameelse:substitutes=[f'{name}_{i}'foriinrange(count)]fornew_nameinsubstitutes:results[new_name]=namereturnresults
[docs]defunique_product(*iterables:Iterable,max_memory_mb:int=1024)->Iterator[tuple]:"""Generate the Cartesian product of multiple iterables, keeping only the unique entries. Raises a MemoryError if memory usage exceeds the specified threshold. :param iterables: Variable number of iterables to compute the Cartesian product from. :type iterables: Iterable :param max_memory_mb: Maximum memory usage in megabytes (default: 1024MB). :type max_memory_mb: int :return: Yields unique entries from the Cartesian product. :rtype: Iterator[tuple] """mb_to_bytes=1024*1024max_memory_bytes=max_memory_mb*mb_to_bytes# Convert MB to bytesseen=set()# Set to store seen entriestotal_memory=0# Track memory usageforitemsinproduct(*iterables):ifitemsnotinseen:seen.add(items)item_size=sum(sys.getsizeof(item)foriteminitems)total_memory+=item_sizeiftotal_memory>max_memory_bytes:raiseMemoryError(f'Memory usage exceeded the specified threshold: 'f'{total_memory/mb_to_bytes:.1f} MB > 'f'{max_memory_bytes/mb_to_bytes} MB.')yielditems