I am trying to make a simple time-based script where the user inputs:
- Time after starting the script to call an object, called
dt_call
- Generated by
time.perf_counter()
(aka it's afloat
)
- Generated by
- Object to call at that time
Is there a Python library that has a key-value store that meets the following conditions?
- Keys are
float
- Values are
object
- Keys are sorted
More Information
This will be part of a scheduler, where every so often the scheduler:
- Gets the current time since starting the script (sec), called
dt
- Maybe call the object, depending on if it's call time has passed
- Looks to see
if dT >= dt_call
- If yes: check if the associated object has been called. If uncalled, then call the object.
- If no: do nothing
- Looks to see
Current Best Idea
Currently, my best idea is based on this: Sort a list of tuples by 2nd item (integer value)
Before starting the script:
- Store
dt_call
+ object pairs in atuple
- Store all pairs in a list
- Sort using this: https://stackoverflow.com/a/44852626/11163122
# Keys are `dt_call`
list_.sort(key=lambda x:x[0])
list_ # [(5.6, obj0), (5.9, obj1), (8.7, obj2)]
After starting the script:
- Get
index
usingbisect.bisect
- See if object at
index - 1
was called. If not, call it.
# Start
start_time = time.perf_counter()
# Some time has passed
dt = time.perf_counter() - start_time
# Step 1
index = bisect.bisect([x[0] for x in list_], dt)
# Step 2
fetched_obj = list_[index - 1][1]
if fetched_obj.is_not_called():
fetched_obj()
Is there a data structure I can use to accomplish this in a more direct manner (all in one)?
This idea combines multiple data structures to get the job done.