Skip to main content

Parse numpy style advanced indexing notation from string.

Project description

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sliceparser-0.9.1b0.tar.gz (3.1 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page