Skip to content

Commit

Permalink
Update to PEP8
Browse files Browse the repository at this point in the history
  • Loading branch information
ronald-jaepel committed Mar 22, 2024
1 parent 540cef6 commit 39334c9
Show file tree
Hide file tree
Showing 5 changed files with 277 additions and 137 deletions.
2 changes: 1 addition & 1 deletion cadet/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

__version__ = "0.14.1"

from .cadet import H5
from .cadet import Cadet
from .cadet import H5
44 changes: 27 additions & 17 deletions cadet/cadet.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import warnings

from addict import Dict

import warnings
with warnings.catch_warnings():
warnings.filterwarnings("ignore",category=FutureWarning)
warnings.filterwarnings("ignore", category=FutureWarning)
import h5py
import numpy
import subprocess
Expand All @@ -17,6 +18,7 @@

from cadet.cadet_dll import CadetDLL


class H5():
pp = pprint.PrettyPrinter(indent=4)

Expand Down Expand Up @@ -117,10 +119,12 @@ def __setitem__(self, key, value):
obj = obj[i]
obj[parts[-1]] = value


def is_dll(value):
suffix = Path(value).suffix
return suffix in {'.so', '.dll'}


class CadetMeta(type):
_cadet_runner_class = None
_is_file_class = None
Expand All @@ -144,7 +148,7 @@ def __init__(cls):
del cls._cadet_runner_class

if is_dll(value):
cls._cadet_runner_class = CadetDLL(value)
cls._cadet_runner_class = CadetDLL(value)
cls._is_file_class = False
else:
cls._cadet_runner_class = CadetFile(value)
Expand All @@ -154,8 +158,9 @@ def __init__(cls):
def cadet_path(cls):
del cls._cadet_runner_class


class Cadet(H5, metaclass=CadetMeta):
#cadet_path must be set in order for simulations to run
# cadet_path must be set in order for simulations to run
def __init__(self, *data):
super().__init__(*data)
self._cadet_runner = None
Expand Down Expand Up @@ -188,7 +193,7 @@ def cadet_path(self, value):
del self._cadet_runner

if is_dll(value):
self._cadet_runner = CadetDLL(value)
self._cadet_runner = CadetDLL(value)
self._is_file = False
else:
self._cadet_runner = CadetFile(value)
Expand All @@ -209,14 +214,14 @@ def load_results(self):
if runner is not None:
runner.load_results(self)

def run(self, timeout = None, check=None):
def run(self, timeout=None, check=None):
data = self.cadet_runner.run(simulation=self.root.input, filename=self.filename, timeout=timeout, check=check)
#self.return_information = data
# self.return_information = data
return data

def run_load(self, timeout = None, check=None, clear=True):
def run_load(self, timeout=None, check=None, clear=True):
data = self.cadet_runner.run(simulation=self.root.input, filename=self.filename, timeout=timeout, check=check)
#self.return_information = data
# self.return_information = data
self.load_results()
if clear:
self.clear()
Expand All @@ -227,14 +232,15 @@ def clear(self):
if runner is not None:
runner.clear()


class CadetFile:

def __init__(self, cadet_path):
self.cadet_path = cadet_path

def run(self, filename = None, simulation=None, timeout = None, check=None):
def run(self, filename=None, simulation=None, timeout=None, check=None):
if filename is not None:
data = subprocess.run([self.cadet_path, filename], timeout = timeout, check=check, capture_output=True)
data = subprocess.run([self.cadet_path, filename], timeout=timeout, check=check, capture_output=True)
return data
else:
print("Filename must be set before run can be used")
Expand All @@ -245,9 +251,10 @@ def clear(self):
def load_results(self, sim):
sim.load(paths=["/meta", "/output"], update=True)


def convert_from_numpy(data, func):
ans = Dict()
for key_original,item in data.items():
for key_original, item in data.items():
key = func(key_original)
if isinstance(item, numpy.ndarray):
item = item.tolist()
Expand All @@ -264,18 +271,20 @@ def convert_from_numpy(data, func):
ans[key] = item
return ans

def recursively_load_dict( data, func):

def recursively_load_dict(data, func):
ans = Dict()
for key_original,item in data.items():
for key_original, item in data.items():
key = func(key_original)
if isinstance(item, dict):
ans[key] = recursively_load_dict(item, func)
else:
ans[key] = item
return ans


def set_path(obj, path, value):
"paths need to be broken up so that subobjects are correctly made"
"""paths need to be broken up so that subobjects are correctly made"""
path = path.split('/')
path = [i for i in path if i]

Expand All @@ -285,7 +294,8 @@ def set_path(obj, path, value):

temp[path[-1]] = value

def recursively_load( h5file, path, func, paths):

def recursively_load(h5file, path, func, paths):
ans = Dict()
if paths is not None:
for path in paths:
Expand All @@ -306,8 +316,8 @@ def recursively_load( h5file, path, func, paths):
ans[key] = recursively_load(h5file, local_path + '/', func, None)
return ans

def recursively_save(h5file, path, dic, func):

def recursively_save(h5file, path, dic, func):
if not isinstance(path, str):
raise ValueError("path must be a string")
if not isinstance(h5file, h5py._hl.files.File):
Expand Down
Loading

0 comments on commit 39334c9

Please sign in to comment.