Make filters and/or-able using bitwise operators.#411
Merged
Conversation
See associated PR for more info.
Merged
__call__ is scary looking for users wanted to create their own filters. Also allows us to put additional logic in __call__ if we want in the future.
# Conflicts: # telegram/ext/messagehandler.py # tests/test_filters.py
Also add tests with both & and |.
Closed
2 tasks
Also deprecates actually using a list.
jh0ker
previously requested changes
Oct 10, 2016
Member
jh0ker
left a comment
There was a problem hiding this comment.
Just a few nitpic documentation changes, otherwise LGTM 👍
telegram/ext/filters.py
Outdated
|
|
||
| And: | ||
|
|
||
| >>> (Filters.text & Filters.entity(MENTION) |
telegram/ext/filters.py
Outdated
|
|
||
| Also works with more than two filters: | ||
|
|
||
| >>> (Filters.text & (Filters.entity(URL |Filters.entity(TEXT_LINK)))) |
| class MergedFilter(BaseFilter): | ||
| """Represents a filter consisting of two other filters.""" | ||
|
|
||
| def __init__(self, base_filter, and_filter=None, or_filter=None): |
Member
There was a problem hiding this comment.
Please add a docstring indicating that and_filter and or_filter are mutually exclusive
Since and empty list cannot (in the future, currently only deprecated) be used.
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Would allow filters to be and'ed/or'ed.
Using syntax like this:
Currently this isn't too useful since the only filter that can be and'ed to any other is
forwarded, but that should all change with #409.The code currently requires all filters to be created as a class and then initiated before they can be used, which could be an inconvenience to the user if they want to create their own filters. To remedy this we could add a decorator like this:
That would allow users (and us) to make filters like this:
The reason I didn't immediately go with this, is that it might be confusing if someone (especially someone new to python) reading the code stumbles upon a dynamic class creator.
Thoughts?
Edit: After talking in dev group, the decorator has been declared too gross to look at :P