-1

I am trying to make a simple time-based script where the user inputs:

  1. Time after starting the script to call an object, called dt_call
    • Generated by time.perf_counter() (aka it's a float)
  2. Object to call at that time

Is there a Python library that has a key-value store that meets the following conditions?

  1. Keys are float
  2. Values are object
  3. Keys are sorted

More Information

This will be part of a scheduler, where every so often the scheduler:

  1. Gets the current time since starting the script (sec), called dt
  2. Maybe call the object, depending on if it's call time has passed
    1. Looks to see if dT >= dt_call
    2. If yes: check if the associated object has been called. If uncalled, then call the object.
    3. If no: do nothing

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:

  1. Store dt_call + object pairs in a tuple
  2. Store all pairs in a list
  3. 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:

  1. Get index using bisect.bisect
  2. 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.

martineau
  • 119,623
  • 25
  • 170
  • 301
Intrastellar Explorer
  • 3,005
  • 9
  • 52
  • 119

1 Answers1

1

Your mention needing a data structure which allows:

Keys as floats
Values are objects
Sorting of Keys

Would suggest module
Heapq

Great for organizing items, which are list of tuples

  • tuple order controlled by elements in the tuple

  • objects can be values in the tuple

  • heap order is updated as items are added or deleted

  • Takes O(log(n)) in time to add or remove an item

Timer

from time import perf_counter 

To use heap

from heapq import heappush, heappop

heap = []   # any heap

Items are tuples

# delay from current time to perform action on object
scheduled_time = perf_counter() + delay
item = (scheduled_time , obji)  # i-th object

To add object to heap

heappush(heap, item)

Assuming heap as a list of items, to one is scheduled we have:

We process objects in the heap using the following loop

While True:

    # heap[0] is almost the smallest for a heap so
    scheduled_time, obj = heap[0]

    if perf_counter() >= schedule_time:
        # pop earliest off heap and do something
        heappop(heap)
        do_something(obj)

The heap automatically reorders the item with the earliest time upon heappop (remove and item) or heappush (add an item)

DarrylG
  • 16,732
  • 2
  • 17
  • 23