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

Uploaded Source

Built Distribution

slice_a_portion-0.1-py3-none-any.whl (7.1 kB view details)

Uploaded Python 3

File details

Details for the file slice-a-portion-0.1.tar.gz.

File metadata

  • Download URL: slice-a-portion-0.1.tar.gz
  • Upload date:
  • Size: 6.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/4.0.2 CPython/3.11.7

File hashes

Hashes for slice-a-portion-0.1.tar.gz
Algorithm Hash digest
SHA256 65f36805cae3490045a64b752202926ca2273c4da3938a60478e093df215753d
MD5 99180ffb81234245bd0b9b72ae4677cd
BLAKE2b-256 b342929b502dcc8f6a3921d7b50e87921105a45577dd25ebb8038d491b7d00ee

See more details on using hashes here.

File details

Details for the file slice_a_portion-0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for slice_a_portion-0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 417a20c9e20404af7566c5a7a2643cf76a7fcb8bbd1f6c9b0f0d1e785090ebe6
MD5 3851d56a08be7539342d94045e0637fa
BLAKE2b-256 4c961f65608969916614daab814e8f9fdbcd933264b2ad697cbea015511e0bcd

See more details on using hashes here.

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