Skip to content

Commit a4c6e1c

Browse files
committed
adding a __getitem__ for PyRecord
1 parent 9ba1fb4 commit a4c6e1c

2 files changed

Lines changed: 22 additions & 7 deletions

File tree

microbiorust-py/src/lib.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,22 @@ impl PyRecord {
442442
),
443443
}
444444
}
445+
fn __getitem__(&self, py: Python<'_>, tag: &str) -> PyResult<PyObject> {
446+
let features = self.features(py);
447+
if features.inner.contains_key(tag) {
448+
// 1. Get the Bound<'_, PyFeatureInfo>
449+
// 2. Use .into_any() to get Bound<'_, PyAny>
450+
// 3. Use .unbind() to get PyObject
451+
return features.__getitem__(tag, py).map(|b| b.into_any().unbind());
452+
}
453+
454+
let sequences = self.sequences(py);
455+
if sequences.inner.contains_key(tag) {
456+
return sequences.__getitem__(tag, py).map(|b| b.into_any().unbind());
457+
}
458+
459+
Err(PyKeyError::new_err(tag.to_string()))
460+
}
445461
}
446462
//parse genbank function - returns the RecordCollection, good for fna and metadata from source
447463
#[pyfunction]

microbiorust-py/tests/test_mbr.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import pytest
88

99

10-
# --- FIXTURES: Generating Mock Data ---
10+
# FIXTURES: Generating Mock Data ---
1111
@pytest.fixture
1212
def mock_gbk(tmp_path):
1313
"""Creates a minimal valid GenBank file."""
@@ -81,7 +81,6 @@ def mock_blast_tab(tmp_path):
8181
return str(path)
8282

8383

84-
<<<<<<< HEAD
8584
def test_gbk_loader_aliases(mock_gbk):
8685
"""Verify all specialized GBK loaders return a valid collection"""
8786
# Test all three aliases to ensure they point to the correct InternalRecord::Gbk logic
@@ -93,12 +92,12 @@ def test_gbk_loader_aliases(mock_gbk):
9392

9493
for load_func in loaders:
9594
collection = load_func(mock_gbk)
96-
assert "source_1" in collection.keys()
95+
keys = list(collection.keys())
96+
97+
# This checks if "source_1" is either the key itself (FNA)
98+
# or the prefix of the key (FAA/FFN)
99+
assert any(k.startswith("source_1") for k in keys), f"Failed on {load_func.__name__}"
97100
assert len(collection.keys()) > 0
98-
assert "SequenceCollection" in repr(collection)
99-
=======
100-
import os
101-
>>>>>>> 505cee7 (final branch fixes)
102101

103102

104103
def test_gbk_count_function(mock_gbk):

0 commit comments

Comments
 (0)