Skip to content

Commit 2993f24

Browse files
committed
Update custom_operator_example.py
1 parent c7f3474 commit 2993f24

File tree

1 file changed

+52
-12
lines changed

1 file changed

+52
-12
lines changed

doc/source/user_guide/tutorials/custom_operators_and_plugins/custom_operator_example.py

Lines changed: 52 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,38 +8,78 @@
88
class CustomOperator(CustomOperatorBase):
99
@property
1010
def name(self):
11-
return "name_of_my_custom_operator"
11+
"""Return the scripting name of the operator in Snake Case."""
12+
return "my_custom_operator"
1213

1314
@property
1415
def specification(self) -> CustomSpecification:
16+
"""Create the specification of the custom operator.
17+
18+
The specification declares:
19+
- the description of the operator
20+
- the inputs of the operator
21+
- the outputs of the operator
22+
- the properties of the operator (a username, a category, a required license)
23+
- the changelog of the operator (starting with DPF 2026 R1)
24+
"""
25+
# Instantiate the custom specification
1526
spec = CustomSpecification()
27+
# Set the description of the operator
1628
spec.description = "What the Operator does. You can use MarkDown and LaTeX in descriptions."
29+
# Define the inputs of the operator if any
1730
spec.inputs = {
18-
0: PinSpecification(name="name_of_pin_0", type_names=[dpf.Field, dpf.FieldsContainer],
31+
0: PinSpecification(name="name_of_input_0", type_names=[dpf.Field, dpf.FieldsContainer],
1932
document="Describe input pin 0."),
2033
}
34+
# Define the outputs of the operator if any
2135
spec.outputs = {
22-
0: PinSpecification(name="name_of_pin_0", type_names=[dpf.Field], document="Describe output pin 0."),
36+
0: PinSpecification(name="name_of_output_0", type_names=[dpf.Field], document="Describe output pin 0."),
2337
}
38+
# Define the properties of the operator if any
2439
spec.properties = SpecificationProperties(
25-
user_name="user name",
26-
category="category",
27-
license="license",
40+
user_name="my custom operator", # Optional, defaults to the scripting name with spaces
41+
category="my_category", # Optional, defaults to 'other'
42+
license="my_license", # Optional, defaults to None
2843
)
2944
# Set the changelog of the operator to track changes
3045
spec.set_changelog(Changelog()
31-
.expect_version("0.0.0")
3246
.patch_bump("Describe a patch bump.")
3347
.major_bump("Describe a major bump.")
3448
.minor_bump("Describe a minor bump.")
35-
.expect_version("1.1.0")
49+
.expect_version("1.1.0") # Checks the resulting version is as expected
3650
)
3751
return spec
3852

3953
def run(self):
40-
field = self.get_input(0, dpf.Field)
54+
"""Run the operator and execute the logic implemented here.
55+
56+
This method defines the behavior of the operator.
57+
58+
Request the inputs with the method ``get_input``,
59+
perform operations on the data,
60+
then set the outputs with the method ``set_output``,
61+
and finally call ``set_succeeded``.
62+
63+
In this example, the operator changes the name of a Field.
64+
65+
"""
66+
# First get the field in input by calling get_input for the different types supported
67+
# # Try requesting the input as a Field
68+
field: dpf.Field = self.get_input(0, dpf.Field)
69+
# # If function returns None, there is no Field connected to this input
4170
if field is None:
42-
field = self.get_input(0, dpf.FieldsContainer)[0]
43-
# compute data
44-
self.set_output(0, dpf.Field())
71+
# # Try requesting the input as a FieldsContainer
72+
field: dpf.FieldsContainer = self.get_input(0, dpf.FieldsContainer).get_field(0)
73+
# # If the input is optional, set its default value
74+
# # If the input is not optional and empty, raise an error
75+
if field is None:
76+
raise ValueError("my_custom_operator: mandatory input name_of_input_0 is empty or of an unsupported type.")
77+
78+
# Perform some operations on the data
79+
field.name = "new_field_name"
80+
81+
# Set the output of the operator
82+
self.set_output(0, field)
83+
84+
# And declare the operator run a success
4585
self.set_succeeded()

0 commit comments

Comments
 (0)