Zero-copy, composable slice views for Python sequences
Project description
sliceview
Zero-copy, composable slice views for Python sequences.
sliceview lets you work with windows into lists, tuples, strings, or any
Sequence without copying the underlying data. It is the proof-of-concept
library accompanying the Python discussion on
Slice Views for Python Sequences.
from sliceview import sliceview
data = list(range(1_000_000))
# O(1) — no copy
window = sliceview(data)[50_000:60_000]
# Compose slices — still O(1), still no copy
every_third = sliceview(data)[::3][100:200]
# In-place windowed update
sv = sliceview(data)
sv[0:5] = [10, 20, 30, 40, 50]
# Sliding window without creating new objects
view = sliceview(data, 0, 1000)
for _ in range(10):
process(view)
view.advance(1000)
Why?
In Python today, seq[a:b] copies the data. For large pipelines — text
processing, audio, genomics, sorting algorithms — those copies dominate
both time and memory. memoryview solves this for bytes-like objects;
sliceview targets generic sequences of Python objects.
Inspired by Go slices, NumPy views, and memoryview.
Installation
pip install sliceview
Features
| Feature | Details |
|---|---|
| Zero-copy slicing | sv[a:b:c] returns a new sliceview in O(1) |
| Composable | sv[2:][::3][5:10] chains correctly with no intermediate copies |
| Live view | Mutations to the base are immediately visible through the view |
| Write-through | sv[i] = x and sv[a:b] = iterable forward to the base |
| Sliding window | sv.advance(n) shifts the window in-place — no new object |
| Any sequence | Works with list, tuple, str, array, or your own type |
API
sliceview(base, start=None, stop=None, step=None)
Create a view over base. start may be a slice object.
sv = sliceview(my_list) # full view
sv = sliceview(my_list, 10, 20) # [10:20]
sv = sliceview(my_list, slice(10, 20, 2)) # [10:20:2]
Indexing and slicing
sv[i] # element access — maps to base[start + i*step]
sv[a:b:c] # returns a new sliceview (O(1))
sv[i] = x # write-through to the base
sv[a:b] = it # slice assignment (delegates to base)
sv.advance(n) -> self
Shift the view's window forward by n index positions (negative to retreat).
Returns self for chaining. Useful for sliding-window algorithms:
view = sliceview(samples, 0, window_size)
while True:
result = process(view)
if view.advance(window_size)._start >= len(samples):
break
sv.tolist() / sv.copy()
Materialise the view as a new list (explicit copy).
sv.base
The underlying sequence the view points into.
Semantics
len(sv)reflects the current base length, so appending to the base is immediately visible.sv[:]returns a newsliceviewpointing at the same base — O(1).- Hashing:
sliceviewis intentionally unhashable. - Equality: compares element-wise to any
Sequence. - Immutable bases:
sv[i] = xraisesTypeErrorif the base does not support__setitem__.
Design notes and open questions
This library implements the core proposal from the Python discussion. Deliberately left out to keep the scope focused:
view()builtin shortcut (consensus in the thread was it's unnecessary)__sliceview__dunder (motivation unclear until adoption data exists)- Multidimensional views (NumPy is the right tool for that)
- ABC / Protocol additions to
collections.abc
Feedback welcome — please open an issue or join the discussion thread.
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file sliceview-0.1.1.tar.gz.
File metadata
- Download URL: sliceview-0.1.1.tar.gz
- Upload date:
- Size: 7.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0d717a05f5005c43c0244e7eff8cfe362a30847b6cf3e3d58cabedd60d660f54
|
|
| MD5 |
e86a9eb4cb75732421d8463432317f79
|
|
| BLAKE2b-256 |
bc71ba31141ca87f097da7ab8c4202adcbe9ac1905af0d9809b1ee5caccde13d
|
File details
Details for the file sliceview-0.1.1-py3-none-any.whl.
File metadata
- Download URL: sliceview-0.1.1-py3-none-any.whl
- Upload date:
- Size: 6.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4cd12ada8bfbcdc0f85d5590df6edd86ce409d7cb4606cd663fe2ffcbf638a55
|
|
| MD5 |
7ea0d50fe89c27f267dc80a2febf7fc1
|
|
| BLAKE2b-256 |
2e784e84e640139ccf18a1d919261075fbe2ae02a075761cecce6aadc53129c0
|