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
8 changes: 6 additions & 2 deletions app/contest_cardnews/copy.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,14 @@ def is_contest_slide_empty(slide: dict[str, Any]) -> bool:
or bool(str(slide.get("headline") or "").strip())
)
if slide.get("items"):
return len([i for i in slide["items"] if str(i.get("text") or "").strip()]) == 0
return len([
i for i in slide["items"]
if (str(i.get("text") or "").strip() if isinstance(i, dict) else str(i).strip())
]) == 0
Comment on lines +140 to +143

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

이슈: None 값에 대한 오탐지 가능성 및 비효율적인 리스트 컴프리헨션 사용

  1. None 처리 시의 버그: 만약 slide['items']None 원소가 포함되어 있다면, isinstance(None, dict)False가 되어 str(None).strip()이 실행됩니다. 파이썬에서 str(None)"None"이라는 빈 문자열이 아닌 값(truthy)을 반환하므로, 실제로는 비어 있는 슬라이드임에도 비어 있지 않은 것으로 잘못 판단하게 됩니다.
  2. 성능 및 가독성: len([x for x in ...]) == 0 방식은 조건에 맞는 모든 요소를 메모리에 리스트로 생성하므로 비효율적입니다. not any(...)를 사용하면 조건에 맞는 요소를 찾는 즉시 단락 평가(short-circuiting)가 이루어져 성능상 유리하고 가독성도 좋습니다.

제안

not any(...)를 사용하고 i is not None 조건을 추가하여 안전하고 효율적으로 빈 항목 여부를 검사하도록 개선합니다.

Suggested change
return len([
i for i in slide["items"]
if (str(i.get("text") or "").strip() if isinstance(i, dict) else str(i).strip())
]) == 0
return not any(
str(i.get("text") or "").strip() if isinstance(i, dict) else str(i).strip()
for i in slide["items"]
if i is not None
)

return not bool(str(slide.get("body") or slide.get("headline") or "").strip())


def prepare_contest_slides(slides: list[dict[str, Any]]) -> list[dict[str, Any]]:
rows = compact_contest_deck(slides)
return [normalize_contest_slide_copy(s) for s in rows if not is_contest_slide_empty(s)]
normalized = [normalize_contest_slide_copy(s) for s in rows]
return [s for s in normalized if not is_contest_slide_empty(s)]
Loading