Skip to content

Commit 8227cf6

Browse files
bosdqwencoder
andcommitted
Optimize _handle_create_error to eliminate unnecessary intermediate list creation\n\n- Refactor _handle_create_error to return the failed_line directly instead of wrapping it in a list\n- Remove redundant list() call when creating failed line\n- Update caller to use append() instead of extend() for better performance\n- Eliminate unnecessary intermediate list creation for better efficiency\n- Maintain all existing functionality while improving performance\n\nFixes the inefficiency noted in code review where the function was creating\nan unnecessary intermediate list and the caller was using extend() instead of append().
Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>
1 parent 2421b4a commit 8227cf6

File tree

1 file changed

+7
-9
lines changed

1 file changed

+7
-9
lines changed

src/odoo_data_flow/import_threaded.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ def _handle_create_error(
420420
create_error: Exception,
421421
line: list[Any],
422422
error_summary: str,
423-
) -> tuple[str, list[list[Any]], str]:
423+
) -> tuple[str, list[Any], str]:
424424
"""Handle errors during record creation.
425425
426426
Args:
@@ -430,9 +430,8 @@ def _handle_create_error(
430430
error_summary: Current error summary
431431
432432
Returns:
433-
Tuple of (error_message, failed_lines, error_summary)
433+
Tuple of (error_message, failed_line, error_summary)
434434
"""
435-
failed_lines = []
436435
error_str = str(create_error)
437436
error_str_lower = error_str.lower()
438437

@@ -450,9 +449,8 @@ def _handle_create_error(
450449
if "Fell back to create" in error_summary:
451450
error_summary = error_message
452451

453-
failed_line = [*list(line), error_message]
454-
failed_lines.append(failed_line)
455-
return error_message, failed_lines, error_summary
452+
failed_line = [*line, error_message]
453+
return error_message, failed_line, error_summary
456454

457455

458456
def _create_batch_individually(
@@ -509,15 +507,15 @@ def _create_batch_individually(
509507
id_map[source_id] = new_record.id
510508
except IndexError as e:
511509
error_message = f"Malformed row detected (row {i + 1} in batch): {e}"
512-
failed_lines.append([*list(line), error_message])
510+
failed_lines.append([*line, error_message])
513511
if "Fell back to create" in error_summary:
514512
error_summary = "Malformed CSV row detected"
515513
continue
516514
except Exception as create_error:
517-
error_message, new_failed_lines, error_summary = _handle_create_error(
515+
error_message, new_failed_line, error_summary = _handle_create_error(
518516
i, create_error, line, error_summary
519517
)
520-
failed_lines.extend(new_failed_lines)
518+
failed_lines.append(new_failed_line)
521519
return {
522520
"id_map": id_map,
523521
"failed_lines": failed_lines,

0 commit comments

Comments
 (0)