How to Create a Data Splitter Class in Python
This code defines a DataSplitter class that splits data by index, into chunks, or by a predicate, demonstrating OOP principles in Python.
Python code
28 linesclass DataSplitter:
def __init__(self, data):
self.data = list(data)
def split_by_index(self, index):
return self.data[:index], self.data[index:]
def split_into_chunks(self, chunk_size):
return [self.data[i:i + chunk_size] for i in range(0, len(self.data), chunk_size)]
def split_by_predicate(self, predicate):
matching = [item for item in self.data if predicate(item)]
non_matching = [item for item in self.data if not predicate(item)]
return matching, non_matching
if __name__ == "__main__":
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
splitter = DataSplitter(numbers)
first, second = splitter.split_by_index(4)
print(f"Split at index 4: {first} | {second}")
chunks = splitter.split_into_chunks(3)
print(f"Chunks of size 3: {chunks}")
evens, odds = splitter.split_by_predicate(lambda x: x % 2 == 0)
print(f"Evens: {evens} | Odds: {odds}")
Output
Split at index 4: [1, 2, 3, 4] | [5, 6, 7, 8, 9, 10]
Chunks of size 3: [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10]]
Evens: [2, 4, 6, 8, 10] | Odds: [1, 3, 5, 7, 9]
How it works
The __init__ method converts the input to a list, allowing any iterable to be used. split_by_index uses slicing to return two new lists at the given index. split_into_chunks uses list comprehension with a range step to create fixed-size chunks. split_by_predicate uses list comprehensions with the predicate to separate matching and non-matching items, and the if __name__ == "__main__" guard runs the example only when executed directly.
Common mistakes
- Forgetting to convert input to a list, which breaks for generators or sets.
- Assuming slice indices are zero-based incorrectly.
- Using a predicate that mutates the data unexpectedly.
- Not handling chunk_size of zero or negative.
Variations
- Add a `max_chunks` parameter to limit the number of chunks.
- Use `itertools.islice` to split iterators lazily without creating a full list.
Real-world use cases
- Splitting a dataset into training and testing sets by index for machine learning.
- Batch processing: dividing a large list of items into chunks for parallel or sequential tasks.
- Filtering log entries into error and non-error categories based on a predicate.
Sponsored
More from OOP & classes
- Add property getter setter validation in Python easy
- Binary Tree Inorder Traversal in Python easy
- Borg pattern shared state in Python medium
- Bridge Pattern in Python: Separate Abstraction from Implementation medium
- Composable Predicates with the &, |, ~ Operators in Python medium
- Composition over Inheritance: How to Build a Wallet Account in Python easy
Keep learning
Related tutorials and quizzes for this topic.