Skip to content

Make filters and/or-able using bitwise operators. #411

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

Merged
merged 14 commits into from
Oct 19, 2016
Merged

Conversation

jsmnbom
Copy link
Member

@jsmnbom jsmnbom commented Sep 14, 2016

Would allow filters to be and'ed/or'ed.
Using syntax like this:

handler = MessageHandler((Filters.text | Filters.venue) & Filters.forwarded, callback)

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:

def Filter(f):
    def wrapper(self, message):
        return f(message)
    return type(f.__name__, (BaseFilter,), {'__call__': wrapper})()

That would allow users (and us) to make filters like this:

@Filter
def Text(self, message):
    return bool(message.text and not message.text.startswith('/'))

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?

  • Figure out implementation
  • Write the code
  • Write the docs
  • Add snippet to wiki
  • Wait for Add entities filter #409 to get merged?

Edit: After talking in dev group, the decorator has been declared too gross to look at :P

See associated PR for more info.
@jh0ker jh0ker added this to the 5.1 milestone Sep 20, 2016
@jh0ker jh0ker removed this from the 5.1 milestone Sep 20, 2016
@jh0ker jh0ker mentioned this pull request Sep 20, 2016
__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 |.
@jsmnbom jsmnbom added the 📋 pending-review work status: pending-review label Sep 25, 2016
Also deprecates actually using a list.
jh0ker
jh0ker previously requested changes Oct 10, 2016
Copy link
Member

@jh0ker jh0ker left a comment

Choose a reason for hiding this comment

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

Just a few nitpic documentation changes, otherwise LGTM 👍


And:

>>> (Filters.text & Filters.entity(MENTION)
Copy link
Member

Choose a reason for hiding this comment

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

missed a ) here


Also works with more than two filters:

>>> (Filters.text & (Filters.entity(URL |Filters.entity(TEXT_LINK))))
Copy link
Member

Choose a reason for hiding this comment

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

this also looks wrong to me

class MergedFilter(BaseFilter):
"""Represents a filter consisting of two other filters."""

def __init__(self, base_filter, and_filter=None, or_filter=None):
Copy link
Member

Choose a reason for hiding this comment

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

Please add a docstring indicating that and_filter and or_filter are mutually exclusive

@jsmnbom jsmnbom dismissed jh0ker’s stale review October 15, 2016 20:27

Changes have been adressed.

@jsmnbom jsmnbom merged commit 4e5f458 into master Oct 19, 2016
@jsmnbom jsmnbom deleted the bitwise-filters branch May 20, 2017 13:50
@Eldinnie Eldinnie mentioned this pull request May 5, 2018
@github-actions github-actions bot locked and limited conversation to collaborators Aug 23, 2020
@Bibo-Joshi Bibo-Joshi added 🔌 enhancement pr description: enhancement and removed enhancement labels Nov 3, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
🔌 enhancement pr description: enhancement 📋 pending-review work status: pending-review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy