-
-
Notifications
You must be signed in to change notification settings - Fork 33
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
IntervalMultiDict #83
Comments
Here's an example of a potential API. Indexing accepts either an interval, a key, or a tuple (interval, key). Internally, the structure would be a dict of IntervalDict (performance might be an issue, but this new IntervalMultiDict would be mostly proposed for convenience). >>> m = P.IntervalMultiDict()
>>> m[P.closed(8, 12), 'teacher'] = 't1'
>>> m[P.closed(8, 10), 'room'] = 'r1'
>>> m[P.closed(11, 12), 'room'] = 'r2'
>>> m['room'] # Providing a key returns a (new?) IntervalDict instance
IntervalDict({P.closed(8, 10): 'r1', P.closed(11, 12): 'r2'})
>>> m[P.closed(8, 10] # Returns a (likely new) IntervalMultiDict
{'teacher': IntervalDict({P.closed(8,10): 't1'}), 'room': IntervalDict(....)}
>>> m[P.singleton(9)] # If interval is a singleton, returns a classical dict
{'teacher': 't1', 'room': 'r1'}
>>> m[P.singleton(9), 'room'] # Singleton + key return a value
'r1'
>>> m[9, 'room'] # For convenience, if a tuple is passed and the interval is a singleton, its value can be provided directly
'r1'
>>> m[P.singleton(9), ['room', 'teacher']] # A list of keys can be passed
{'room': 'r1', 'teacher': 't1'}
>>> m[P.closed(10, 12)] = { 'room': 'r3', 'course': 'c1'} # Dict can be used for assignment
>>> m[P.singleton(11)]
{'teacher': 't1', 'room': 'r3', 'course': 'c1'} Mutability would only we allowed through indexing (i.e., Since this API is quite distinct from the one of a traditional dictionary, perhaps this structure shouldn't be called "IntervalMultiDict". Using "Dict" in the name implicitly suggests we mimick the API of a |
While convenient, these two examples are "incorrect" in the sense that one would expect the output type to be consistent for all intervals, including singletons: >>> m[P.singleton(9)] # If interval is a singleton, returns a classical dict
{'teacher': 't1', 'room': 'r1'}
>>> m[P.singleton(9), 'room'] # Singleton + key return a value
'r1' In the first example, |
I'll keep this for later... |
Hello,
We currently have an
IntervalDict
structure that enables to map values toInterval
instances. One of the main limitation of this structure is its inability to store more than one value for eachInterval
. The current issue proposes to extentIntervalDict
(or to replace it) with anIntervalMultiDict
structure allowing to store more than one pair key/value with intervals.Let's assume the following scenario where "teachers" need to book "rooms" for timeslot (and a timeslot is an
Interval
instance ofdatetime
or, to make it simpler, of Integers). Currently, one possible option to store these kind of data is as follows:The purpose of the current issue is to make it more "API-friendly" to deal with such use-cases:
Or even:
Among other features, this enables:
In this case, partial views on the
MultiIntervalDict
instances would be returned.The opposite behaviour is also an option:
In that case, the returned value type would be an
IntervalDict
.Obviously, combinations of both examples are expected:
or
The underlying structure would be a dictionary of
IntervalDict
instances. Performance is not expected to be good, but the whole structure would be mostly provided for convenience, not for performance. One main question that remains is how to distinguish between keys that areInterval
instances and keys that are "appropriate dict keys". Is it easy to do a check on the type of the key, but what should be done when a key is, e.g., a numerical or a string value ? (since anInterval
instance can be composed of numerical or string values).Another point to consider is how to handle updates on "partial" objects? For example, if one asks for
d[x][y]
, we need to ensure thatd
is updated ifd[x]
is. Returning anIntervalDict
ford[x]
is an option (sinceIntervalDict
instances are mutable) but probably we need to create a new subclass ofdict
so that any modification tod[i][k]
wherei
is an interval andk
is a "traditional" key would be reflected ond
. Another option, as seen in pandas, would be to go for ad[x, y]
notation (i.e., a tuple has to be provided to make sure that we return an appropriate data structure to keep track of the changes, instead of allowing notations such asd[x][y]
).The text was updated successfully, but these errors were encountered: