I need a data structure which can handle the following:
date_from (datetime)
date_to (datetime)
value (float)
…and I need to be able to ‘query’ this data structure based on a datetime (e.g. in pseudocode: SELECT * FROM data_structure WHERE a_datetime >= date_from AND a_datetime <= date_to;).
If there isn’t a result from this ‘query’, I would need to be able to insert a new value into the data structure.
What’s the best way of doing this? (I’m a bit stuck at the moment)
Have a look at this SortedCollection Recipe. It uses the bisect module and lets you created a keyed collection. E.g.:
May help you create your cache of time period information, the bisect approach should let you access your date-keyed information efficiently.