[docs]defrate_limited_batch(batch_size:int=5,max_per_second:int=5):""" Decorator for a batch-processing function that takes a list of items and yields results. Applies rate limiting: max N calls per second, B items per batch. """assertbatch_size>0andmax_per_second>0defdecorator(func:Callable[...,Iterable[Any]]):@functools.wraps(func)defwrapper(cls,items:Iterable[Any],*args,**kwargs)->Iterable[Any]:batch=[]start_time=Noneforiteminitems:logging.info(f"Processing item: {item}")batch.append(item)iflen(batch)>=batch_size:ifstart_time:elapsed=time.time()-start_timesleep_time=max(0,1.0-elapsed)ifsleep_time>0:time.sleep(sleep_time)start_time=time.time()forresultinfunc(cls,batch,*args,**kwargs):yieldresultbatch=[]# Remaining itemsifbatch:ifstart_time:elapsed=time.time()-start_timesleep_time=max(0,1.0-elapsed)ifsleep_time>0:time.sleep(sleep_time)forresultinfunc(cls,batch,*args,**kwargs):yieldresultreturnwrapperreturndecorator