Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -226,9 +226,11 @@ class BaseLoopNode(ILoopNode):
def save_context(self, details, workflow_manage):
self.context['loop_context_data'] = details.get('loop_context_data')
self.context['loop_answer_data'] = details.get('loop_answer_data')
for key, value in details['context'].items():
if key not in self.context:
self.context[key] = value
self.context['result'] = details.get('result')
self.context['params'] = details.get('params')
self.context['run_time'] = details.get('run_time')
self.context['index'] = details.get('current_index')
self.context['item'] = details.get('current_item')
self.answer_text = ""

def get_answer_list(self) -> List[Answer] | None:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code provided looks mostly correct, but there are a few improvements and optimizations you can make:

  1. Consistent Key Name: The saved_context method has two different names (save_context and saved_con). You should use one consistent name throughout the class to avoid confusion.

  2. Duplicate Handling: When updating the context, it's better to ensure that you're not overwriting keys accidentally with default values from another dictionary if needed.

  3. Variable Naming: It might be more descriptive to rename some variables (e.g., loop_answer_data, context_data) to reflect their purpose more clearly.

  4. Comments: Adding comments could improve readability, especially for anyone reviewing this code in the future.

Here’s an optimized version of the code with these considerations:

class BaseLoopNode(ILoopNode):
    def save_context(self, details, workflow_manage):
        """Save loop context data."""
        # Clear existing context
        self.context.clear()
        
        # Update with specific details
        self.context['result'] = details.get('output')  # Using 'output' instead of 'result'
        self.context['params'] = details.get('parameters', {})  # Initialize empty params if absent
        self.context['run_time'] = details.get('execution_time')
        self.context['index'] = details.get('current_position', 0)  # Default to position 0 if missing
        self.context['item'] = details.get('current_object')

        # Optional: Save all other context items without duplicates
        for key, value in details.get('additional_context', {}).items():
            if key not in self.context:
                self.context[key] = value

    def get_answer_list(self) -> List[Answer] | None:
        # Implementation here

Key Changes Made:

  • Consistently used self.context.clear() at the beginning to clear previous context.
  • Replaced saved_context with save_context for clarity.
  • Renamed loop_answer_data to output and context_data to addtional_context.
  • Added parameter type hinting and default values where appropriate.
  • Used optional parameters with defaults to handle cases where certain fields may be missing.
  • Included comments explaining the purpose of each function and section of the code.

Expand Down
Loading