Skip to content

Commit

Permalink
Trivial code simplifications and cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
WilliamJamieson committed Aug 22, 2023
1 parent 4ed3443 commit 6e4ec5a
Showing 1 changed file with 16 additions and 32 deletions.
48 changes: 16 additions & 32 deletions src/roman_datamodels/datamodels/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,27 +105,23 @@ def __init__(self, init=None, **kwargs):
raise OSError("Argument does not appear to be an ASDF file or TaggedObjectNode.")
self._asdf = asdffile

def check_type(self, asdffile_instance):
def check_type(self, asdf_file):
"""
Subclass is expected to check for proper type of node
"""
if "roman" not in asdffile_instance.tree:
if "roman" not in asdf_file.tree:
raise ValueError('ASDF file does not have expected "roman" attribute')
topnode = asdffile_instance.tree["roman"]
if MODEL_REGISTRY[topnode.__class__] != self.__class__:
return False
return True

return MODEL_REGISTRY[asdf_file.tree["roman"].__class__] == self.__class__

@property
def schema_uri(self):
# Determine the schema corresponding to this model's tag
schema_uri = next(t for t in stnode.NODE_EXTENSIONS[0].tags if t.tag_uri == self._instance._tag).schema_uris[0]
return schema_uri
return next(t for t in stnode.NODE_EXTENSIONS[0].tags if t.tag_uri == self._instance._tag).schema_uris[0]

def close(self):
if not self._iscopy:
if self._asdf is not None:
self._asdf.close()
if not (self._iscopy or self._asdf is None):
self._asdf.close()

def __enter__(self):
return self
Expand All @@ -147,15 +143,13 @@ def copy(self, deepcopy=True, memo=None):
@staticmethod
def clone(target, source, deepcopy=False, memo=None):
if deepcopy:
instance = copy.deepcopy(source._instance, memo=memo)
target._asdf = source._asdf.copy()
target._instance = instance
target._iscopy = True
target._instance = copy.deepcopy(source._instance, memo=memo)
else:
target._asdf = source._asdf
target._instance = source._instance
target._iscopy = True

target._iscopy = True
target._files_to_close = []
target._shape = source._shape
target._ctx = target
Expand Down Expand Up @@ -183,11 +177,7 @@ def save(self, path, dir_path=None, *args, **kwargs):

def open_asdf(self, init=None, **kwargs):
with validate.nuke_validation():
if isinstance(init, str):
asdffile = asdf.open(init, **kwargs)
else:
asdffile = asdf.AsdfFile(init, **kwargs)
return asdffile
return asdf.open(init, **kwargs) if isinstance(init, str) else asdf.AsdfFile(init, **kwargs)

def to_asdf(self, init, *args, **kwargs):
with validate.nuke_validation():
Expand All @@ -202,19 +192,15 @@ def get_primary_array_name(self):
This is intended to be overridden in the subclasses if the
primary array's name is not "data".
"""
if hasattr(self, "data"):
primary_array_name = "data"
else:
primary_array_name = ""
return primary_array_name
return "data" if hasattr(self, "data") else ""

@property
def override_handle(self):
"""override_handle identifies in-memory models where a filepath
would normally be used.
"""
# Arbitrary choice to look something like crds://
return "override://" + self.__class__.__name__
return f"override://{self.__class__.__name__}"

@property
def shape(self):
Expand Down Expand Up @@ -266,10 +252,9 @@ def convert_val(val):
return str(val)
return val

if include_arrays:
return {"roman." + key: convert_val(val) for (key, val) in self.items()}
else:
return {"roman." + key: convert_val(val) for (key, val) in self.items() if not isinstance(val, np.ndarray)}
return {
f"roman.{key}": convert_val(val) for (key, val) in self.items() if include_arrays or not isinstance(val, np.ndarray)
}

def items(self):
"""
Expand Down Expand Up @@ -305,12 +290,11 @@ def get_crds_parameters(self):
-------
dict
"""
crds_header = {
return {
key: val
for key, val in self.to_flat_dict(include_arrays=False).items()
if isinstance(val, (str, int, float, complex, bool))
}
return crds_header

def validate(self):
"""
Expand Down

0 comments on commit 6e4ec5a

Please sign in to comment.