Skip to main content

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 new sliceview pointing at the same base — O(1).
  • Hashing: sliceview is intentionally unhashable.
  • Equality: compares element-wise to any Sequence.
  • Immutable bases: sv[i] = x raises TypeError if 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


Download files

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

Source Distribution

sliceview-0.1.0.tar.gz (7.2 kB view details)

Uploaded Source

Built Distribution

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

sliceview-0.1.0-py3-none-any.whl (3.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: sliceview-0.1.0.tar.gz
  • Upload date:
  • Size: 7.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.3

File hashes

Hashes for sliceview-0.1.0.tar.gz
Algorithm Hash digest
SHA256 1c2c0ff5dbd2ad5d38c8a27a4fb9f10fcedec6a6de88ae895d55b271f25bc079
MD5 9628ead14344f38562d366b6e275e139
BLAKE2b-256 07bfe2edf7b690c6cb6a42d9da79d18cb9e251d1dd9daac0e48d504c6b09e954

See more details on using hashes here.

File details

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

File metadata

  • Download URL: sliceview-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 3.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.3

File hashes

Hashes for sliceview-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 dfcbbc5b5c018e178819d9771932c3862b8f2fe3f93502ce64f26d852e8b1a8a
MD5 8496622c27e14555b0193c5c084f4e49
BLAKE2b-256 31572d7b0ebe5e75b60fbe3d9a0d56de4a1fb9137a0874ff671c7dd3066b5cf2

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