Package: loopimer
loopimerPackage for time-controlled consecutive execution of a function using threading. Allows control over the time between each function execution and the introduction of time delays/pauses. Furthermore, the package utilizes Queues and slicing to provide the user with the ability to control the execution of the looping function using queued slices. The package was originally developed for working with rate limited APIs and only uses python standard libraries.RequirementsPython Standard LibrariesthreadingtimedatetimequeuemathsysosInstallationpip install loopimerpip install git+https://github.com/rouzbeh-afrasiabi/loopimer.gitDecorators:loopimerExamplesImportingfromloopimerimport*Usage:In the simplest usage case, you pass a value in seconds to the decorator through the 'every' variable. This will modify the
function that is declared after the decorator to execute every x seconds as an indefinite loop when called.#cauion: infinite loop@loopimer(every=5)deftest(loop,):print('loopimer')test()Setting 'every' to zero will remove any delays between function executions and will convert the loop into a basic loop.#caution: infinite loop@loopimer(every=0)deftest(loop,):print('loopimer')test()The first variable passed to the target function allows you to control the loop running in the background (called 'loop' in the examples on this page). You can choose any name for this variable, but it is important that you pass a variable name as a placeholder to the target function when declaring your desired function. The first variable name passed to your target function during declaration will always be reserved for the loop that will be running in the background. Upon calling the target function, this placeholder will be linked to the related thread controlling the loop.The loop starts authomatically when the function is called and will run indefinity. You can control the loop and how the function is executed using the placeholder variable. This is currently only possible when declaring the target function.Stopping the loop@loopimer(every=5)deftest(loop,):print('loopimer')loop.kill()test()Please note that when calling the target function we don't pass the placeholder variable name to the function.Stopping the loop using loop counterIt is important to note that the counter starts at 1 instead of 0.@loopimer(every=1)deftest(loop,):print(loop.counter)if(loop.counter==10):print('loopimer')loop.kill()test()Stopping the loop using loop elapsed time@loopimer(every=1)deftest(loop,):print(loop.elapsed,loop.total_seconds)if(loop.total_seconds>=6):print('loopimer')loop.kill()test()Adding custom variables to the loopThe loop placeholder variable is an open class and you can add new attributes to it. However, it is important to use attribute names that don't cause a naming conflict with names already being used by the class.Attributes can be created inside the function or passed to the decorator instead.fromrandomimportrandrange@loopimer(every=1,i=0)deftest(loop,):print(loop.i)if(loop.i==10):print('loopimer')loop.kill()loop.i+=1test()@loopimer(every=1)deftest(loop,):loop.k=randrange(100)print(loop.k)if(loop.k>80):loop.kill()test()Changing the value of 'every' in the loop functionThe 'every' variable passed through the decorator is available to the loop. You can change the value of this variable using 'loop.every'.@loopimer(every=1)defdo(loop):print(loop.counter)if(loop.counter>10):loop.every=5if(loop.counter>20):loop.kill()do()Using queueA sliceable variable can be passed to the loopimer decorator through the 'target' variable for processing. This sliceable variable is then split into slices of 'n_splits' size. The slices are placed in a queue and can be accessed through the 'sequence' attribute of the loop (loop.squence). This attribute is an instance ofQueue. The loop automatically stops when no items are left in the queue.When a target  is not provided the queue is automatically filled with one item containing a zero. This means that the loop will stop after one cycle if the queue is accessed.@loopimer(every=1)deftest(loop,):print(loop.counter,loop.sequence.get())test()Using queue with target and n_splitstarget=[iforiinrange(0,100,1)]n_splits=10@loopimer(target=target,n_splits=n_splits,every=1)deftest(loop,):print(loop.sequence.get())test()Using queue without providing target and n_splits to decoratorimportqueuenew_queue=queue.Queue()new_target=[1,2,3,4,5,6,7,8,9]foriteminnew_target:new_queue.put(item)@loopimer(every=1)deftest(loop,new_queue):if(loop.counter==1):loop.sequence=new_queueelse:print(loop.counter,loop.sequence.get())test(new_queue=new_queue)importqueuenew_queue=queue.Queue()new_target=[1,2,3,4,5,6,7,8,9]foriteminnew_target:new_queue.put(item)@loopimer(every=1,new_queue=new_queue)deftest(loop,):if(loop.counter==1):loop.sequence=new_queueelse:print(loop.counter,loop.sequence.get())test()importqueue@loopimer(every=1)deftest(loop):if(loop.counter==1):items=[1,2,3,4,5,6]loop.sequence=queue.Queue()foriteminitems:loop.sequence.put(item)else:print(loop.counter,loop.sequence.get())test()Adding your own queue variable to the loopimportqueue@loopimer(every=1)deftest(loop):if(loop.counter==1):items=[1,2,3,4,5,6]loop.my_queue=queue.Queue()foriteminitems:loop.my_queue.put(item)else:print(loop.counter,loop.my_queue.get())if(loop.my_queue.qsize()==0):loop.kill()test()Using time delaysBy Changing the value of the loop's pause attribute you can introduce time delays. This is especially useful when you reach a rate limit when pulling/pushing data from/to an API.target=[iforiinrange(0,100,1)]n_splits=10@loopimer(target=target,n_splits=n_splits,every=1)deftest(loop,):print(loop.sequence.qsize(),loop.sequence.get())if(loop.sequence.qsize()==8):loop.pause=20test()Using queue and jsonimportnumpyasnpimportpandasaspdimportdatetimeasdtimportsimplejsonasjsondf=pd.DataFrame(np.random.randint(0,100,size=(100,4)),columns=list('ABCD'))df['date']=dt.datetime.now()df_json=json.loads(df.to_json(orient='records'))@loopimer(target=df_json,n_splits=10,every=1)deftest(loop,):print(json.dumps(loop.sequence.get()),'\n')test()Without using the loopimer decoratordeftest(loop,):print(loop.counter)if(loop.counter==10):print('loopimer')loop.kill()x=[iforiinrange(0,10000,1)]loop=loopi(target=x,)loop.apply_to(test,)loop.startTimedLoop(every=1)deftest(loop,t):print(loop.counter+t)if(loop.counter==10):print('loopimer')loop.kill()x=[iforiinrange(0,10000,1)]loop=loopi(target=x,)loop.apply_to(test,t=2)loop.startTimedLoop(every=1)