MutableIntervalDict¶
-
class
part.MutableIntervalDict(iterable=None, default=None, operator=None, strict=True)¶ Mutable Interval Dictionary class.
The
MutableIntervalDictclass (which inherits from theIntervalDictclass) is designed to hold mutable dict of disjoint sorted intervals.Note
Les \(n\) (or \(n_0\)) the number of intervals of the self variable and \(m\) the number of intervals in the other variable. Let \(n_1, ... n_k\) the number of intervals for methods with multiple arguments.
The complexity in time of methods is:
Methods
Average case
\(O(n)\)
\(O(n)\)
\(O(m\log(n+m))\)
\(O((\sum_{i=1}^kn_i)\log(\sum_{i=0}^kn_i))\)
\(O(1)\)
-
__init__(iterable=None, default=None, operator=None, strict=True)¶ Initialize a
MutableIntervalDictinstance.- Parameters
iterable (
Iterable) – An optional iterable that can be converted to a dictionary of ( interval, value).- Keyword Arguments
Note
If
operatoris a commutative and associative law onV, the complexity in time is much faster ifstrictis set toFalse.Examples
>>> from part import MutableIntervalDict >>> a = MutableIntervalDict[int, set]( ... operator=lambda x, y: x | y, ... strict=False ... ) >>> a.update({(1, 10): {1}}) >>> print(a) {'[1;10)': {1}} >>> a.update({(5, 20): {2}}) >>> print(a) {'[1;5)': {1}, '[5;10)': {1, 2}, '[10;20)': {2}} >>> a.update({(10, 30): {1}}) >>> print(a) {'[1;5)': {1}, '[5;10)': {1, 2}, '[10;20)': {1, 2}, '[20;30)': {1}} >>> print(a.compress()) {'[1;5)': {1}, '[5;20)': {1, 2}, '[20;30)': {1}}
-
__getitem__(key)¶ Return a value using either a slice or an interval value.
-
__setitem__(key, value)¶ Set a value using either a slice or an interval value.
- Parameters
key (Union[IntervalValue, slice]) – The interval requested.
- Raises
KeyError – If the key is out of range.
Examples
>>> from part import MutableIntervalDict >>> a = MutableIntervalDict[int, int]( ... {(10, 15): 1, (20, 25): 2, (30, 35): 3} ... ) >>> print(a) {'[10;15)': 1, '[20;25)': 2, '[30;35)': 3} >>> a[12] = 4 >>> print(a) {'[10;12)': 1, '[12;12]': 4, '(12;15)': 1, '[20;25)': 2, '[30;35)': 3} >>> a[13:31] = 5 >>> print(a) {'[10;12)': 1, '[12;12]': 4, '(12;13)': 1, '[13;31)': 5, '[31;35)': 3} >>> a[:] = 0 >>> print(a) {'(-inf;+inf)': 0}
-
__delitem__(key)¶ Delete a value using either a slice or an interval value.
- Parameters
key (Union[IntervalValue, slice]) – The interval requested.
- Raises
KeyError – If the key is out of range.
Examples
>>> from part import MutableIntervalDict >>> a = MutableIntervalDict[int, int]( ... {(10, 15): 1, (20, 25): 2, (30, 35): 3} ... ) >>> print(a) {'[10;15)': 1, '[20;25)': 2, '[30;35)': 3} >>> del a[12] >>> print(a) {'[10;12)': 1, '(12;15)': 1, '[20;25)': 2, '[30;35)': 3} >>> del a[13:31] >>> print(a) {'[10;12)': 1, '(12;13)': 1, '[31;35)': 3} >>> del a[:] >>> print(a) {}
-
__or__(other)¶ Construct a new dictionary using self and the other.
- Parameters
other (
IntervalDict) – Another interval dict.- Returns
The new
IntervalDict.- Return type
Examples
>>> from part import MutableIntervalDict >>> a = MutableIntervalDict[int, int]( ... {(10, 15): 1, (20, 25): 2, (30, 35): 3}, ... operator=lambda x, y: x + y, ... strict=False ... ) >>> print(a | FrozenIntervalDict[int, int]({(15, 22): 4})) {'[10;15)': 1, '[15;20)': 4, '[20;22)': 6, '[22;25)': 2, '[30;35)': 3}
-
__ior__(other)¶ Update self with the other.
- Parameters
other (
IntervalDict) – Another interval dict.- Returns
The updated
MutableIntervalDict.- Return type
Examples
>>> from part import MutableIntervalDict >>> a = MutableIntervalDict[int, int]( ... operator=lambda x, y: x + y, ... strict=False ... ) >>> a |= MutableIntervalDict[int, int]({(1, 10): 1}) >>> print(a) {'[1;10)': 1} >>> a |= MutableIntervalDict[int, int]({(5, 20): 2}) >>> print(a) {'[1;5)': 1, '[5;10)': 3, '[10;20)': 2} >>> a |= MutableIntervalDict[int, int]({(10, 30): 3}) >>> print(a) {'[1;5)': 1, '[5;10)': 3, '[10;20)': 5, '[20;30)': 3}
-
update(*args)¶ Update the dict.
- Parameters
*args (
Iterable) – An iterable ofIntervalDictor valid iterable for an interval dictionary creation.- Raises
TypeError – if an argument is not iterable.
Note
If the parameter
strictused in the constructor isFalse, the complexity is in \(O(n\,log(n)\,k\,\lambda)\) where:\(n\) is the length of
*args;\(k\) is the number of output intervals;
\(\lambda\) is the the cost of the
operatorparameter used in the constructor.
Examples
>>> from part import MutableIntervalDict >>> from operator import add >>> a = MutableIntervalDict[int, int]( ... operator=add, ... default=lambda: 0, ... ) >>> a.update({(1, 10): 1}) >>> print(a) {'[1;10)': 1} >>> a.update( ... FrozenIntervalDict[int, int]({(5, 20): 2}), ... FrozenIntervalDict[int, int]({(10, 30): 3}) ... ) >>> print(a) {'[1;5)': 1, '[5;10)': 3, '[10;20)': 5, '[20;30)': 3} >>> a = MutableIntervalDict[int, set]( ... operator=lambda x, y: x | y, ... strict=False ... ) >>> a.update({(1, 10): {1}}) >>> print(a) {'[1;10)': {1}} >>> a.update( ... FrozenIntervalDict[int, set]({(5, 20): {2}}), ... FrozenIntervalDict[int, set]({(10, 30): {3}}) ... ) >>> print(a) {'[1;5)': {1}, '[5;10)': {1, 2}, '[10;20)': {2, 3}, '[20;30)': {3}}
-
clear()¶ Remove all items from self (same as del self[:]).
-