This function determines what action should be taken when performing slice assignment on a sequence.
Project description
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.
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 determine_slice_assignment_action-0.1.0a6.tar.gz.
File metadata
- Download URL: determine_slice_assignment_action-0.1.0a6.tar.gz
- Upload date:
- Size: 4.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
04fadfeeb3e5a971358ba9ba5323a6dabf51cf74b87dd747d1c1cfe1f0eceb29
|
|
| MD5 |
db3ec382906ceb8f1e8795af9b2e783e
|
|
| BLAKE2b-256 |
b0d128b0cbe31fe49df768a6a2cffe7d90f05104136d19f1df73aa60d50a44a3
|
File details
Details for the file determine_slice_assignment_action-0.1.0a6-py2.py3-none-any.whl.
File metadata
- Download URL: determine_slice_assignment_action-0.1.0a6-py2.py3-none-any.whl
- Upload date:
- Size: 4.8 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cb00d18f456ad506482455e73266846c2919054a81d05be965192877398a8e7f
|
|
| MD5 |
593ba180e9e5666204c7c155a55c2b0d
|
|
| BLAKE2b-256 |
45e430f6cd7d442bff87b737b36785dcb3bc30420a0964a23a685442f8c4086d
|