SliceMap API
slicemap.SliceMap(include: str = 'start', raise_missing: bool = False)
SliceMap is like dict that allows setting values for whole slices of keys.
It is efficient, having O(log(n)) insertion and querying time complexity. Under the hood, it uses SortedList and bisect search to find the correct place for a key to insert.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
include |
str
|
Either "start" or "end". If "start", the key on the threshold between two slices will belong to the second slice. If "end" it will belong to the first slice. |
'start'
|
raise_missing |
bool
|
If True, accessing a key that was not set will raise KeyError. If False, accessing a key that was not set will return None. |
False
|
copy() -> 'SliceMap'
Returns a deepcopy of itself.
export() -> list[Slice]
Export SliceMap as list of tuples.
This allows using SliceMap's final slices in other parts of your program.
__setitem__(slice_key: slice, value: Any) -> None
Add a new slice to SliceMap. All values in slice key will map to the value.
When adding new slice, a binary search will take place. This operation will
have O(log(n)) time complexity.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
slice_key |
slice
|
Slice of numerical values. If already exists in SliceMap, overlapping values will be overwritten. If, as a result of adding new slice, an existing slice will be 100% covered, it'll be removed. |
required |
value |
Any
|
Any python object can be a value. |
required |
__getitem__(key: SupportsFloat | slice) -> Any
Check the value under the given key.
If there's none and raise_missing was set to False during SliceMap
initialization, None will be returned.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key |
SupportsFloat | slice
|
A numerical key value. |
required |
Returns:
| Type | Description |
|---|---|
Any
|
Value of the key or None (if key is not present in SliceMap). |
Raises:
| Type | Description |
|---|---|
KeyError
|
If |
get_slice_at(key: SupportsFloat) -> Slice
Check the slice at the given key.
__len__() -> int
Return the number of slices in SliceMap.
If a slice becomes redundant because other slices are covering it 100%, it is removed from the SliceMap, and length might decrease.
Returns:
| Type | Description |
|---|---|
int
|
The number of slices in SliceMap. |
plot() -> None
If values are numerical and matplotlib is installed: plots SliceMap.