Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Filter jobs with an optional parameter use_regex #4544

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
FrancescoDiMuro opened this issue Nov 1, 2024 · 5 comments · Fixed by #4613
Closed

Filter jobs with an optional parameter use_regex #4544

FrancescoDiMuro opened this issue Nov 1, 2024 · 5 comments · Fixed by #4613

Comments

@FrancescoDiMuro
Copy link

What kind of feature are you missing? Where do you notice a shortcoming of PTB?

I want be able to filter the jobs with a partial match of the job names, but the function get_jobs_by_name doesn't allow it, since it does an absolute comparison between the provided name and the job's name.

Describe the solution you'd like

It would be nice to be able to filter the jobs by their names with the support of regular expressions, so developers can use the same function (get_jobs_by_name), extending its functionality to find jobs matching the pattern in their names.

Describe alternatives you've considered

I considered to use another approach, like getting the list of jobs first, and then filter it with regular expressions, but that's redundant, since everytime this logic has to be applied, a filter step has to be inserted.

Additional context

I've already implemented the functionality in my project, and it seems that it's doing its job:

from re import compile # To put at the top of the module

def get_jobs_by_name(self, name: str, use_regex: bool = False) -> Tuple["Job[CCT]", ...]:
    """Returns a tuple of all *pending/scheduled* jobs with the given name that are currently
    in the :class:`JobQueue`.
        
    If :obj:`use_regex` parameter is set to :obj:`True`, re.search() function is used to look for
    a match with the provided :obj:`name` as pattern on the :obj:`job.name`.

    Returns:
        Tuple[:class:`Job`]: Tuple of all *pending* or *scheduled* jobs matching the name.
    """
    if use_regex:
        pattern = compile(str(name))
        return tuple(job for job in self.jobs() if pattern.search(job.name))
        
    return tuple(job for job in self.jobs() if job.name == name)

Since the new parameter use_regex is set to False as default, the behaviour of this function for actual uses doesn't change, therefore is not a breaking change.

@FrancescoDiMuro FrancescoDiMuro changed the title [FEATURE] Filter jobs with an optional parameter with_regex [FEATURE] Filter jobs with an optional parameter use_regex Nov 1, 2024
@Bibo-Joshi
Copy link
Member

Hi,
thanks for reaching out! Filterung jobs by regex sounds like a reasonable use case for me, so I'm open to a contribution 👍
Your proposed interface does work, however I'm not sure if it the most natural implementation. Having a new parameter use_regex modify how the name parameter is interpreted doesn't feel straight forward to me. Plus the _by_name part would then no longer be really true 😅

What do you instead think of the following:

  • add an optional paremeter pattern to jobs so that jobs() still returns all jobs but jobs(pattern) filters based on the regex
  • refactor get_jobs_by_name by making it call jobs(f"^{name}$")

The filtered search would then be available through jobs rather than get_jobs_by_name.

@FrancescoDiMuro
Copy link
Author

Good day @Bibo-Joshi !
Yeah, looking at the code of get_jobs_by_name in the actual implementation, and adding the parameter that I added, I actually thought that it could generate a little bit of (justified) confusion, due to the ambigue role of name.
Let me see how can I implement your suggestion, and I'll get back to you.
In the meanwhile, have a good day!

@FrancescoDiMuro
Copy link
Author

@Bibo-Joshi This is my proposition:

def jobs(self, name: str) -> Tuple["Job[CCT]", ...]:
    """Returns a tuple of all *scheduled* jobs that are currently in the :class:`JobQueue`,
    matching the :obj:`name` provided.

    Returns:
        Tuple[:class:`Job`]: Tuple of all *scheduled* jobs.
    """
    pattern = compile(str(name))
    return tuple(Job.from_aps_job(job) for job in self.scheduler.get_jobs() if pattern.search(job.name))

def get_jobs_by_name(self, name: str) -> Tuple["Job[CCT]", ...]:
    """Returns a tuple of all *pending/scheduled* jobs with the given name that are currently
    in the :class:`JobQueue`.
    Please note that the parameter :obj:`name` accepts regular expressions.

    Returns:
        Tuple[:class:`Job`]: Tuple of all *pending* or *scheduled* jobs matching the name.
    """
    return tuple(job for job in self.jobs(name=f"^{name}$"))

The parameter name added in the jobs method can't be optional, otherwise all jobs would be returned.
Being said so, with the suggestion you proposed, I managed to filter the jobs providing a regular expression in the name parameter of the _get_jobs_by_name", keeping the logic as it was before thanks to the anchors ^ and $ inserted respectively in the name parameter of the _jobs_method.
Tested a little bit around, and as long as the user is not providing any strange regular expression, the result would just be a no-match, as it was before.
Let me know if I can proceed with this new feature integration 🙂

@Bibo-Joshi
Copy link
Member

Hi, thanks for getting back. Three remarks that I see on first glance:

  1. The new argument of jobs should be named pattern as a regex pattern is what's expected. Moreover it could also accept re.Pattern objects in addition to strings.
  2. The new argument must be optional, otherwise it would be breaking backward compatibility. If no pattern is passed, jobs() should just return all jubs as it's currently done.
  3. To ensure that regex syntax within the name argument of get_jobs_by_name doesn't break the logic, you can use re.escape.

I suggest that you draft a PR with your edits so that we can do a proper review on the PR :) Please be sure to follow our contribution guide, especially the checklist

@Bibo-Joshi Bibo-Joshi changed the title [FEATURE] Filter jobs with an optional parameter use_regex Filter jobs with an optional parameter use_regex Nov 8, 2024
@Bibo-Joshi
Copy link
Member

FYI, I went ahead and openend #4613

@github-actions github-actions bot locked and limited conversation to collaborators Jan 10, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants