| name | format-implementation |
| description | Guide for implementing new data formats in IterableData. Use when adding support for new file formats, compression codecs, or extending format capabilities. |
Format Implementation Guide
Adding New Formats
Step-by-Step Process
- Create format file:
iterable/datatypes/<format>.py
- Implement BaseIterable: Inherit from
BaseIterable in iterable/base.py
- Required methods:
read(), write(), read_bulk(), write_bulk(), etc.
- Add detection: Update
iterable/helpers/detect.py
- Create tests:
tests/test_<format>.py
- Update dependencies: Add optional dependency to
pyproject.toml if needed
- Update documentation: Add format to docs
Implementation Pattern
from iterable.base import BaseIterable
class NewFormatIterable(BaseIterable):
def __init__(self, source, mode='r', **kwargs):
super().__init__(source, mode, **kwargs)
def read(self):
pass
def write(self, data):
pass
def read_bulk(self, size=1000):
pass
def write_bulk(self, data):
pass
Format Detection
Update iterable/helpers/detect.py:
- Add file extension detection
- Add magic number detection (for binary formats)
- Add content-based heuristics if needed
- Update
detect_file_type() function
Example:
def detect_file_type(filename, content=None):
if filename.endswith('.newformat'):
return 'newformat'
if content and content.startswith(b'MAGIC'):
return 'newformat'
Adding New Codecs
Step-by-Step Process
- Create codec file:
iterable/codecs/<codec>codec.py
- Implement codec class:
read(), write(), close() methods
- Add detection: Update
iterable/helpers/detect.py
- Add compression detection: Update format detection logic
- Create tests: Add to relevant test file or create new one
- Update dependencies: Add optional dependency to
pyproject.toml
Codec Pattern
class NewCodec:
def __init__(self, fileobj, mode='r'):
self.fileobj = fileobj
self.mode = mode
def read(self, size=-1):
pass
def write(self, data):
pass
def close(self):
pass
Testing Requirements
Test File Structure
import pytest
from iterable.helpers.detect import open_iterable
class TestNewFormat:
def test_read(self):
pass
def test_write(self):
pass
def test_read_bulk(self):
pass
def test_compressed(self):
pass
def test_edge_cases(self):
pass
Test Coverage
- Basic read/write operations
- Bulk operations
- Compressed files (if supported)
- Various encodings (for text formats)
- Edge cases: empty files, malformed data
- Missing optional dependencies (should skip gracefully)
Dependencies
Optional Dependencies
Add to pyproject.toml:
[project.optional-dependencies]
newformat = ["newformat-library>=1.0.0"]
Import Handling
Handle missing dependencies gracefully:
try:
import newformat_library
except ImportError:
raise ImportError(
"newformat support requires 'newformat-library'. "
"Install with: pip install iterabledata[newformat]"
)
Format Capabilities
Implement capability reporting:
def get_capabilities(self):
return {
'read': True,
'write': True,
'bulk': True,
'totals': False,
'streaming': True,
'tables': False,
}
Examples
Look at existing implementations:
iterable/datatypes/csv.py - Text format example
iterable/datatypes/parquet.py - Binary format example
iterable/codecs/gzipcodec.py - Compression codec example
Common Pitfalls
- Memory issues: Use streaming for large files
- Encoding: Handle various text encodings automatically
- Compression: Support common codecs (gzip, bz2, zstd, etc.)
- Error messages: Provide helpful errors for missing dependencies
- Context managers: Always support
with statements
- Filename-only formats: If your format requires a file path and does not support stream or codec, validate in
reset() (or __init__) and raise a clear error (e.g. ReadError) when filename is None.
- read_bulk() exhaustion: When no more records are available,
read_bulk() MUST return an empty list []; do not raise StopIteration.