Skip to content
Snippets Groups Projects
Commit ded67dc2 authored by Florian Ziemen's avatar Florian Ziemen
Browse files

Moving to @dataclasses

parent 09d25b0b
No related branches found
No related tags found
1 merge request!2draft: Basic dataset
from collections.abc import Sequence
from dataclasses import dataclass
@dataclass
class dataset_location:
def __init__(
self,
method: str,
host: str,
path: str,
):
self.method = method
self.host = host
self.path = path
method: str
host: str
path: str
def is_valid(self) -> bool:
return all(x is not None for x in vars(self).values())
......@@ -19,15 +15,18 @@ class dataset_location:
return vars(self)
@dataclass
class dataset:
def __init__(
self, name: str, serve: bool, locations: Sequence[dataset_location], **kwargs
):
self.name = name
self.serve = serve
self.locations = set(locations)
for k, v in kwargs.items():
setattr(self, k, v)
name: str
serve: bool
locations: Sequence[dataset_location]
def __post_init__(self):
self.locations = list(self.locations)
if not all(isinstance(x, dataset_location) for x in self.locations):
raise TypeError(
f"Dateset locations must be of type dataset_location, found {[type(x) for x in self.locations]}"
)
def is_valid(self) -> bool:
return (
......
......@@ -39,8 +39,8 @@ class test_dataset(unittest.TestCase):
bad_location = dataset.dataset(name=None, serve=True, locations=(badpath,))
self.assertFalse(bad_location.is_valid())
bad_location = dataset.dataset(name=None, serve=True, locations=(None,))
self.assertFalse(bad_location.is_valid())
with self.assertRaises(TypeError):
bad_location = dataset.dataset(name=None, serve=True, locations=(None,))
if __name__ == "__main__":
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment