Slices

exception deker_tools.slices.SliceConversionError

Bases: Exception

If something goes wrong during slice conversion.

class deker_tools.slices.slice_converter

Bases: object

Converts slices to string and vice versa.

Standard index expressions
>>> slice_converter[5]
'[5]'
>>> slice_converter[1, :]
'[1, :]'
>>> slice_converter["[1, :]"]
(1, slice(None, None, None))
Index expressions with datetime and floats
>>> slice_converter[datetime.datetime(2023,1,1):datetime.datetime(2023,2,1), 0.1:0.9:0.05]
'[`2023-01-01T00:00:00`:`2023-02-01T00:00:00`, 0.1:0.9:0.05]'
>>> slice_converter["[`2023-01-01T00:00:00`:`2023-02-01T00:00:00`, 0.1:0.9:0.05]"]
(slice('2023-01-01T00:00:00', '2023-02-01T00:00:00', None), slice(0.1, 0.9, 0.05))
Timezone and microseconds are also acceptable
>>> slice_converter[datetime.datetime(2023,1,1,0,0,0,123456)]
'[`2023-01-01T00:00:00.123456`]'
>>> slice_converter['[`2023-01-01T00:00:00-03:30`]']
'2023-01-01T00:00:00-03:30'
>>> slice_converter['[`2023-01-01T00:00:00.123456+05:00`]']
'2023-01-01T00:00:00.123456+05:00'
Index expressions with strings
>>> slice_converter["1a":"10b":"5c"]
'[`1a`:`10b`:`5c`]'
>>> slice_converter['[1a:10b:5c]']
slice('1a', '10b', '5c')
>>> slice_converter["1":"10":"5.2"]
'[`1`:`10`:`5.2`]'
>>> slice_converter["1", "10", "5.2"]
'[`1`, `10`, `5.2`]'
>>> slice_converter['[`1`, `10`, `5.2`]']
('1', '10', '5.2')
>>> slice_converter['[`1`:`10`:`5.2`]']
slice('1', '10', '5.2')
deker_tools.slices.create_shape_from_slice(array_shape, index_exp)

Calculate shape of a subset from the index expression passed to __getitem__.

Parameters
  • array_shape (Tuple[int, ...]) – shape of the parent array

  • index_exp (Union[IndexExpression, slice, ellipsis, int, Tuple[Optional[Union[slice, int, ellipsis]], ...]]) – index expression passed to the array __getitem__ method

Return type

Tuple[int, …]

deker_tools.slices.match_slice_size(dim_len, slice_=None)

Convert slice into a sequence and get its length.

Parameters
  • dim_len (int) – length of the corresponding Dimension

  • slice – slice to be converted

  • slice_ (Optional[slice]) –

Return type

Tuple[int, int, int]