Skip to content

Commit

Permalink
Run python files through black (#610)
Browse files Browse the repository at this point in the history
# Description
Just simple black-formatting of python files.

## Type of change
- [x] Bug fix & code cleanup
- [ ] New feature
- [ ] Documentation update
- [ ] Test update

## Checklist for the reviewer
This checklist should be used as a help for the reviewer.

- [ ] Is the change limited to one issue?
- [ ] Does this PR close the issue?
- [ ] Is the code easy to read and understand?
- [ ] Do all new feature have an accompanying new test?
- [ ] Has the documentation been updated as necessary?
  • Loading branch information
jesper-friis authored Aug 21, 2023
2 parents 1f91078 + 5f68a39 commit 8bb1aa3
Show file tree
Hide file tree
Showing 43 changed files with 1,582 additions and 1,299 deletions.
46 changes: 27 additions & 19 deletions bindings/python/datamodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@
class DataModelError(dlite.DLiteError):
"""Raised if the datamodel is inconsistent."""


class MissingDimensionError(DataModelError):
"""Raised if a dimension referred to in a property is not defined."""


class UnusedDimensionError(DataModelError):
"""Raised if a dimension is not referred to in any property."""

Expand All @@ -38,7 +40,7 @@ def __init__(self, uri, schema=None, description=None):
schema = property(
lambda self: self._schema,
lambda self, schema: self._set_schema(schema),
doc='Meta-metadata for the datamodel.',
doc="Meta-metadata for the datamodel.",
)

def _set_schema(self, schema):
Expand All @@ -49,17 +51,17 @@ def _set_schema(self, schema):
elif isinstance(schema, str):
self._schema = dlite.get_instance(schema)
else:
TypeError('`schema` must be a string or a DLite metadata schema.')
TypeError("`schema` must be a string or a DLite metadata schema.")

def add_dimension(self, name, description):
"""Add dimension with given `name` and description to data model."""
if name in self.dimensions:
raise KeyError(f'A dimension named "{name}" already exists')
self.dimensions[name] = dlite.Dimension(name, description)


def add_property(self, name, type, shape=None, unit=None, description=None,
dims=None):
def add_property(
self, name, type, shape=None, unit=None, description=None, dims=None
):
"""Add property to data model.
Parameters:
Expand Down Expand Up @@ -91,14 +93,18 @@ def add_property(self, name, type, shape=None, unit=None, description=None,
)

def _get_dims_variables(self):
"""Returns a set of all dimension names referred to in property dims."""
"""Returns a set of all dimension names referred to in property shapes.
"""
names = set()
for prop in self.properties.values():
if prop.shape is not None:
for dim in prop.shape:
tree = ast.parse(dim)
names.update(node.id for node in ast.walk(tree)
if isinstance(node, ast.Name))
names.update(
node.id
for node in ast.walk(tree)
if isinstance(node, ast.Name)
)
return names

def get_missing_dimensions(self):
Expand All @@ -108,23 +114,24 @@ def get_missing_dimensions(self):
return self._get_dims_variables().difference(self.dimensions)

def get_unused_dimensions(self):
"""Returns a set of dimensions not referred to in any property shapes."""
"""Returns a set of dimensions not referred to in any property shapes.
"""
return set(self.dimensions).difference(self._get_dims_variables())

def validate(self):
"""Raises an exception if there are missing or unused dimensions."""
missing = self.get_missing_dimensions()
if missing:
raise MissingDimensionError(f'Missing dimensions: {missing}')
raise MissingDimensionError(f"Missing dimensions: {missing}")
unused = self.get_unused_dimensions()
if unused:
raise UnusedDimensionError(f'Unused dimensions: {unused}')
raise UnusedDimensionError(f"Unused dimensions: {unused}")

def get(self):
"""Returns a DLite Metadata created from the datamodel."""
self.validate()
dims = [len(self.dimensions), len(self.properties)]
if 'nrelations' in self.schema:
if "nrelations" in self.schema:
dims.append(len(self.relations))

# Hmm, there seems to be a bug when instantiating from schema.
Expand All @@ -134,15 +141,16 @@ def get(self):
# For now, lets assume that it is EntitySchema.
if self.schema.uri != dlite.ENTITY_SCHEMA:
raise NotImplementedError(
f'Currently only entity schema is supported')
f"Currently only entity schema is supported"
)

#meta = self.schema(dims, id=self.uri)
#meta.description = self.description
#meta['dimensions'] = list(self.dimensions.values())
#meta['properties'] = list(self.properties.values())
#if 'relations' in meta:
# meta = self.schema(dims, id=self.uri)
# meta.description = self.description
# meta['dimensions'] = list(self.dimensions.values())
# meta['properties'] = list(self.properties.values())
# if 'relations' in meta:
# meta['relations'] = self.relations
#return meta
# return meta

return dlite.Instance.create_metadata(
uri=self.uri,
Expand Down
Loading

0 comments on commit 8bb1aa3

Please sign in to comment.