|
8 | 8 | class CustomOperator(CustomOperatorBase): |
9 | 9 | @property |
10 | 10 | 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" |
12 | 13 |
|
13 | 14 | @property |
14 | 15 | 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 |
15 | 26 | spec = CustomSpecification() |
| 27 | + # Set the description of the operator |
16 | 28 | spec.description = "What the Operator does. You can use MarkDown and LaTeX in descriptions." |
| 29 | + # Define the inputs of the operator if any |
17 | 30 | 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], |
19 | 32 | document="Describe input pin 0."), |
20 | 33 | } |
| 34 | + # Define the outputs of the operator if any |
21 | 35 | 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."), |
23 | 37 | } |
| 38 | + # Define the properties of the operator if any |
24 | 39 | 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 |
28 | 43 | ) |
29 | 44 | # Set the changelog of the operator to track changes |
30 | 45 | spec.set_changelog(Changelog() |
31 | | - .expect_version("0.0.0") |
32 | 46 | .patch_bump("Describe a patch bump.") |
33 | 47 | .major_bump("Describe a major bump.") |
34 | 48 | .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 |
36 | 50 | ) |
37 | 51 | return spec |
38 | 52 |
|
39 | 53 | 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 |
41 | 70 | 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 |
45 | 85 | self.set_succeeded() |
0 commit comments