#!/usr/bin/env python3 """ Convert to Percent Node This node takes an input string formatted as "numerator/denom" (e.g., "50/50"), splits it into two numbers, computes (numerator / denom) * 100, and outputs the result as a float formatted to 4 decimal places. If an error occurs, an error message is stored. The node's title is always "Convert to Percent". """ from NodeGraphQt import BaseNode from Qt import QtCore class ConvertToPercentNode(BaseNode): __identifier__ = 'io.github.nicole.convert' NODE_NAME = 'Convert to Percent' def __init__(self): super(ConvertToPercentNode, self).__init__() # Add one input port (expects a string in "numerator/denom" format). self.add_input("in") # Add one output port. self.add_output("Percent") # Initialize internal value. self.value = "No Input" # Set the node title to a static string. self.set_name(self.NODE_NAME) # Initialize a values dictionary so that connected Display nodes can read output. self.values = {} def process_input(self): input_port = self.input(0) connected_ports = input_port.connected_ports() if input_port is not None else [] if connected_ports: connected_output = connected_ports[0] parent_node = connected_output.node() port_name = connected_output.name() # Use parent's values dictionary if available, else use its value attribute. if hasattr(parent_node, 'values') and isinstance(parent_node.values, dict): input_value = parent_node.values.get(port_name, "") else: input_value = getattr(parent_node, 'value', "") input_str = str(input_value).strip() try: parts = input_str.split('/') if len(parts) != 2: raise ValueError("Input must be in the format 'num/denom'") numerator = float(parts[0].strip()) denominator = float(parts[1].strip()) if denominator == 0: raise ZeroDivisionError("Division by zero") percent = (numerator / denominator) * 100 formatted_percent = f"{percent:.4f}" self.value = formatted_percent except Exception as e: self.value = f"Error: {e}" else: self.value = "No Input" # Always keep the title static. self.set_name(self.NODE_NAME) # Store the computed value in the values dictionary under the output port key. self.values["Percent"] = self.value