Skip to content

⚡ Bolt: Pre-compile regex patterns in job_parser.py#276

Open
anchapin wants to merge 1 commit intomainfrom
bolt-optimize-job-parser-regex-11956294251020149341
Open

⚡ Bolt: Pre-compile regex patterns in job_parser.py#276
anchapin wants to merge 1 commit intomainfrom
bolt-optimize-job-parser-regex-11956294251020149341

Conversation

@anchapin
Copy link
Copy Markdown
Owner

@anchapin anchapin commented Apr 30, 2026

⚡ Bolt: Pre-compile regex patterns in job_parser.py

💡 What: Lifted repeatedly compiled regular expressions out of job_parser.py parsing methods and pre-compiled them as module-level constants.
🎯 Why: Instantiating re.compile within parsing methods (e.g. soup.find) or inside loops incurs redundant parsing overhead. Evaluated them exactly once instead.
📊 Impact: Decreases parsing time for large HTML documents.
🔬 Measurement: Run integration tests to ensure extraction remains regression-free.


PR created automatically by Jules for task 11956294251020149341 started by @anchapin

Summary by Sourcery

Pre-compile regular expression patterns in the job parser to reduce repeated compilation overhead during HTML parsing.

Enhancements:

  • Introduce module-level pre-compiled regex constants for headers, sections, bullets, salary, job type, and experience level extraction in job_parser.
  • Update text extraction helpers to accept pre-compiled regex patterns in addition to pattern strings, reusing compiled patterns across calls.

Documentation:

  • Extend the Bolt engineering notes with guidance on pre-compiling regexes in web scraping code paths.

💡 What: Lifted repeatedly compiled regular expressions out of `job_parser.py` parsing methods and pre-compiled them as module-level constants.
🎯 Why: Instantiating `re.compile` within parsing methods (e.g. `soup.find`) or inside loops incurs redundant parsing overhead. Evaluated them exactly once instead.
📊 Impact: Decreases parsing time for large HTML documents.
🔬 Measurement: Run integration tests to ensure extraction remains regression-free.

Co-authored-by: anchapin <[email protected]>
@google-labs-jules
Copy link
Copy Markdown
Contributor

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

@sourcery-ai
Copy link
Copy Markdown

sourcery-ai Bot commented Apr 30, 2026

Reviewer's Guide

Pre-compiles frequently used regular expressions in job_parser.py into module-level constants and updates parsing utilities to accept pre-compiled patterns, reducing redundant regex compilation in hot HTML parsing paths; also documents this optimization pattern in .jules/bolt.md.

Class diagram for updated JobParser regex handling

classDiagram
    class JobParser {
        +_parse_indeed(html: str) JobDetails
        +_parse_generic(html: str) JobDetails
        +_find_by_selectors(soup: BeautifulSoup, selectors: List[str]) Optional[Tag]
        +_extract_text_by_pattern(text: str, pattern: Union[str, re.Pattern]) Optional[str]
        +_extract_salary_from_text(text: str) Optional[str]
        +_extract_sections_from_description(description: str) Tuple[List[str], List[str]]
        +_extract_items_from_text(text: str) List[str]
        +_extract_list_items(element: Tag) List[str]
        +_extract_list_by_keyword(html: str, keyword: Union[str, re.Pattern]) List[str]
        +_extract_job_type(html: str) Optional[str]
        +_extract_experience_level(html: str) Optional[str]
    }

    class JobParserRegexPatterns {
        <<module_constants>>
        _INDEED_HEADER_PATTERN : re.Pattern
        _INDEED_TITLE_SUFFIX_PATTERN : re.Pattern
        _REQ_HEADING_PATTERN : re.Pattern
        _RESP_HEADING_PATTERN : re.Pattern
        _SALARY_PATTERNS : List[re.Pattern]
        _SALARY_CLEAN_PATTERN : re.Pattern
        _SALARY_K_PATTERN : re.Pattern
        _REQ_SECTION_PATTERN : re.Pattern
        _RESP_SECTION_PATTERN : re.Pattern
        _NEXT_SECTION_PATTERNS : List[re.Pattern]
        _BULLET_PATTERNS : List[re.Pattern]
        _COMMA_SEP_PATTERN : re.Pattern
        _JOB_TYPE_PATTERNS : List[re.Pattern]
        _EXPERIENCE_LEVEL_PATTERNS : List[re.Pattern]
    }

    JobParser --> JobParserRegexPatterns : uses precompiled patterns
Loading

File-Level Changes

Change Details Files
Pre-compile all repeatedly used regex patterns in job_parser.py as module-level constants and use them across parsing helpers.
  • Introduce named module-level regex constants for Indeed headers, title suffix stripping, requirement/responsibility headings, salaries, section headers, bullets, comma-separated lists, job type, and experience level.
  • Replace inline re.compile/re.search/re.sub calls in parsing logic with references to the pre-compiled regex constants, including BeautifulSoup lookups and text-processing helpers.
  • Annotate BeautifulSoup .find/.find_all calls that now receive pre-compiled patterns with type: ignore comments to appease type checking.
cli/integrations/job_parser.py
Generalize helper methods to accept pre-compiled regex objects in addition to pattern strings.
  • Update _extract_text_by_pattern to accept either a string or compiled regex (Union[str, re.Pattern]) and branch between re.search with flags vs pattern.search.
  • Update _extract_list_by_keyword to accept either a string or compiled regex, compiling strings to case-insensitive regexes and reusing compiled patterns for BeautifulSoup searches.
cli/integrations/job_parser.py
Document the regex pre-compilation optimization pattern for web scraping in the Bolt knowledge file.
  • Append a new learning entry describing the performance impact of pre-compiling regexes in web scraping/parsing hot paths and the recommended action to hoist them to module-level constants.
.jules/bolt.md

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Copy Markdown

@sourcery-ai sourcery-ai Bot left a comment

Choose a reason for hiding this comment

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

Hey - I've found 1 issue, and left some high level feedback:

  • For the helpers that accept regexes (e.g. _extract_text_by_pattern, _extract_list_by_keyword), consider typing the pattern argument as Union[str, Pattern[str]] instead of Union[str, re.Pattern] to play more nicely with type checkers and avoid the need for some of the type: ignore annotations when passing precompiled patterns into BeautifulSoup.
  • In _SALARY_PATTERNS, only some patterns use capturing groups while others rely on the full match; it may be clearer and less error-prone to either make all patterns use a consistent capturing convention (e.g. always capture the salary part) or to split them into two lists with explicit handling, so future edits don’t accidentally break the group indexing logic.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- For the helpers that accept regexes (e.g. `_extract_text_by_pattern`, `_extract_list_by_keyword`), consider typing the pattern argument as `Union[str, Pattern[str]]` instead of `Union[str, re.Pattern]` to play more nicely with type checkers and avoid the need for some of the `type: ignore` annotations when passing precompiled patterns into BeautifulSoup.
- In `_SALARY_PATTERNS`, only some patterns use capturing groups while others rely on the full match; it may be clearer and less error-prone to either make all patterns use a consistent capturing convention (e.g. always capture the salary part) or to split them into two lists with explicit handling, so future edits don’t accidentally break the group indexing logic.

## Individual Comments

### Comment 1
<location path="cli/integrations/job_parser.py" line_range="849" />
<code_context>
+        for pattern in _JOB_TYPE_PATTERNS:
+            match = pattern.search(html)
             if match:
                 return match.group(1).lower().replace("-", "-")

</code_context>
<issue_to_address>
**issue (bug_risk):** The `replace('-', '-')` call is a no-op and likely not doing the intended normalization.

Right now this only lowercases the job type; the `.replace('-', '-')` has no effect. If you meant to normalize spacing or hyphens, the second argument should differ (e.g. `.replace(' ', '-')` or `.replace('-', ' ')`). Please update to reflect the intended normalization.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

for pattern in _JOB_TYPE_PATTERNS:
match = pattern.search(html)
if match:
return match.group(1).lower().replace("-", "-")
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

issue (bug_risk): The replace('-', '-') call is a no-op and likely not doing the intended normalization.

Right now this only lowercases the job type; the .replace('-', '-') has no effect. If you meant to normalize spacing or hyphens, the second argument should differ (e.g. .replace(' ', '-') or .replace('-', ' ')). Please update to reflect the intended normalization.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant