This release is a pre-release and may not be stable for production use.
This function determines what action should be taken when performing slice assignment on a sequence.
Usage
class SliceAssignmentAction: ...
class NoAction(SliceAssignmentAction): pass
class ReplaceOffsetRange(SliceAssignmentAction):
offset_start: int
offset_stop: int
offset_step: int
class Insert(SliceAssignmentAction):
index: int
reverse: bool
def determine_slice_assignment_action(sequence_length: int, slice_object: slice) -> SliceAssignmentAction: ...
The function takes:
sequence_length: int: Length of the target sequenceslice_object: slice: The slice object
It returns one of several action classes that describe how the assignment should be handled:
NoAction: No operation neededReplaceOffsetRange: Replace a valid and canonicalized (step != 0, length >= 0, stop == start + length * step) offset range of elements in the sequenceInsert: Insert new elements at a specific index (in the closed interval[0, sequence_length])
Implementation
- The function first converts the slice to absolute offsets using
slice_to_offset_range. - For empty selections (where
start == stop):- If the index is within bounds, it's an insertion
- If before the sequence, it's an insertion at the beginning
- If after, it's an insertion at the end
- For non-empty sequences:
- With positive step: splits the interval and either replaces or inserts at the beginning or end
- With negative step: similar but works in reverse
One notable limitation is that some negative step cases aren't fully supported in Python's actual slice assignment (
ValueError: attempt to assign sequence of size 2 to extended slice of size 0), but the function still returns the
theoretically correct action.
Examples
from determine_slice_assignment_action import *
# Non-empty sequence, non-empty slice (`ReplaceOffsetRange`)
# l = list(range(10)); l[2:20] = [False, True]; print(l)
# [0, 1, False, True]
result = determine_slice_assignment_action(10, slice(2, 20))
assert isinstance(result, ReplaceOffsetRange)
assert (result.offset_start, result.offset_stop, result.offset_step) == (2, 10, 1) # Canonicalized
# l = list(range(10)); l[2:-3:2] = [False, True, False]; print(l)
# [0, 1, False, 3, True, 5, False, 7, 8, 9]
result = determine_slice_assignment_action(10, slice(2, -3, 2))
assert isinstance(result, ReplaceOffsetRange)
assert (result.offset_start, result.offset_stop, result.offset_step) == (2, 8, 2) # Canonicalized
# l = list(range(10)); l[5:3:-1] = [False, True]; print(l)
# [0, 1, 2, 3, True, False, 6, 7, 8, 9]
result = determine_slice_assignment_action(10, slice(5, 3, -1))
assert isinstance(result, ReplaceOffsetRange)
assert (result.offset_start, result.offset_stop, result.offset_step) == (5, 3, -1)
# l = list(range(10)); l[5:-12:-1] = [False, True, False, True, False, True]; print(l)
# [True, False, True, False, True, False, 6, 7, 8, 9]
result = determine_slice_assignment_action(10, slice(5, -12, -1))
assert isinstance(result, ReplaceOffsetRange)
assert (result.offset_start, result.offset_stop, result.offset_step) == (5, -1, -1) # Canonicalized
# Non-empty sequence, empty slice (`Insert`)
# l = list(range(10)); l[5:5] = [False, True]; print(l)
# [0, 1, 2, 3, 4, False, True, 5, 6, 7, 8, 9]
result = determine_slice_assignment_action(10, slice(5, 5))
assert isinstance(result, Insert)
assert result.reverse == False
assert result.index == 5
# l = list(range(10)); l[5:3] = [False, True]; print(l)
# [0, 1, 2, 3, 4, False, True, 5, 6, 7, 8, 9]
result = determine_slice_assignment_action(10, slice(5, 3))
assert isinstance(result, Insert)
assert result.reverse == False
assert result.index == 5
# l = list(range(10)); l[-11:-11] = [False, True]; print(l)
# [False, True, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
result = determine_slice_assignment_action(10, slice(-11, -11))
assert isinstance(result, Insert)
assert result.reverse == False
assert result.index == 0
# l = list(range(10)); l[-12:0] = [False, True]; print(l)
# [False, True, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
result = determine_slice_assignment_action(10, slice(-12, 0))
assert isinstance(result, Insert)
assert result.reverse is False
assert result.index == 0
# l = list(range(10)); l[10:10] = [False, True]; print(l)
# [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, False, True]
result = determine_slice_assignment_action(10, slice(10, 10))
assert isinstance(result, Insert)
assert result.reverse == False
assert result.index == 10
# l = list(range(10)); l[15:20] = [False, True]; print(l)
# [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, False, True]
result = determine_slice_assignment_action(10, slice(15, 20))
assert isinstance(result, Insert)
assert result.reverse is False
assert result.index == 10
# l = list(range(10)); l[10:10:-1] = [False, True]; print(l)
# Theoretically possible, not yet supported by Python
# ValueError: attempt to assign sequence of size 2 to extended slice of size 0
result = determine_slice_assignment_action(10, slice(10, 10, -1))
assert isinstance(result, Insert)
assert result.reverse == True
assert result.index == 10
# l = list(range(10)); l[20:15:-1] = [False, True]; print(l)
# Theoretically possible, not yet supported by Python
# ValueError: attempt to assign sequence of size 2 to extended slice of size 0
result = determine_slice_assignment_action(10, slice(20, 15, -1))
assert isinstance(result, Insert)
assert result.reverse is True
assert result.index == 10
# l = list(range(10)); l[-11:-15:-1] = [False, True]; print(l)
# Theoretically possible, not yet supported by Python
# ValueError: attempt to assign sequence of size 2 to extended slice of size 0
result = determine_slice_assignment_action(10, slice(-11, -15, -1))
assert isinstance(result, Insert)
assert result.reverse is True
assert result.index == 0
# Empty sequence, empty slice (`Insert`)
# l = list(); l[0:5] = [False, True]; print(l)
# [False, True]
result = determine_slice_assignment_action(0, slice(0, 5))
assert isinstance(result, Insert)
assert result.reverse is False
assert result.index == 0
# l = list(); l[5:0:-1] = [False, True]; print(l)
# Theoretically possible, not yet supported by Python
# ValueError: attempt to assign sequence of size 2 to extended slice of size 0
result = determine_slice_assignment_action(0, slice(5, 0, -1))
assert isinstance(result, Insert)
assert result.reverse is True
assert result.index == 0
Contributing
Contributions are welcome! Please submit pull requests or open issues on the GitHub repository.
License
This project is licensed under the MIT License.
Release files for determine-slice-assignment-action 0.1.0a6
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| determine_slice_assignment_action-0.1.0a6.tar.gz | 4.4 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| determine_slice_assignment_action-0.1.0a6-py2.py3-none-any.whl | Python 3, Python 2 | none | any | Details |
Total release size: 9.2 kB
Release files / determine_slice_assignment_action-0.1.0a6.tar.gz
| Download URL | determine_slice_assignment_action-0.1.0a6.tar.gz |
|---|---|
| Size | 4.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
04fadfeeb3e5a971358ba9ba5323a6dabf51cf74b87dd747d1c1cfe1f0eceb29
|
|
BLAKE2b-256 checksum How to use checksums |
b0d128b0cbe31fe49df768a6a2cffe7d90f05104136d19f1df73aa60d50a44a3
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.12.2
|
Release files / determine_slice_assignment_action-0.1.0a6-py2.py3-none-any.whl
| Download URL | determine_slice_assignment_action-0.1.0a6-py2.py3-none-any.whl |
|---|---|
| Size | 4.8 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
cb00d18f456ad506482455e73266846c2919054a81d05be965192877398a8e7f
|
|
BLAKE2b-256 checksum How to use checksums |
45e430f6cd7d442bff87b737b36785dcb3bc30420a0964a23a685442f8c4086d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.12.2
|