Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/improve-types' into abstract-cla…
Browse files Browse the repository at this point in the history
…sses

# Conflicts:
#	cfgrib/abc.py
#	cfgrib/messages.py
  • Loading branch information
alexamici committed Aug 2, 2021
2 parents 2830e87 + 4df1d8d commit 24bc5de
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/on-push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ jobs:
- name: Type check with mypy
shell: bash -l {0}
run: |
mypy --strict cfgrib
mypy --strict cfgrib tests/test_*py
code-style:
runs-on: ubuntu-latest
Expand Down
56 changes: 54 additions & 2 deletions cfgrib/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,9 +238,9 @@ def itervalues(self) -> T.Iterator[Message]:
raise EOFError("No valid message found: %r" % self.filestream.path)
break
except Exception:
if errors == "ignore":
if self.errors == "ignore":
pass
elif errors == "raise":
elif self.errors == "raise":
raise
else:
LOG.exception("skipping corrupted Message")
Expand Down Expand Up @@ -399,3 +399,55 @@ def from_indexpath_or_filestream(
log.exception("Can't read index file %r", indexpath)

return cls.from_filestream(filestream, index_keys)

def __iter__(self) -> T.Iterator[str]:
return iter(self.index_keys)

def __len__(self) -> int:
return len(self.index_keys)

@property
def header_values(self) -> T.Dict[str, T.List[T.Any]]:
if not hasattr(self, "_header_values"):
all_header_values = {} # type: T.Dict[str, T.Dict[T.Any, None]]
for header_values, _ in self.offsets:
for i, value in enumerate(header_values):
values = all_header_values.setdefault(self.index_keys[i], {})
if value not in values:
values[value] = None
self._header_values = {k: list(v) for k, v in all_header_values.items()}
return self._header_values

def __getitem__(self, item: str) -> T.List[T.Any]:
return self.header_values[item]

def getone(self, item):
# type: (str) -> T.Any
values = self[item]
if len(values) != 1:
raise ValueError("not one value for %r: %r" % (item, len(values)))
return values[0]

def subindex(self, filter_by_keys={}, **query):
# type: (T.Mapping[str, T.Any], T.Any) -> FileIndex
query.update(filter_by_keys)
raw_query = [(self.index_keys.index(k), v) for k, v in query.items()]
offsets = []
for header_values, offsets_values in self.offsets:
for idx, val in raw_query:
if header_values[idx] != val:
break
else:
offsets.append((header_values, offsets_values))
index = type(self)(
filestream=self.filestream,
index_keys=self.index_keys,
offsets=offsets,
filter_by_keys=query,
)
return index

def first(self) -> Message:
with open(self.filestream.path, "rb") as file:
first_offset = self.offsets[0][1][0]
return self.filestream.message_from_file(file, offset=first_offset)

0 comments on commit 24bc5de

Please sign in to comment.