Skip to main content

Add Mutating Slices to Python

Project description

Slicey


Add Mutating Slices to Python

Mutating slices (or "reference slices") deal with a subset of a list as if it was its own list. This contrasts python's native "value slicing" where slicing a list provides a new list.

Let's Take a Look


Starting with python's normal slicing

base = [0, 1, 2, 3, 4]
slc = base[0:]  # take a slice of the whole list
# now let's check each element is the same in each list
assert all(base[i] == slc[i] for i in range(len(base)))
# if we change the slice (slc) the base won't change
slc[2] = -1
# now they should be different
assert base[2] != slc[2]

However, with slicey you can opt into slices that change the base.

from slicey import Slice

base = [0, 1, 2, 3, 4]
slc = Slice(base)[0:] # slicey slicing syntax
assert all(base[i] == slc[i] for i in range(len(base)))
slc[2] = -1
assert base[2] == slc[2]

Excluding that slicey slices mutate the base list, they can be used like a normal slice!

from slicey import Slice

base = [7, 3, 2, 5, 4]

slc = Slice(base)[1:-1]  # grab from 1 through 3 ([3, 2, 5])
assert [*slc] == [3, 2, 5]

slc[2] = 1
assert base[3] == 1

slc.sort()
assert base == [7, 1, 2, 3, 4]

Why


Don't get me wrong, I love python! But sometimes... you just want a pointer.

The initial inspiration for this project was implementing merge sort, in a clear manner. Often merge sort is written with a variety of indices (i, j, k, etc); I can't speak for other but I can't make heads or tails of it. In languages that expose raw pointers, like c, one can mutate subsections of arrays by referencing an item and passing a smaller length to some function. In short, I wanted to write a merge sort I sould read.

Some Nitty Gritty


Slicing Slicey Slices ... ( ☉︵ ಠ )?

slicey is always opt-in. This means when you slice a slicey slice (an instance of Slice) you'll get an object of whatever slicing the base object would be.

In the likely case that was non-sense, let's just take a look

from slicey import Slice

base = [*range(10)]  # a native list
slc = Slice(base)[1:10]  # a slicey slice
natv_sublc = slc[1:-1]  # a native list equal to base[2:9]
assert type(natv_sublc) == type(base)
assert natv_sublc == base[2:9]

Sometimes it make sense to have a refence slice of a reference slice.

# continuing from above

slcy_subslc = slc.slice[1:-1]  # an instance of Slice
assert type(slcy_subslc) is Slice
assert [*slcy_subslc] == base[2:9]
# for the generic programmers among us Slices can be subsliced like a list
gnrl_slc = Slice(slc)[1:-1]

Transparent Slice Collapsing

Some steps have been taken to avoid indirection when sub-slicing instances of Slice. Taking a sub-slice of a Slice is always equivalent to take a slice out of the base object

readme todo

  • changing base list size not permitted, b/c notifications
  • excluding .pop() and other length mutating methods
  • typing and Sliceable

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

slicey-0.1.0.tar.gz (5.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

slicey-0.1.0-py3-none-any.whl (6.4 kB view details)

Uploaded Python 3

File details

Details for the file slicey-0.1.0.tar.gz.

File metadata

  • Download URL: slicey-0.1.0.tar.gz
  • Upload date:
  • Size: 5.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.9.4

File hashes

Hashes for slicey-0.1.0.tar.gz
Algorithm Hash digest
SHA256 7adf036045c810f34e7cd0e4562041ac97bacc55996e1de75d8f87d1819082e0
MD5 c9e1bce05c0962764242479f12c4b809
BLAKE2b-256 3cec9e9dc454009404238b3d47fba72c13062ae41dbc12a35dc88fad75d4795f

See more details on using hashes here.

File details

Details for the file slicey-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: slicey-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 6.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.9.4

File hashes

Hashes for slicey-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 41fd0f185ee971fcf0d302aec80630083128263678edc97657dc81b6cd1ef2ae
MD5 4af179550f532de875b47523fbf0f4e9
BLAKE2b-256 ae7db91a936ca76485e5c8cb1c2573b80cc4d87d67410967e0803271481f290b

See more details on using hashes here.

Supported by

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