Skip to content

Commit f6648db

Browse files
committed
.
1 parent 0f314b6 commit f6648db

2 files changed

Lines changed: 31 additions & 0 deletions

File tree

openmatrix/file.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,9 +355,14 @@ def __iter__(self):
355355
return iter([])
356356

357357
def __contains__(self, item):
358+
# Respect root-level members first (e.g., data/lookup groups or attrs exposed by h5py)
359+
if super(File, self).__contains__(item):
360+
return True
361+
358362
if super(File, self).__contains__("data"):
359363
data_group = super(File, self).__getitem__("data")
360364
return item in data_group
365+
361366
return False
362367

363368
# BACKWARD COMPATIBILITY:

tests/test_file.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,20 @@ def test_contains():
133133
assert "m1" in f
134134

135135

136+
def test_contains_groups_and_datasets():
137+
with omx.open_file(TEST_FILE, "w") as f:
138+
# groups auto-created in writable mode
139+
assert "data" in f
140+
assert "lookup" in f
141+
142+
f.create_mapping("zones", entries=np.array([1, 2, 3]))
143+
f.create_matrix("m1", obj=np.ones((5, 5)))
144+
145+
assert "m1" in f # dataset inside data
146+
assert "zones" in f.lookup # dataset inside lookup group
147+
assert "missing" not in f
148+
149+
136150
def test_list_all_attrs():
137151
with omx.open_file(TEST_FILE, "w") as f:
138152
add_m1_node(f)
@@ -204,3 +218,15 @@ def test_mappings():
204218
assert entries == [1, 2, 3]
205219
with pytest.raises(LookupError):
206220
f.map_entries("missing")
221+
222+
223+
def test_open_existing_with_append_mode():
224+
# Create file and add a matrix
225+
with omx.open_file(TEST_FILE, "w") as f:
226+
data = np.arange(9, dtype=float).reshape(3, 3)
227+
f.create_matrix("my_matrix", obj=data)
228+
229+
# Re-open in append mode and ensure dataset is accessible
230+
with omx.open_file(TEST_FILE, "a") as f:
231+
assert "my_matrix" in f
232+
npt.assert_array_equal(f["my_matrix"], data)

0 commit comments

Comments
 (0)