Skip to content

Commit 42f9fbf

Browse files
author
Hamish Downer
committed
Make it easier to (de)serialize RunReport
By making it, and the classes it depends upon, into attrs classes rather than dataclass. This means we can use cattrs to structure() and unstructure() it to/from a dict, and then to/from JSON, so we can store it more easily.
1 parent 59a3708 commit 42f9fbf

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

src/sortition_algorithms/utils.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
import sys
88
from abc import ABC, abstractmethod
99
from collections.abc import Generator, Iterable, Mapping, Sequence
10-
from dataclasses import dataclass
1110
from typing import TYPE_CHECKING, Any
1211

12+
from attrs import define, field
1313
from tabulate import tabulate
1414

1515
from sortition_algorithms import errors
@@ -82,30 +82,30 @@ class ReportLevel(enum.Enum):
8282
CRITICAL = 2
8383

8484

85-
@dataclass
85+
@define(slots=True)
8686
class RunLineLevel:
8787
line: str
8888
level: ReportLevel
8989
log_level: int = logging.NOTSET
9090

9191

92-
@dataclass
92+
@define(slots=True)
9393
class RunTable:
9494
headers: list[str]
9595
data: list[list[str | int | float]]
9696

9797

98-
@dataclass
98+
@define(slots=True)
9999
class RunError:
100100
error: Exception
101101
is_fatal: bool
102102

103103

104+
@define
104105
class RunReport:
105106
"""A class to hold a report to show to the user at the end"""
106107

107-
def __init__(self) -> None:
108-
self._data: list[RunLineLevel | RunTable | RunError] = []
108+
_data: list[RunLineLevel | RunTable | RunError] = field(factory=list)
109109

110110
def __bool__(self) -> bool:
111111
"""

0 commit comments

Comments
 (0)