Python class implementation with dunder methods
Curated, evidence-grounded skill and software-tool collections for scientific AI agents, generated by the AgenticScienceBuilder
npx -y skills add HolobiomicsLab/asb-skill-collections --skill python-class-implementation-with-dunder-methodsAssembled from the repository path, not quoted from the project. Check it against their README if it does not work.
One thing to look at
- 14 stars14 stars. Stars are a popularity signal and not a quality one, but at this level it is likely that nobody has read this closely except its author, and you would be relying on your own review.
What its author says it does
Copied from the file, not written here
Use when you have mzML spectral data stored in a non-standard format (SQLite database, custom binary store, or indexed archive) and need pymzML's Reader to parse and iterate over spectra as if they were in native mzML files. Specifically, when FileInterface.
The file declares its own license as CC-BY-4.0. That is the author’s claim about this one file, and it is not the same thing as the license GitHub reports for the repository, which is listed with the other numbers below.
SKILL.md
6.7 KB, ~1.2k tokens by cl100k_base, as published. Nobody here has run it
python-class-implementation-with-dunder-methods
Summary
Implement custom Python classes with dunder methods (getitem, init, read) to enable random and sequential access to mzML spectral data stored in alternative formats (SQLite databases, compressed files). This skill bridges data storage backends with pymzML's Reader interface by defining the protocol through which the library retrieves spectra on demand.
When to use
You have mzML spectral data stored in a non-standard format (SQLite database, custom binary store, or indexed archive) and need pymzML's Reader to parse and iterate over spectra as if they were in native mzML files. Specifically, when FileInterface._open() must dispatch file paths to a custom handler that implements both random access (by spectrum ID) and sequential iteration.
When NOT to use
- Input is already a native .mzML or .mzML.gz file — use pymzML's built-in parser instead
- You do not need random access — streaming a single pass through sequential data may not justify the implementation overhead
- The storage backend does not support indexed or keyed retrieval — the class requires both getitem and read methods
Inputs
- SQLite database file (.db) containing spectrum IDs and XML element strings in a 'Spectra' table
- File path string (detected by extension)
- Integer or string spectrum identifiers (keys)
Outputs
- Spectrum or Chromatogram objects (from pymzML spec module)
- XML element strings (from read() method for sequential access)
- File handler object integrated into pymzML's FileInterface
How to apply
Define a custom class (e.g., SQLiteDatabase) that implements three dunder methods: (1) init(filepath) to open and cache the underlying storage connection; (2) getitem(key) to retrieve individual Spectrum or Chromatogram objects by integer index or string spectrum ID, parsing XML elements from storage and constructing spec module objects; (3) read() to yield sequential XML strings for iteration during normal file traversal. Parse XML strings using ElementTree to convert database-stored elements into pymzML spectrum objects. Finally, modify FileInterface._open() to detect the file format (e.g., check path.endswith('.db')) and instantiate your custom class instead of the default mzML handler, routing the file_handler assignment accordingly.
Related tools
- pymzML (Provides Reader, spec module, and FileInterface dispatcher that the custom class integrates with) — https://github.com/pymzml/pymzML
- sqlite3 (Built-in Python library to query and retrieve spectral XML from the database backend)
- ElementTree (Parses XML strings retrieved from storage into XML elements for spectrum construction)
- black (Code formatting standard used in pymzML project for style consistency) — https://github.com/psf/black
Examples
from pymzml import Reader; import sqlite3; r = Reader('spectra.db'); spectra = [spec for spec in r]; print(f'Loaded {len(spectra)} spectra from database')
Evaluation signals
- Instantiate the custom class with a test .db file and verify getitem(0) returns a valid Spectrum or Chromatogram object with correct ID and m/z–intensity pairs
- Call read() sequentially and confirm it yields all spectrum XML strings in the database without duplication
- Confirm FileInterface._open() correctly detects the .db extension and routes to the custom class (trace via isinstance checks or file_handler type)
- Iterate over pymzML.Reader(db_path) and verify spectrum count matches the database table row count
- Check that spectrum metadata (scan time, mass range, precursor m/z) parsed from XML matches the original mzML file
Limitations
- Performance depends on database query efficiency and storage I/O; random access is only as fast as the underlying database index
- XML parsing adds memory overhead for large spectra; very large XML elements may exceed available RAM during element construction
- The class must implement both getitem and read methods — incomplete implementations will fail pymzML iteration or indexing workflows
- File format detection in FileInterface._open() is extension-based; databases with non-.db extensions will not be routed to the custom handler
Evidence
- [intro] a new class needs to be written, which implements a
readand a__getitem__function: "a new class needs to be written, which implements areadand a__getitem__function" - [other] getitem should parse XML and return Spectrum or Chromatogram objects via pymzML's spec module: "getitem should parse XML and return Spectrum or Chromatogram objects"
- [other] Add an elif clause to FileInterface._open() that detects '.db' file extensions and instantiates SQLiteDatabase: "Add an elif clause to FileInterface._open() that detects '.db' file extensions and instantiates SQLiteDatabase"
- [other] In order to make pymzML accept other kinds of mzML data (e.g databases), one can implement an own wrapper: "In order to make pymzML accept other kinds of mzML data (e.g databases), one can implement an own wrapper"
- [readme] pymzML is an extension to Python that offers a very fast parser for mzML data, the standard mass spectrometry data format: "pymzML is an extension to Python that offers a very fast parser for mzML data, the standard mass spectrometry data format"