SliceView
Bases: PyoSequence[T]
flowchart TD
pyochain.core._sliceview.SliceView[SliceView]
pyochain.abc._sequences.PyoSequence[PyoSequence]
pyochain.abc._sequences.PyoReversible[PyoReversible]
pyochain.abc._collection.PyoCollection[PyoCollection]
pyochain.abc._iterable.PyoIterable[PyoIterable]
pyochain.abc._collection.PyoContainer[PyoContainer]
pyochain.abc._collection.PyoSized[PyoSized]
pyochain.abc._mixins.Checkable[Checkable]
pyochain.abc._mixins.Fluent[Fluent]
pyochain.abc._mixins.Pipe[Pipe]
pyochain.abc._mixins.Tap[Tap]
pyochain.abc._sequences.PyoSequence --> pyochain.core._sliceview.SliceView
pyochain.abc._sequences.PyoReversible --> pyochain.abc._sequences.PyoSequence
pyochain.abc._iterable.PyoIterable --> pyochain.abc._sequences.PyoReversible
pyochain.abc._mixins.Checkable --> pyochain.abc._iterable.PyoIterable
pyochain.abc._mixins.Fluent --> pyochain.abc._iterable.PyoIterable
pyochain.abc._mixins.Pipe --> pyochain.abc._mixins.Fluent
pyochain.abc._mixins.Tap --> pyochain.abc._mixins.Fluent
pyochain.abc._collection.PyoCollection --> pyochain.abc._sequences.PyoSequence
pyochain.abc._iterable.PyoIterable --> pyochain.abc._collection.PyoCollection
pyochain.abc._mixins.Checkable --> pyochain.abc._iterable.PyoIterable
pyochain.abc._mixins.Fluent --> pyochain.abc._iterable.PyoIterable
pyochain.abc._mixins.Pipe --> pyochain.abc._mixins.Fluent
pyochain.abc._mixins.Tap --> pyochain.abc._mixins.Fluent
pyochain.abc._collection.PyoContainer --> pyochain.abc._collection.PyoCollection
pyochain.abc._mixins.Checkable --> pyochain.abc._collection.PyoContainer
pyochain.abc._collection.PyoSized --> pyochain.abc._collection.PyoCollection
pyochain.abc._mixins.Checkable --> pyochain.abc._collection.PyoSized
click pyochain.core._sliceview.SliceView href "" "pyochain.core._sliceview.SliceView"
click pyochain.abc._sequences.PyoSequence href "" "pyochain.abc._sequences.PyoSequence"
click pyochain.abc._sequences.PyoReversible href "" "pyochain.abc._sequences.PyoReversible"
click pyochain.abc._collection.PyoCollection href "" "pyochain.abc._collection.PyoCollection"
click pyochain.abc._iterable.PyoIterable href "" "pyochain.abc._iterable.PyoIterable"
click pyochain.abc._collection.PyoContainer href "" "pyochain.abc._collection.PyoContainer"
click pyochain.abc._collection.PyoSized href "" "pyochain.abc._collection.PyoSized"
click pyochain.abc._mixins.Checkable href "" "pyochain.abc._mixins.Checkable"
click pyochain.abc._mixins.Fluent href "" "pyochain.abc._mixins.Fluent"
click pyochain.abc._mixins.Pipe href "" "pyochain.abc._mixins.Pipe"
click pyochain.abc._mixins.Tap href "" "pyochain.abc._mixins.Tap"
A zero-copy, composable slice view over any collections::abc::Sequence.
A SliceView presents a live window into an existing sequence:
- reads and writes reflect the underlying sequence
- view-to-view slicing composes in O(1)
- no data is copied unless explicitly requested.
Any object that implements __len__ and __getitem__ with integer indices is accepted
Credits
- Original code and idea by @julianofischer in https://github.com/julianofischer/sliceview
- Generically typed version by @hwelch-fle in https://github.com/hwelch-fle/sliceview which is what was used as the basis for this implementation.
No major changes besides linter/type-checker/docstring related-changes were made, besides the name (titled SliceView here instead of sliceview in the original repos).
And of course the pyochain integration with PyoSequence.
Examples:
from pyochain import SliceView, Seq
sv = SliceView([0, 1, 2, 3, 4, 5])
assert sv[1:4].iter().collect(Seq) == Seq(1, 2, 3)
assert sv[::2].iter().collect(Seq) == Seq(0, 2, 4)
sv2 = sv[1:][::2] # composed — O(1), no copy
assert sv2.iter().collect(Seq) == Seq(1, 3, 5)
Source code in pyochain/core/_sliceview.pyi
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 | |
inner
instance-attribute
Final[Sequence[T] | MutableSequence[T]]: The underlying sequence that this view is based on.
__new__(base, start=None, stop=None, step=None)
__new__(base: Sequence[T]) -> Self
__new__(base: Sequence[T], start: slice) -> Self
__new__(
base: Sequence[T],
start: int | None = None,
stop: int | None = None,
step: int | None = None,
) -> Self
Create a new SliceView over the given sequence.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
base
|
Sequence[T]
|
The underlying sequence. |
required |
start
|
slice | int | None
|
Starting index of the view (inclusive). Defaults to 0. |
None
|
stop
|
int | None
|
Ending index of the view (exclusive). Has no effect if start is a |
None
|
step
|
int | None
|
Step size for the view. Has no effect if start is a |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
Self |
Self
|
A new |
Source code in pyochain/core/_sliceview.pyi
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | |
advance(n)
Shift the view's window forward by n index positions in-place.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Positions to advance (negative to retreat). |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Self |
Self
|
the view with its window advanced. |
Examples:
This can be useful for sliding windows:
from pyochain import SliceView, Range, Seq
data = Range(10).iter().collect(Seq)
sv = SliceView(data, 0, 3)
assert sv.iter().collect(Seq) == Seq(0, 1, 2)
sv.advance(3)
assert sv.iter().collect(Seq) == Seq(3, 4, 5)
Source code in pyochain/core/_sliceview.pyi
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 | |