Skip to content

Commit 10fb41d

Browse files
committed
Enable creating attributes from attribute templates
implemented with the __call__ magic method. By default: - name is inherited - attribute is linked to the attribute template that created it
1 parent d5b0675 commit 10fb41d

File tree

6 files changed

+126
-0
lines changed

6 files changed

+126
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,28 @@
11
from taurus.entity.template.attribute_template import AttributeTemplate
2+
from taurus.entity.attribute.condition import Condition
23

34

45
class ConditionTemplate(AttributeTemplate):
56
"""A template for a condition attribute."""
67

78
typ = "condition_template"
9+
10+
def __call__(
11+
self, name=None, template=None, origin="unknown", value=None, notes=None, file_links=None
12+
):
13+
"""Produces a condition that is linked to this condition template."""
14+
15+
if not name: # inherit name from the template by default
16+
name = self.name
17+
18+
if not template: # link the condition to this template by default
19+
template = self
20+
21+
return Condition(
22+
name=name,
23+
template=template,
24+
origin=origin,
25+
value=value,
26+
notes=notes,
27+
file_links=file_links,
28+
)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,28 @@
11
from taurus.entity.template.attribute_template import AttributeTemplate
2+
from taurus.entity.attribute.parameter import Parameter
23

34

45
class ParameterTemplate(AttributeTemplate):
56
"""A template for the parameter attribute."""
67

78
typ = "parameter_template"
9+
10+
def __call__(
11+
self, name=None, template=None, origin="unknown", value=None, notes=None, file_links=None
12+
):
13+
"""Produces a parameter that is linked to this parameter template."""
14+
15+
if not name: # inherit name from the template by default
16+
name = self.name
17+
18+
if not template: # link the parameter to this template by default
19+
template = self
20+
21+
return Parameter(
22+
name=name,
23+
template=template,
24+
origin=origin,
25+
value=value,
26+
notes=notes,
27+
file_links=file_links,
28+
)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,28 @@
11
from taurus.entity.template.attribute_template import AttributeTemplate
2+
from taurus.entity.attribute.property import Property
23

34

45
class PropertyTemplate(AttributeTemplate):
56
"""A template for the property attribute."""
67

78
typ = "property_template"
9+
10+
def __call__(
11+
self, name=None, template=None, origin="unknown", value=None, notes=None, file_links=None
12+
):
13+
"""Produces a property that is linked to this template."""
14+
15+
if not name: # inherit name from the template by default
16+
name = self.name
17+
18+
if not template: # link the property to this template by default
19+
template = self
20+
21+
return Property(
22+
name=name,
23+
template=template,
24+
origin=origin,
25+
value=value,
26+
notes=notes,
27+
file_links=file_links,
28+
)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
"""Test of the condition template."""
2+
from taurus.entity.template.condition_template import ConditionTemplate
3+
from taurus.entity.bounds.categorical_bounds import CategoricalBounds
4+
from taurus.entity.value.nominal_categorical import NominalCategorical
5+
6+
7+
condition_template = ConditionTemplate(
8+
name="test_template", bounds=CategoricalBounds(["True", "False"])
9+
)
10+
11+
12+
def test_creating_condition():
13+
"""Test creating a condition from a condition template."""
14+
15+
condition = condition_template() # by default, inherit name and the template
16+
assert condition.name == condition_template.name
17+
assert condition.template is condition_template
18+
19+
condition = condition_template(name='other name', value=NominalCategorical('True'))
20+
assert condition.name != condition_template.name
21+
assert condition.name is not None
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
"""Test of the parameter template."""
2+
from taurus.entity.template.parameter_template import ParameterTemplate
3+
from taurus.entity.bounds.categorical_bounds import CategoricalBounds
4+
from taurus.entity.value.nominal_categorical import NominalCategorical
5+
6+
7+
parameter_template = ParameterTemplate(
8+
name="test_template", bounds=CategoricalBounds(["True", "False"])
9+
)
10+
11+
12+
def test_creating_parameter():
13+
"""Test creating a parameter from a parameter template."""
14+
15+
parameter = parameter_template() # by default, inherit name and the template
16+
assert parameter.name == parameter_template.name
17+
assert parameter.template is parameter_template
18+
19+
parameter = parameter_template(name='other name', value=NominalCategorical('True'))
20+
assert parameter.name != parameter_template.name
21+
assert parameter.name is not None
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
"""Test of the property template."""
2+
from taurus.entity.template.property_template import PropertyTemplate
3+
from taurus.entity.bounds.categorical_bounds import CategoricalBounds
4+
from taurus.entity.value.nominal_categorical import NominalCategorical
5+
6+
7+
property_template = PropertyTemplate(
8+
name="test_template", bounds=CategoricalBounds(["True", "False"])
9+
)
10+
11+
12+
def test_creating_property():
13+
"""Test creating a property from a property template."""
14+
15+
property = property_template() # by default, inherit name and the template
16+
assert property.name == property_template.name
17+
assert property.template is property_template
18+
19+
property = property_template(name='other name', value=NominalCategorical('True'))
20+
assert property.name != property_template.name
21+
assert property.name is not None

0 commit comments

Comments
 (0)