Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
nh13 committed Aug 15, 2024
1 parent 7829047 commit b06b986
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 10 deletions.
15 changes: 7 additions & 8 deletions fgpyo/util/metric.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,10 @@
["first last", "42"]
```
Re-ordering and sub-setting the columns when writing is supported by overriding the `_header()`
method. In the example below, the `weight` field is not written, but is optional to support reading
the metric back in. Also, the `name` and `age` columns are written in reverse order.
Re-ordering and sub-setting the columns when writing is supported by overriding the
`_fields_to_write()` method. In the example below, the `weight` field is not written, but is
optional to support reading the metric back in. Also, the `name` and `age` columns are written in
reverse order.
```python
>>> from fgpyo.util.metric import Metric
Expand All @@ -125,7 +126,7 @@
... age: int
... weight: Optional[int]
... @classmethod
... def _header(cls, field_types: Optional[List[FieldType]] = None) -> List[str]:
... def _fields_to_write(cls, field_types: List[FieldType] = None) -> List[str]:
... return ["age", "name"]
>>> person = Person(name="john", age=42, weight=180)
>>> person.header()
Expand Down Expand Up @@ -295,21 +296,19 @@ def header(cls) -> List[str]:
"""The list of header values for the metric."""
field_types = list(inspect.get_fields(cls)) # type: ignore[arg-type]
field_names = {field.name for field in field_types}
header = cls._header(field_types=field_types)
header = cls._fields_to_write(field_types=field_types)
extra_fields = [h for h in header if h not in field_names]
if len(extra_fields) > 0:
raise ValueError("header() returned extra fields: " + ", ".join(extra_fields))

Check warning on line 302 in fgpyo/util/metric.py

View check run for this annotation

Codecov / codecov/patch

fgpyo/util/metric.py#L302

Added line #L302 was not covered by tests
return header

@classmethod
def _header(cls, field_types: Optional[List[FieldType]] = None) -> List[str]:
def _fields_to_write(cls, field_types: List[FieldType]) -> List[str]:
"""Returns a list of field names for the header and values.
This method may be overridden to re-order or subset the columns written to file with
`write()` or returned by `values()`.
"""
if field_types is None:
field_types = list(inspect.get_fields(cls)) # type: ignore[arg-type]
return [a.name for a in field_types]

@classmethod
Expand Down
4 changes: 2 additions & 2 deletions tests/fgpyo/util/test_metric.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ class LastFirstMetric(Metric["LastFirstMetric"]):
last: str

@classmethod
def _header(cls, field_types: Optional[List[FieldType]] = None) -> List[str]:
def _fields_to_write(cls, field_types: Optional[List[FieldType]] = None) -> List[str]:
return ["last", "first"]

@make_dataclass(use_attr=use_attr)
Expand All @@ -137,7 +137,7 @@ class SubsetMetric(Metric["SubsetMetric"]):
hidden: Optional[str]

@classmethod
def _header(cls, field_types: Optional[List[FieldType]] = None) -> List[str]:
def _fields_to_write(cls, field_types: Optional[List[FieldType]] = None) -> List[str]:
return ["written"]

@make_dataclass(use_attr=use_attr)
Expand Down

0 comments on commit b06b986

Please sign in to comment.