Skip to content

Commit

Permalink
feat: add asdict
Browse files Browse the repository at this point in the history
  • Loading branch information
msto committed May 6, 2024
1 parent dd82a34 commit 9638f31
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions fgpyo/util/metric.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@
"""

import dataclasses
from abc import ABC
from enum import Enum
from pathlib import Path
Expand All @@ -127,6 +128,8 @@
from typing import List
from typing import TypeVar

import attr

from fgpyo import io
from fgpyo.util import inspect

Expand Down Expand Up @@ -334,3 +337,14 @@ def fast_concat(*inputs: Path, output: Path) -> None:
io.write_lines(
path=output, lines_to_write=list(io.read_lines(input_path))[1:], append=True
)


def asdict(metric: Metric) -> dict[str, Any]:
"""Convert a Metric instance to a dictionary."""

if dataclasses.is_dataclass(metric):
return dataclasses.asdict(metric)
elif attr.has(metric):
return attr.asdict(metric)
else:
assert False, "Unreachable"

0 comments on commit 9638f31

Please sign in to comment.