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

basic dataset class

parent eded79cb
No related branches found
No related tags found
No related merge requests found
import utils
import logging
from collections.abc import Sequence
class dataset_location:
def __init__(
self,
method: str,
host: str,
path: str,
):
self.method = method
self.host = host
self.path = path
def is_valid(self) -> bool:
return all(x is not None for x in vars(self).values())
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)
def is_valid(self) -> bool:
return (
all(x is not None for x in vars(self).values())
and type(self.serve) is bool
and all(type(x) is dataset_location for x in self.locations)
and all(x.is_valid() for x in self.locations)
)
import dataset
import unittest
class test_dataset(unittest.TestCase):
def test_dataset(self):
dsl1 = dataset.dataset_location(method="direct", host="local", path="/scratch/")
ds = dataset.dataset(name="test_ds", serve=True, locations=(dsl1,))
self.assertTrue(ds.is_valid())
dsl2 = dataset.dataset_location(method="direct", host="local", path="/work/")
ds = dataset.dataset(name="test_ds", serve=True, locations=(dsl1, dsl2))
self.assertTrue(ds.is_valid())
def test_bad_dataset_location(self):
badhost = dataset.dataset_location(method="direct", host=None, path="/scratch/")
self.assertFalse(badhost.is_valid())
badpath = dataset.dataset_location(method="direct", host="local", path=None)
self.assertFalse(badpath.is_valid())
badmethod = dataset.dataset_location(method=None, host="local", path="/scratch")
self.assertFalse(badmethod.is_valid())
def test_bad_dataset(self):
dsl1 = dataset.dataset_location(method="direct", host="local", path="/scratch/")
bad_name = dataset.dataset(name=None, serve=True, locations=(dsl1,))
self.assertFalse(bad_name.is_valid())
bad_serve = dataset.dataset(name="test", serve=7, locations=(dsl1,))
self.assertFalse(bad_serve.is_valid())
with self.assertRaises(TypeError):
bad_location = dataset.dataset(name=None, serve=True, locations=dsl1)
badpath = dataset.dataset_location(method="direct", host="local", path=None)
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())
if __name__ == "__main__":
unittest.main()
from urllib.parse import urlparse
def check_url(url):
result = urlparse(url)
if result.scheme and result.netloc:
return True
else:
return False
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