This repository has been archived by the owner on Apr 23, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #237 from simphony/feature-material
Feature material
- Loading branch information
Showing
26 changed files
with
380 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
@startuml | ||
class Material { | ||
uid: uuid.UUID | ||
data: DataContainer | ||
description: str | ||
-- classmethods -- | ||
from_material(material: Material): Material {copy} | ||
} | ||
@enduml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
@startuml | ||
!include materials.txt | ||
!include material.txt | ||
@enduml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
@startuml | ||
class Materials { | ||
add_material(Material) | ||
get_material(uid: uuid.UUID): Materials | ||
update_material(material: Material) | ||
iter_materials(uids: iterable of uuid.UUID {optional}): iterator of Materials {unordered} | ||
} | ||
@enduml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import uuid | ||
|
||
from simphony.core.data_container import DataContainer | ||
|
||
|
||
class Material(object): | ||
"""A class that represents a material. | ||
Class describes a material and its data. | ||
Attributes | ||
---------- | ||
uid : uuid.UUID | ||
the id of the material. If None, then it is generated | ||
data : DataContainer | ||
data associated with this material | ||
description : str | ||
textual description of the material | ||
""" | ||
def __init__(self, uid=None, data=None, description=""): | ||
self.data = data if data else DataContainer() | ||
self.uid = uid if uid else uuid.uuid4() | ||
self.description = description | ||
|
||
@classmethod | ||
def from_material(cls, material): | ||
return cls( | ||
uid=material.uid, | ||
description=material.description, | ||
data=DataContainer(material.data)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
from simphony.cuds.material import Material | ||
|
||
|
||
class Materials(object): | ||
"""A class that contains materials. | ||
Class manages the materials. | ||
""" | ||
def __init__(self): | ||
self._materials = {} | ||
|
||
def add_material(self, material): | ||
"""Add a material | ||
Parameters | ||
---------- | ||
material : Material | ||
the material to be added. | ||
Raises | ||
------ | ||
ValueError: | ||
If a material with the same uid already exists | ||
""" | ||
try: | ||
self._materials[material.uid] = Material.from_material(material) | ||
except KeyError: | ||
raise ValueError | ||
|
||
def remove_material(self, uid): | ||
""" Remove a material | ||
Parameters | ||
---------- | ||
uid: uuid.UUID | ||
the id of the material to be deleted | ||
Raises | ||
------ | ||
KeyError: | ||
If there is no material with the given uid | ||
""" | ||
del self._materials[uid] | ||
|
||
def update_material(self, material): | ||
""" Update a material | ||
Parameters | ||
---------- | ||
material : Material | ||
the material to be updated. | ||
Raises | ||
------ | ||
ValueError: | ||
If a material with this uid does not exist | ||
""" | ||
self._materials[material.uid] = Material.from_material(material) | ||
|
||
def get_material(self, uid): | ||
""" Get the material | ||
Parameters | ||
---------- | ||
uid: uuid.UUID | ||
the id of the material to be retrieved. | ||
Returns | ||
------- | ||
material | ||
Raises | ||
------ | ||
KeyError: | ||
If there is no material with the given uid | ||
""" | ||
return Material.from_material(self._materials[uid]) | ||
|
||
def iter_materials(self, uids=None): | ||
""" Returns an iterator over a subset or all of the materials. | ||
Parameters | ||
---------- | ||
uids : sequence of uids, optional | ||
uids of specific material to be iterated over. If uids is not | ||
given, then all materials will be iterated over. | ||
""" | ||
if uids is None: | ||
for material in self._materials.itervalues(): | ||
yield material | ||
else: | ||
for uid in uids: | ||
yield self.get_material(uid) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import unittest | ||
import uuid | ||
from functools import partial | ||
|
||
from simphony.cuds.materials import Materials | ||
from simphony.cuds.material import Material | ||
from simphony.testing.utils import (compare_material, | ||
create_data_container) | ||
|
||
|
||
class TestMaterials(unittest.TestCase): | ||
"""Test case for Materials class.""" | ||
|
||
def setUp(self): | ||
self.addTypeEqualityFunc(Material, | ||
partial(compare_material, testcase=self)) | ||
self.materials = Materials() | ||
self.example_materials = [] | ||
for i in xrange(5): | ||
self.example_materials.append( | ||
Material(description="Material {}".format(i), | ||
data=create_data_container())) | ||
|
||
def test_add_get_material(self): | ||
self.materials.add_material(self.example_materials[0]) | ||
self.assertEqual(self.materials.get_material( | ||
self.example_materials[0].uid), | ||
self.example_materials[0]) | ||
|
||
def test_get_missing_material(self): | ||
with self.assertRaises(KeyError): | ||
self.materials.get_material(uuid.uuid4()) | ||
|
||
def test_remove_missing_material(self): | ||
with self.assertRaises(KeyError): | ||
self.materials.remove_material(uuid.uuid4()) | ||
|
||
def test_iter_materials_with_ids(self): | ||
# given | ||
material_subset = [material for material in self.example_materials[:2]] | ||
subset_ids = [material.uid for material in material_subset] | ||
for material in self.example_materials: | ||
self.materials.add_material(material) | ||
|
||
# when | ||
iterated_with_id_materials = [ | ||
material for material in self.materials.iter_materials( | ||
subset_ids)] | ||
|
||
# then | ||
for m, r in zip(iterated_with_id_materials, material_subset): | ||
self.assertEqual(m, r) | ||
|
||
# when | ||
iterated_all_materials = {material.uid: material for material | ||
in self.materials.iter_materials()} | ||
|
||
# then | ||
self.assertEqual(len(iterated_all_materials), | ||
len(self.example_materials)) | ||
for material in self.example_materials: | ||
self.assertEqual(material, iterated_all_materials[material.uid]) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.