Skip to main content

Slice a list of sliceables (1 indexed, start and end index both are inclusive)

Project description

Build Status

This package provides Python module to slice a list of sliceables (1 indexed, both start and end index are inclusive). Helps to slice file content line by line or column by column or a combination of both.

Install

Install from Pypi:

pip install rcslice

Usage

from rcslice import RowSlice

rs = RowSlice()

list_of_sliceables = rs.slice_list_of_sliceables(list_of_sliceables, slice_string)

Slicing syntax

Below, r is the row number (inclusive, 1 indexed), and c is the column number (inclusive, 1 indexed)

r.c-r.c
r.c-r.c,r.c-r.c,...
r-r         [not specifying c means the last c (always)]
.c-.c       [not specifying both r means slice on every row for the columns]
r.c-.c      [not specifying r means the last row when another r is specified]
.c-r        [last row.c to r'th row, reversion]
r           [only r'th row]

Reversion, row wise or column wise or a mix of two are allowed.

For multiple slice syntax r.c-r.c,r.c-r.c,..., a separator will be inserted between each slice. You can set the separator by passing it during class object instantiation:

rs = RowSlice(['I am a separator'])

This separator is essentially a list of sliceables.

Examples

An example of slicing a file content read by readlines():

import os
from rcslice import RowSlice

def get_file_lines(filename):
    content = []
    try:
        with open(os.path.join(os.path.dirname(__file__), filename), 'r') as f:
            content = f.readlines()
    except OSError as e:
        raise
    return content


rs = RowSlice(['','']) # ['',''] will add 2 new lines for multi slice syntax (e.g 1-2,3-4,...)

list_of_lines = get_file_lines('test.txt')

print("before: ", list_of_lines)

list_of_lines = rs.slice_list_of_sliceables(list_of_lines, '1-2,1.2-4.5,3.4-1.3,.4-.9')

print("after: ", list_of_lines)

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

rcslice-1.0.1.tar.gz (4.0 kB view hashes)

Uploaded Source

Built Distribution

rcslice-1.0.1-py3-none-any.whl (4.6 kB view hashes)

Uploaded Python 3

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