Skip to main content

get the i/n slice of a sequence

Project description

slice-a-portion

PyPI Tests Changelog License

get the i/n slice of a sequence

Installation

Install this library using pip:

pip install slice-a-portion

Usage

There are two functions

>>> from slice_a_portion import slice_by_coefficients, slice_by_portion

   slice_by_coefficients(sequence: Sequence, start: Optional[float] = None, end: Optional[float] = None)
        Return the slice between the coefficients `start` and `end`
        between 0 and 1.
        
        Corner elements may be repeated in consecutive slices.
        
        >>> slice_by_coefficients(abc", 0, 0.333)
        'a'
        >>> slice_by_coefficients("abc", 0.666, 1)
        'c'
        >>> slice_by_coefficients("abcd", 0, 0.499)
        'ab'
        >>> slice_by_coefficients("abcd", None, 0.499)
        'ab'
        >>> slice_by_coefficients("abcd", 0.5, 1)
        'cd'
        >>> slice_by_coefficients("abcd", 0.5)    # until the end
        'cd'
   slice_by_fraction(sequence: Sequence, i: int, n: int)
       Split a sequence in `n` slices and then return the i-th (1-indexed).
       The last slice will be longer if the sequence can't be splitted even-sized or
       n is greater than the sequence's size.
       
       >>> from slice_a_portion import slice_by_fraction
       >>> slice_by_fraction("abcdefghi", 1, 2)
       'abcd'
       >>> slice_by_fraction("abcdefghi", 2, 2)
       'efghi'
       >>> slice_by_fraction("abcdefghi", 1, 3)
       'abc'
       >>> slice_by_fraction("abcdefghi", 2, 3)
       'def'
       >>> slice_by_fraction("abcdefghi", 3, 3)
       'ghi'        
       >>>

Development

To contribute to this library, first checkout the code. Then create a new virtual environment:

cd slice-a-portion
python -m venv venv
source venv/bin/activate

Now install the dependencies and test dependencies:

pip install -e '.[test]'

To run the tests:

pytest

Project details


Release history Release notifications | RSS feed

This version

0.1

Download files

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

Source Distribution

slice-a-portion-0.1.tar.gz (6.9 kB view hashes)

Uploaded Source

Built Distribution

slice_a_portion-0.1-py3-none-any.whl (7.1 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