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
7 changes: 6 additions & 1 deletion apps/common/utils/split_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,12 @@ def parse_level(text, pattern: str):


def re_findall(pattern, text):
if not pattern or not pattern.strip():
# 检查 pattern 是否为空或无效
if pattern is None:
return []

# 如果是字符串类型,检查是否为空字符串
if isinstance(pattern, str) and (not pattern or not pattern.strip()):
return []

try:
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 function re_findall has several improvements to enhance readability and robustness:

  1. Early Return on Invalid Pattern:
    • Added a check for pattern is None to handle cases where the pattern might be explicitly set to None.
  2. Check for Empty String Patterns:
    • Changed the condition inside the main pattern check from using not pattern.strip() to just (not pattern). This makes it more concise.

Here's the revised version of the code with these changes:

import re

def parse_level(text, pattern: str):
    pass  # Placeholder implementation


def re_findall(pattern, text):
    # Early return if no pattern or invalid pattern is provided
    if pattern is None:
        return []

    # Check if pattern is an empty string
    if pattern == "":
        return []

    try:
        return re.findall(pattern, text)
    except Exception as e:
        print(f"Error parsing regex {pattern}: {e}")
        return []

Key Changes:

  • Pattern Null Check: Ensures that the function returns an empty list if the pattern is not provided or is explicitly set to None.
  • Empty String Check: Adds a simple comparison to check if the pattern is an empty string.

These changes make the function more defensive and easier to understand, especially when dealing with unexpected input values.

Expand Down
Loading