The Ultimate Slicing Module for various data types and files.
Project description
Datachop: The Ultimate Slicing Module
A high-performance, robust, and versatile Python library for slicing and processing various data types and files. Datachop goes beyond simple string slicing, providing a unified and intuitive API for handling everything from Unicode text to complex file formats like images, videos, audio, and documents.
This library is designed for developers who need a single, reliable tool for data extraction and manipulation, ensuring correctness and efficiency across all data types.
👤 Author Information
- Author: Mallik Mohammad Musaddiq
- Email:
mallikmusaddiq1@gmail.com - GitHub:
https://github.com/mallikmusaddiq1/datachop
🚀 Features
- Universal Slicing: A single
chop()function handles strings, lists, bytes, files, and more. - Emoji-Aware Slicing: Correctly slices strings based on Unicode grapheme clusters, not raw characters, ensuring emojis and multi-character symbols are handled properly.
- File Format Support:
- Text Files: Slice by line, character, or byte with automatic encoding detection.
- Images: Crop and extract pixels or regions from PNG, JPEG, GIF, and TIFF files.
- Videos: Extract frames or sub-clips from MP4, AVI, MOV, and MKV files.
- Audio: Slice audio by time or frame count from MP3, WAV, and FLAC files.
- Documents: Extract pages from PDFs, paragraphs from DOCX/ODT files, and rows from CSV/JSON.
- Performance & Efficiency:
- Uses
mmapfor efficient file I/O on large files. - Thread-safe caching (
LRUCache) for repeated operations. - Asynchronous I/O and multi-threading for parallel processing.
- Uses
- Extensibility: A powerful plugin system allows developers to easily add support for new file types and custom slicing logic.
- Security & Robustness:
- Sandboxing: Prevents path traversal attacks with a secure file handling context.
- Comprehensive Error Handling: Provides clear, actionable error codes for every possible failure (
CHOP-001,CHOP-002, etc.).
- Dependency Management: Optional dependencies are lazily loaded and gracefully handle missing packages.
📦 Installation
Install Datachop using pip. For advanced features, you can install optional dependencies with an [extras] flag.
# Basic installation for string, list, and text file slicing
pip install datachop
# Install with all optional dependencies for full functionality
pip install datachop[full]
Optional Dependencies
* Pillow: Image processing (PNG, JPEG, etc.).
* moviepy, ffmpeg-python: Video and GIF processing.
* pydub: Audio processing (MP3, WAV, etc.).
* PyPDF2, python-docx, odfpy: Document processing (PDF, DOCX, ODT).
* redis: Distributed caching for large-scale applications.
* tqdm: Progress bars for long-running operations.
📖 Usage
Slicing a String
Datachop's chop() function intelligently handles Unicode and emojis.
import asyncio
from datachop import chop
async def main():
text = "Hello, World! 😊👍🚀"
# Slice a single character (grapheme)
result = await chop(text, 14)
# result: '👍'
# Slice a range
result = await chop(text, slice(7, 12))
# result: 'World'
# Get length of a string in graphemes
length = await chop(text)
# length: 17
asyncio.run(main())
Slicing a File (by line)
You can slice text files by line number, just like a list.
import asyncio
from datachop import chop
# Create a sample file
with open("sample.txt", "w", encoding="utf-8") as f:
f.write("Line 1\nLine 2\nLine 3\nLine 4")
async def main():
# Get a specific line
line = await chop("sample.txt", 2, file_mode='lines')
# line: 'Line 3'
# Slice multiple lines
lines = await chop("sample.txt", slice(0, 3), file_mode='lines')
# lines: ['Line 1', 'Line 2', 'Line 3']
asyncio.run(main())
Slicing an Image
Extract pixels or crop a specific region from an image.
import asyncio
from datachop import chop, get_length
from PIL import Image
# Create a sample image
img = Image.new('RGB', (100, 100), color='blue')
img.save("sample.png")
async def main():
# Get the total number of pixels
pixel_count = await get_length("sample.png", file_mode='pixels')
# pixel_count: 10000
# Get the pixel at index 5000 (roughly center)
pixel = await chop("sample.png", 5000)
# pixel: (0, 0, 255)
# Crop a 50x50 region and save it to a new file
cropped_image = await chop(
"sample.png",
region=(25, 25, 75, 75),
export_path="cropped.png"
)
# A new file 'cropped.png' is created with the cropped region.
# The function also returns a PIL Image object.
asyncio.run(main())
Slicing a Video (by time)
Extract a sub-clip from a video file.
import asyncio
from datachop import chop
async def main():
# This requires an existing video file like "video.mp4"
# and the 'moviepy' and 'ffmpeg-python' dependencies.
# Extract a 5-second clip from 10s to 15s and save it
clip = await chop(
"video.mp4",
slice(10, 15),
file_mode='time',
export_path="clip.mp4"
)
# A new file 'clip.mp4' is created.
asyncio.run(main())
📝 API Reference
chop(obj, index_or_slice, **kwargs)
The primary function for all slicing operations.
Parameters:
* obj: The data to be sliced (string, list, file path, file object, etc.).
* index_or_slice: An int for a single element, a slice object, or an iterable of indices.
* **kwargs:
* file_mode (str): Specifies how to read a file ('lines', 'bytes', 'pixels', 'frames', etc.). This is inferred if not provided.
* region (tuple): A 4-tuple (x1, y1, x2, y2) for cropping images.
* export_path (str): Path to save the sliced content (e.g., cropped image, video clip).
* compression (str): Type of compression for archives ('zip', 'gz', etc.).
* decode (str): Decode sliced bytes into a string using the specified encoding.
get_length(obj, **kwargs)
Returns the length of a sequence or a file based on the specified mode.
Parameters:
* obj: The data object (string, list, file path).
* **kwargs: Same as chop().
<!-- end list -->
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 datachop-1.0.0.tar.gz.
File metadata
- Download URL: datachop-1.0.0.tar.gz
- Upload date:
- Size: 20.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
95df66c6b3c40e30d0c0d6ef8fd1cc08ffdbf62eb78a3fb31a836a07071a9cd3
|
|
| MD5 |
1a912bad7e569b1404b96a21b9c4f567
|
|
| BLAKE2b-256 |
c704327665f55cbeafa705cd2c88e24459ed43a65126e35a687eb4340ca82860
|
File details
Details for the file datachop-1.0.0-py3-none-any.whl.
File metadata
- Download URL: datachop-1.0.0-py3-none-any.whl
- Upload date:
- Size: 20.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
25150fcd5b87d9400b11dafa5eba88cd5672ccc35c1eeaecfaa4e7f163222b5a
|
|
| MD5 |
c5ecf2550dbf4e641f8e91ba8930fb4f
|
|
| BLAKE2b-256 |
38fa7680ab769748bf15f664b3449a59fbffc81d9ba9085474023f8796165e1c
|