Registry / data / slicerator

slicerator

JSON →
library1.1.0pypypi✓ verified 90d ago

Slicerator is a Python library that provides a lazy-loading, fancy-sliceable iterable. It allows you to wrap existing classes or functions to make them behave like reusable generators that support advanced slicing (like NumPy arrays), but only load data when explicitly accessed. The current version is 1.1.0, released in April 2022. While releases are not on a strict schedule, it remains a functional and actively used utility in various projects.

pip install slicerator
INSTALL
IMPORT
SIG · SLICERATOR
S
slicerator
datapythonv1.1.0
Install
1.7s avg
Import
31ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.9–3.13
musl
3.9–3.13
Install & Compatibility
Where this runs
tested against v1.1.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.10–3.920 runs
installs and imports cleanly · install 0.0s · import 0.033s · 17.8MB
glibc
py 3.10–3.920 runs
installs and imports cleanly · install 1.7s · import 0.029s · 18MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Slicerator
✓ from slicerator import Slicerator
pipeline
✓ from slicerator import pipeline
Used as a decorator for lazy function evaluation, often imported alongside Slicerator.

This quickstart demonstrates how to use the `@Slicerator.from_class` decorator to turn a regular class with `__getitem__` and `__len__` into a lazy-loading, fancy-sliceable iterable. It highlights that actual data loading (`__getitem__` execution) only occurs when specific items are requested or when the Slicerator is fully iterated.

from slicerator import Slicerator @Slicerator.from_class class MyLazyLoader: def __getitem__(self, i): # This method is wrapped by Slicerator to accept slices, lists of integers, or boolean masks. # The code below will only execute when an individual item (integer index) is requested. # In a real application, this would load data from disk, a database, or network. print(f"[DEBUG] Loading item {i}") return f"data_item_{i}" def __len__(self): # Slicerator needs __len__ for proper slicing (e.g., reverse slicing or knowing slice bounds). print("[DEBUG] Calculating total length") return 10 # Instantiate the lazy loader loader = MyLazyLoader() # Create a Slicerator object by slicing. No data is loaded yet. sliced_data = loader[::2] # Get every second item print(f"\nInitial slice created: {sliced_data}") # Further slice the Slicerator. Still no data loaded. sub_sliced_data = sliced_data[1:] # Get elements from index 1 onwards of the sliced_data print(f"Further sliced: {sub_sliced_data}") # Access a single item - this triggers loading for that specific item. first_item = sub_sliced_data[0] print(f"First item accessed: {first_item}") # Convert to list - this triggers loading for all items in `sub_sliced_data`. all_items = list(sub_sliced_data) print(f"All items loaded: {all_items}")
Debug
Known issues
gotchaOperations like `list(my_slicerator)` or iterating directly over a Slicerator object (`for item in my_slicerator:`) will force all data to be loaded eagerly, negating the benefits of lazy loading for the entire dataset. Always be mindful of operations that trigger full evaluation.
fix
Access specific elements or smaller slices when possible (`my_slicerator[index]`, `my_slicerator[start:end]`) rather than converting the entire object to a collection if lazy loading is desired.
affects: All versions
gotchaThe propagation of attributes from the original class to the Slicerator object can be complex. There are multiple mechanisms (`propagate_attrs` parameter, `@propagate_attr` decorator, or a `propagate_attrs` class attribute), and their precedence can lead to unexpected behavior if not understood.
fix
Consult the official documentation for the order of precedence and recommended practices for attribute propagation. Use explicit methods like the `propagate_attrs` argument in `from_class` for clarity.
affects: All versions, specifically 0.9.5 onwards which fixed propagation issues.
breakingVersion 0.9.8 introduced changes enabling `Pipelines` to modify properties. This might alter the expected behavior of existing pipelines that relied on immutable properties or specific side-effects, potentially leading to different outcomes after an upgrade.
fix
Review any code utilizing `slicerator.pipeline` or `Pipeline` objects when upgrading to version 0.9.8 or later. Test pipelines thoroughly to ensure property modifications align with new expectations.
affects: Upgrading from <0.9.8 to 0.9.8+
Errors
Common errors & fixes
TypeError: object is not subscriptable
You are attempting to slice a regular Python object (e.g., an instance of `MyLazyLoader`) directly, without it being wrapped or decorated by `Slicerator`.
fix
Ensure your class is decorated with `@Slicerator.from_class` or your function is explicitly wrapped with `Slicerator.from_func` to enable fancy slicing. For instance, `loader = MyLazyLoader()` should be `loader = Slicerator.from_class(MyLazyLoader)()` or `loader = MyLazyLoader()` where `MyLazyLoader` is decorated.
TypeError: object of type 'MyLazyLoader' has no len()
When using `@Slicerator.from_class` or `Slicerator.from_func`, the underlying object or function is expected to provide a length, typically via a `__len__` method for classes or the `length` argument for `from_func`.
fix
Implement a `__len__` method in your class that returns the total number of items, or pass the `length` argument when creating a `Slicerator` using `Slicerator.from_func`.
AttributeError: 'Slicerator' object has no attribute 'my_custom_attribute'
Attributes from the original class are not automatically propagated to the `Slicerator` instance by default. You tried to access an attribute defined on your base class directly on the Slicerator object.
fix
Use the `propagate_attrs` parameter in `@Slicerator.from_class` (e.g., `@Slicerator.from_class(propagate_attrs=['my_custom_attribute'])`), use the `@propagate_attr` decorator on the method/property within your class, or define a `propagate_attrs` class attribute to explicitly list attributes to be propagated.
Upgrade
Version history
1.1.0latest on PyPI · released Apr 7, 2022
Audit
Dependencies
pythonrequiredSlicerator requires Python 3.7 or newer to run.
Agent activity
11 hits · last 30 days
node
10
OpenAI (training)
1
Resources
slicerator — pip install slicerator · libregistry