Referencing components in code
1. Import Required Modules:
In this code, you are importing necessary modules for working with the numerous.image_tools
package, as well as the logging
module for debugging purposes.
from numerous.image_tools.job import NumerousBaseJob, ExitCode
from numerous.image_tools.app import run_job
import logging
2. Create a Logger:
A logger named "ReferencingComponents" is created using the logging
module. This logger will be used to print debugging information.
logger = logging.getLogger("ReferencingComponents")
3. Define a Custom Job Class:
A custom job class named NumerousJob
is defined. It inherits from NumerousBaseJob
, which is provided by the numerous.image_tools
package. The run_job
method is overridden to define the actual logic that the job will execute.
class NumerousJob(NumerousBaseJob):
def run_job(self) -> ExitCode:
components = self.system.components
# Iterate over the components in the system and access their parameters.
for component_name, component in components.items():
if component_name == "component1":
# Access the value of a parameter named "param1" for component1.
param1_value = component.constants["param1"]
# Now the value of param1_value can be used in the job logic.
...
return ExitCode.COMPLETED
4. Run the Example:
A function named run_example
is defined to run the job using the run_job
function from the numerous.image_tools.app
module. An instance of NumerousJob
is created and passed as an argument. Additionally, you specify the application name and the model folder.
def run_example():
run_job(
numerous_job=NumerousJob(),
appname="referencing-components",
model_folder=".",
)
5. Main Execution:
The if __name__ == "__main__":
block ensures that the code inside it is only executed when the script is run directly, not when it's imported as a module. In this block, you configure the logging level and call the run_example
function to execute the job.
if __name__ == "__main__":
logging.basicConfig(level=logging.DEBUG)
run_example()
Usage Guide:
- Replace
"component1"
with the actual name of the component you want to reference. - Access the value of a parameter using
component.constants["param1"]
. - Use the retrieved parameter value in the job's logic as needed.
- Execute the script to run the job and reference the component's parameter.
Remember to replace placeholders and customize the code according to your specific use case and requirements.