sliceparser

Parse numpy style advanced indexing notation from string.


License
MIT
Install
pip install sliceparser==0.9.1b0

Documentation

sliceparser

Introduction

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.

Install

pip install sliceparser
# or pip3 install sliceparser

Usage

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[1]

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])

etc.