Skip to main content

slice-match

Simple class, that simplify usage of slices in __getitem__.

Available on PyPi: https://pypi.org/project/slice-match/

Installation

Install with pip:

pip install -U slice-match

Requirements

Python 3.10 and newer

Description

It's just a little trick, that allows you to do:

class custom_indexed:
    def __getitem__(self, index):  
        match index:    
            case int(s):      
                print('this is "me[0]"-like expression')        
            case Slice(int(s)):
                # note, this also matches "me[0:<Any>:<Any>]"-like
                print('this is "me[0:]"-like expression')        
            case Slice(int(start), int(stop)):      
                # note, this also matches "me[0:5:<Any>]"-like
                print('this is "me[0:5]"-like expression')        
            case Slice(int(start), int(stop), int(stride)):      
                print('this is "me[0:5:2]"-like expression')        
            case Slice(start, None, stride):      
                print('this is "me[<Any>::<Any>]"-like expression')
            case Slice(start=12):
                print('this is "me[12:<Any>:<Any>]"-like expression')

Performance drop

Using Slice cause ~2x performance drop, compared with using slice.

Simple benchmark notebook can be found here: https://github.com/SaelKimberly/slice-match/blob/main/slice-match.ipynb

So, readability improved (over 9000% ;) ), but performance slightly drops.

Metadata

Release files for slice-match 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distribution (wheel)

Table of built distributions (wheels) for slice-match 0.2.0
File Interpreter ABI Platform
slice_match-0.2.0-py3-none-any.whl Python 3 none any Details

Release files / slice_match-0.2.0-py3-none-any.whl

Download URL slice_match-0.2.0-py3-none-any.whl
Size 3.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3a1cdfc99701cad24d86d4a71e5ba5caefefe851b0bf728fc4dae93776e6a38e
BLAKE2b-256 checksum
How to use checksums
da8cdc05def34ffbf24d40ec3c21878607288b6a8cc48867ff5f548160dce465
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.10.7

Release history Release notifications | RSS feed

This release

0.2.0 This release

1 release file

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page