Parse numpy style advanced indexing notation from string.
As per this question, creating from string slice object, or even advanced indexing tuple, is a common requirement. However, there exists few robust and safe solution, if at all, to solve the problem. Therefore I attempt to solve it and expose programmatic interface via PyPI. I also put my answer to the question above.
This repo is adapted from my Gist.
pip install sliceparser # or pip3 install sliceparser
import sliceparser a = [1,2,3,4] assert a[sliceparser.parse_slice('2:')] == a[2:] assert a[sliceparser.parse_slice('::2')] == a[::2] assert a[sliceparser.parse_slice('1')] == a import numpy as np A = np.eye(3) assert np.array_equal(A[sliceparser.parse_slice('0, 1:')], A[0,1:]) assert np.array_equal(A[sliceparser.parse_slice('..., 2')], A[..., 2])
Release history Release notifications | RSS feed
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
sliceparser-0.9.1b0.tar.gz (3.1 kB view hashes)