Skip to content
This repository was archived by the owner on Apr 23, 2021. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion simphony/cuds/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from .abc_lattice import ABCLattice
from .abc_mesh import ABCMesh
from .abc_particles import ABCParticles
from .mesh import Mesh, Point, Element, Edge, Face, Cell
from .mesh import Mesh
from .mesh_items import Point, Element, Edge, Face, Cell
from .lattice import Lattice, LatticeNode
from .particles import Particles, Particle, Bond
from .model import CUDS
Expand Down
189 changes: 189 additions & 0 deletions simphony/cuds/abc_dataset.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
# -*- coding: utf-8 -*-
from abc import ABCMeta, abstractmethod


class ABCDataset(object):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DataSets must conform the the structore of CUBA.DATA_SET, i.e. they need uid, name, etc defined.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok then I probably used the wrong name. I just picked dataset but I am not sure about it.
What is the generic name for things like particles, mesh, and lattice?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DataSet!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you suggest that I add these as well, or that I should choose another name? This is just an interface to the generic interface, so I am not sure if we want to name it after the Dataset.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the name is fine. How about adding meta.DataSet as a parent to get the properties?

"""Abstract base class for a dataset.
"""
__metaclass__ = ABCMeta

@abstractmethod
def add(self, iterable): # pragma: no cover
"""Adds a set of objects from the provided iterable
to the dataset.

If any object has no uids, the dataset will generate a new
uid for it. If the object has already an uid, it won't add the
object if an object with the same type uid already exists.
If the user wants to replace an existing object in the container
there is an 'update' method for that purpose.

Parameters
----------
iterable : iterable of objects
the new set of objects that will be included in the container.

Returns
-------
uids : list of uuid.UUID
The uids of the added objects.

Raises
------
ValueError :
when there is an object with an uids that already exists
in the dataset.
"""

@abstractmethod
def update(self, iterable): # pragma: no cover
"""Updates a set of objects from the provided iterable.

Takes the uids of the objects and searches inside the dataset for
those objects. If the object exists, they are replaced in the
dataset. If any object doesn't exist, it will raise an exception.

Parameters
----------

iterable : iterable of objects
the objects that will be replaced.

Raises
------
ValueError :
If any object inside the iterable does not exist.
"""

@abstractmethod
def get(self, uid): # pragma: no cover
"""Returns a copy of the object with the 'uid' id.

Parameters
----------

uid : uuid.UUID
the uid of the object

Raises
------
KeyError :
when the object is not in the container.

Returns
-------
object :
A copy of the internally stored info.
"""

@abstractmethod
def remove(self, uids): # pragma: no cover
"""Remove the object with the provided uids from the container.

The uids inside the iterable should exists in the container. Otherwise
an exception will be raised.

Parameters
----------
uids : iterable of uuid.UUID
the uids of the objects to be removed.

Raises
------
KeyError :
If any object doesn't exist.
"""

@abstractmethod
def iter(self, uids=None, item_type=None): # pragma: no cover
"""Generator method for iterating over the objects of the container.

It can receive any kind of sequence of uids to iterate over
those concrete objects. If nothing is passed as parameter, it will
iterate over all the objects.

Parameters
----------
uids : iterable of uuid.UUID, optional
sequence containing the uids of the objects that will be
iterated. When the uids are provided, then the objects are
returned in the same order the uids are returned by the iterable.
If uids is None, then all objects are returned by the iterable
and there is no restriction on the order that they are returned.

Yields
------
object : Particle
The object item.

Raises
------
KeyError :
if any of the ids passed as parameters are not in the dataset.
"""

@abstractmethod
def has(self, uid): # pragma: no cover

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest to implement __contains__ instead of this.
https://docs.python.org/3/reference/datamodel.html#object.__contains__

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

However, the same applies to all the other pythonic container methods. Perhaps it's better to leave this the subject of another PR. (And I see we have has_something methods there)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well, if you do so, it's certainly possible, but at that point we should just reimplement the dict() interface... :)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! However, we have to attack the problem of being a dict or not soon...

"""Checks if an object with the given uid already exists
in the dataset.

Parameters
----------
uid : uuid.UUID
the uid of the object

Returns
-------
True if the uid is found, False otherwise.
"""

@abstractmethod
def has_type(self, item_type): # pragma: no cover
"""Checks if the specified CUDSItem type is present
in the dataset.

Parameters
----------
item_type : CUDSItem
The CUDSItem enum of the type of the items to return the count of.

Returns
-------
True if the type is present, False otherwise.
"""

@abstractmethod
def count_of(self, item_type): # pragma: no cover

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shell we implement __len__ beside this? Of course it will return the whole number of elements inside the dataset.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's possible, yes.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added

""" Return the count of item_type in the container.

Parameters
----------
item_type : CUDSItem
The CUDSItem enum of the type of the items to return the count of.

Returns
-------
count : int
The number of items of item_type in the dataset.

Raises
------
ValueError :
If the type of the item is not supported in the current
dataset.
"""

@abstractmethod
def __len__(self):
"""Returns the total number of items in the container.

Returns
-------
count : int
The number of items of item_type in the dataset.
"""

def __contains__(self, item):
"""Implements the `in` interface. Behaves as the has() method.
"""
return self.has(item)
Loading