Commit 3a05157
authored
refactor: updated OpenMLEvaluation to use dataclass decorator (#1559)
I have Refactored the `OpenMLEvaluation` class from a traditional Python class to use the `@dataclass` decorator to reduce boilerplate code and improve code maintainability.
#### Metadata
* Reference Issue: #1540
* New Tests Added: No
* Documentation Updated: No
* Change Log Entry: Refactored the `OpenMLEvaluation` class to use the `@dataclass`
#### Details
Edited the `OpenMLEvaluation` class in `openml\evaluations\evaluation.py` to use `@dataclass` decorator. This significantly reduces the boilerplate code in the following places:
- Instance Variable Definitions
**Before:**
```python
def __init__(
self,
run_id: int,
task_id: int,
setup_id: int,
flow_id: int,
flow_name: str,
data_id: int,
data_name: str,
function: str,
upload_time: str,
uploader: int,
uploader_name: str,
value: float | None,
values: list[float] | None,
array_data: str | None = None,
):
self.run_id = run_id
self.task_id = task_id
self.setup_id = setup_id
self.flow_id = flow_id
self.flow_name = flow_name
self.data_id = data_id
self.data_name = data_name
self.function = function
self.upload_time = upload_time
self.uploader = uploader
self.uploader_name = uploader_name
self.value = value
self.values = values
self.array_data = array_data
```
**After:**
```python
run_id: int
task_id: int
setup_id: int
flow_id: int
flow_name: str
data_id: int
data_name: str
function: str
upload_time: str
uploader: int
uploader_name: str
value: float | None
values: list[float] | None
array_data: str | None = None
```
- _to_dict Method Simplification
**Before:**
```python
def _to_dict(self) -> dict:
return {
"run_id": self.run_id,
"task_id": self.task_id,
"setup_id": self.setup_id,
"flow_id": self.flow_id,
"flow_name": self.flow_name,
"data_id": self.data_id,
"data_name": self.data_name,
"function": self.function,
"upload_time": self.upload_time,
"uploader": self.uploader,
"uploader_name": self.uploader_name,
"value": self.value,
"values": self.values,
"array_data": self.array_data,
}
```
**After:**
```python
def _to_dict(self) -> dict:
return asdict(self)
```
All tests are passing with accordnce to the changes:
```bash
PS C:\Users\ASUS\Documents\work\opensource\openml-python> pytest tests/test_evaluations/
======================================= test session starts =======================================
platform win32 -- Python 3.14.0, pytest-9.0.2, pluggy-1.6.0
rootdir: C:\Users\ASUS\Documents\work\opensource\openml-python
configfile: pyproject.toml
plugins: anyio-4.12.0, flaky-3.8.1, asyncio-1.3.0, cov-7.0.0, mock-3.15.1, rerunfailures-16.1, timeout-2.4.0, xdist-3.8.0, requests-mock-1.12.1
asyncio: mode=Mode.STRICT, debug=False, asyncio_default_fixture_loop_scope=None, asyncio_default_test_loop_scope=function
collected 13 items
tests\test_evaluations\test_evaluation_functions.py ............ [ 92%]
tests\test_evaluations\test_evaluations_example.py . [100%]
================================= 13 passed in 274.80s (0:04:34) ==================================
```1 parent 8672ffb commit 3a05157
1 file changed
+21
-51
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | 3 | | |
| 4 | + | |
| 5 | + | |
4 | 6 | | |
5 | 7 | | |
6 | 8 | | |
7 | 9 | | |
8 | 10 | | |
9 | 11 | | |
10 | 12 | | |
11 | | - | |
12 | | - | |
| 13 | + | |
13 | 14 | | |
14 | 15 | | |
15 | 16 | | |
| |||
48 | 49 | | |
49 | 50 | | |
50 | 51 | | |
51 | | - | |
52 | | - | |
53 | | - | |
54 | | - | |
55 | | - | |
56 | | - | |
57 | | - | |
58 | | - | |
59 | | - | |
60 | | - | |
61 | | - | |
62 | | - | |
63 | | - | |
64 | | - | |
65 | | - | |
66 | | - | |
67 | | - | |
68 | | - | |
69 | | - | |
70 | | - | |
71 | | - | |
72 | | - | |
73 | | - | |
74 | | - | |
75 | | - | |
76 | | - | |
77 | | - | |
78 | | - | |
79 | | - | |
80 | | - | |
81 | | - | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
82 | 66 | | |
83 | 67 | | |
84 | | - | |
85 | | - | |
86 | | - | |
87 | | - | |
88 | | - | |
89 | | - | |
90 | | - | |
91 | | - | |
92 | | - | |
93 | | - | |
94 | | - | |
95 | | - | |
96 | | - | |
97 | | - | |
98 | | - | |
99 | | - | |
| 68 | + | |
100 | 69 | | |
101 | 70 | | |
102 | 71 | | |
| |||
119 | 88 | | |
120 | 89 | | |
121 | 90 | | |
122 | | - | |
| 91 | + | |
123 | 92 | | |
124 | 93 | | |
125 | 94 | | |
126 | | - | |
| 95 | + | |
| 96 | + | |
127 | 97 | | |
128 | 98 | | |
129 | 99 | | |
| |||
0 commit comments