From 86443d8317cddd2cc179e53d02b1425cd75399f6 Mon Sep 17 00:00:00 2001 From: Kauser Zulfiqar Date: Thu, 5 Dec 2024 13:25:14 +0100 Subject: [PATCH] path implementation added in the data --- micromagneticdata/data.py | 41 +++++++++++++++++++++++++++++---------- 1 file changed, 31 insertions(+), 10 deletions(-) diff --git a/micromagneticdata/data.py b/micromagneticdata/data.py index 5f797b8..829e93e 100644 --- a/micromagneticdata/data.py +++ b/micromagneticdata/data.py @@ -1,14 +1,13 @@ import glob import os +from pathlib import Path import ipywidgets import pandas as pd -import ubermagutil.typesystem as ts import micromagneticdata as md -@ts.typesystem(name=ts.Typed(expected_type=str), dirname=ts.Typed(expected_type=str)) class Data: """Computational magnetism data class. @@ -43,15 +42,37 @@ class Data: """ - def __init__(self, name, dirname="./"): - self.name = name - self.dirname = dirname - self.path = os.path.join(dirname, name) - + def __init__(self, name=None, dirname="./", path=None): + if path is not None: + if name is not None: + raise TypeError("No other arguments can be passed with `path`.") + self.path = path + else: + if name is None: + raise TypeError("`name` must be provided if `path` is not given.") + self.path = Path(dirname) / name if not os.path.exists(self.path): - msg = f"Directory {self.path=} cannot be found." + msg = f"Directory {self.path} cannot be found." raise OSError(msg) + @property + def path(self): + return self._path + + @path.setter + def path(self, path): + if not isinstance(path, (str, Path)): + raise TypeError(f"Unsupported {type(path)=}; expected str or pathlib.Path") + self._path = Path(path).absolute() + + @property + def name(self): + return self.path.name + + @property + def dirname(self): + return self.path.parent + def __repr__(self): """Representation string. @@ -74,7 +95,7 @@ def __repr__(self): Data(...) """ - return f"Data(name='{self.name}', dirname='{self.dirname}')" + return f"Data(path='{self.path}')" @property def info(self): @@ -169,7 +190,7 @@ def __getitem__(self, item): return md.Drive( name=self.name, number=range(self.n)[item], - dirname=self.dirname, + dirname=str(self.dirname), ) def __iter__(self):