add items() to FilePattern
MODIFY
pangeo_forge/patterns.py
pangeo_forge/patterns.py
@@ -134,6 +134,11 @@ class FilePattern:
for val in product(*[range(n) for n in self.shape]):
yield val
+ def items(self):
+ """Iterate over key, filename pairs."""
+ for key in self:
+ yield key, self[key]
+
def pattern_from_file_sequence(file_list, concat_dim, nitems_per_file=None):
"""Convenience function for creating a FilePattern from a list of files."""

MODIFY
tests/test_patterns.py
tests/test_patterns.py
@@ -33,6 +33,7 @@ def test_pattern_from_file_sequence():
assert list(fp) == expected_keys
for key in expected_keys:
assert fp[key] == file_sequence[key[0]]
+ assert list(fp.items()) == list(zip(expected_keys, file_sequence))
def test_file_pattern_concat_merge():
@@ -51,5 +52,9 @@ def test_file_pattern_concat_merge():
assert fp.concat_sequence_lens == {"time": None}
expected_keys = [(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2)]
assert list(fp) == expected_keys
+ fnames = []
for key in expected_keys:
- assert fp[key] == format_function(variable=merge.keys[key[0]], time=concat.keys[key[1]])
+ fname = format_function(variable=merge.keys[key[0]], time=concat.keys[key[1]])
+ assert fp[key] == fname
+ fnames.append(fname)
+ assert list(fp.items()) == list(zip(expected_keys, fnames))

add items() to FilePattern