Package: agentloop
A simple, lightweight loop for your agent. Start/stop, step-through, and more.Quickstartfromagentloopimportstart,stopdefstep_one(next_output,loop_data):print("step_one")returnnext_outputdefstep_two(next_output,loop_data):print("step_two")returnnext_output# Run the looploop_data=start(steps=[step_one,step_two])# Pause the looppause(loop_data)# Unpause the loopunpause(loop_data)# Stop the loopstop(loop_data)InstallationpipinstallagentloopUsageThis package provides a set of functions to perform a stepped or continuous loop of operations in a separate thread. This can be used for various purposes like running a continuous process that can be controlled from the outside, a debugging tool for a cycle of operations, etc.StepsEach step must take in input from the last step and return output for the next step. The first step will receive None as input, and this will need to be handled. You can either start with an initialization step that returns the initial input, or you can check for None in the first step and return the initial input if it is None.Example steps:defstep_one(next_output,loop_data):# next output None first run, then received from step_twoprint("step_one")returnnext_output# next output sent to step_twodefstep_two(next_output,loop_data):# next output received from step_oneprint("step_two")returnnext_output# next output sent to step_onesteps=[step_one,step_two]Functionstartstart(steps,stepped=False,step_interval=0.0)DescriptionStarts the main loop in a separate thread. This loop will run the steps given, in a continuous or stepped manner.Parameterssteps: a list of functions that should be executed in the loop. Each function should accept a single argument and return a single value which will be passed as an argument to the next function. The first function will receiveNoneas an argument.paused(optional): a boolean value that determines whether the loop should run in paused step mode or not. Defaults toFalse.step_interval(optional): a float value that determines the time interval between steps in seconds. Defaults to0.0.ReturnsA dictionary containingstop_event: an instance ofthreading.Eventthat's used to control stopping of the loop.step_event: an instance ofthreading.Eventthat's used to control stepping.started_event: an instance ofthreading.Eventthat's set when the loop starts running.thread: an instance ofthreading.Threadthat's running the main loop.Functionstopstop(loop_data)DescriptionHandles stopping of the loop.Parametersloop_data: a dictionary containing thestop_eventandthreadwhich is returned by thestartfunction.ReturnsNoneSure, here are the updated sections for thepauseandunpausefunctions in your README file.Functionpausepause(loop_data)DescriptionPauses the loop. When paused, the loop will not execute the next step until it's either stepped using thestepfunction or unpaused using theunpausefunction.Parametersloop_data: a dictionary containing thepause_eventwhich is returned by thestartfunction.ReturnsNoneFunctionunpauseunpause(loop_data)DescriptionResumes the loop after it has been paused with thepausefunction. If the loop is not paused, calling this function has no effect.Parametersloop_data: a dictionary containing thepause_eventwhich is returned by thestartfunction.ReturnsNoneFunctionstepstep(loop_data)DescriptionPerforms a single step in the loop.Parametersloop_data: a dictionary containing thestep_eventwhich is returned by thestartfunction.ReturnsNoneFunctionlooploop(steps,paused=False,loop_data=None,step_interval=0.0)DescriptionRuns the step array in a loop until stopped.Parameterssteps: a list of functions that should be executed in the loop. Each function should accept a single argument and return a single value which will be passed as an argument to the next function. The first function will receiveNoneas an argument.paused(optional): a boolean value that determines whether the loop should run in paused / stepped mode or not. Defaults toFalse.loop_data(optional): a dictionary containingstop_eventandstep_eventinstances. If not provided, new events will be created.step_interval(optional): a float value that determines the time interval between steps in seconds. Defaults to0.0.ReturnsNoneFunctionuse_keyboarduse_keyboard(loop_data,input_key=keyboard.Key.space)DescriptionCreates a keyboard listener and attaches it to the provided loop data object. This listener listens for a specified key press, and when detected, steps the loop (sets the 'step_event').Parametersloop_data: A loop data object, which is typically created by thestartfunction.input_key(optional): The keyboard key that the listener will react to. Defaults tokeyboard.Key.space.ReturnsReturns the updated loop dictionary with the newly created keyboard listener added to it.
Note: Pass the updated dictionary to the stop function to also stop the keyboard listenerDefaults to SpacebarExampleloop_data={"stop_event":threading.Event(),"step_event":threading.Event(),"started_event":threading.Event(),"thread":None,}updated_loop_dict=use_keyboard(loop_data,input_key)Testingpytest test.pyPublishingbashpublish.sh--version=<version>--username=<pypi_username>--password=<pypi_password>Contributions WelcomeIf you like this library and want to contribute in any way, please feel free to submit a PR and I will review it. Please note that the goal here is simplicity and accesibility, using common language and few dependencies.