Skip to content

Commit

Permalink
path implementation added in the data
Browse files Browse the repository at this point in the history
  • Loading branch information
kzqureshi committed Dec 5, 2024
1 parent 0764650 commit 86443d8
Showing 1 changed file with 31 additions and 10 deletions.
41 changes: 31 additions & 10 deletions micromagneticdata/data.py
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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.
Expand All @@ -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):
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit 86443d8

Please sign in to comment.