Skip to content
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

Add the ability to invert (not) filters #552

Merged
merged 3 commits into from Mar 28, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 27 additions & 0 deletions telegram/ext/filters.py
Expand Up @@ -32,9 +32,14 @@ class BaseFilter(object):

>>> (Filters.audio | Filters.video)

Not:

>>> ~ Filters.command

Also works with more than two filters:

>>> (Filters.text & (Filters.entity(URL) | Filters.entity(TEXT_LINK)))
>>> Filters.text & (~ Filters.forwarded)

If you want to create your own filters create a class inheriting from this class and implement
a `filter` method that returns a boolean: `True` if the message should be handled, `False`
Expand All @@ -51,10 +56,32 @@ def __and__(self, other):
def __or__(self, other):
return MergedFilter(self, or_filter=other)

def __invert__(self):
return InvertedFilter(self)

def filter(self, message):
raise NotImplementedError


class InvertedFilter(BaseFilter):
"""Represents a filter that has been inverted.

Args:
f: The filter to invert
"""

def __init__(self, f):
self.f = f

def filter(self, message):
return not self.f(message)

def __str__(self):
return "<telegram.ext.filters.InvertedFilter inverting {}>".format(self.f)

__repr__ = __str__


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

Expand Down
26 changes: 26 additions & 0 deletions tests/test_filters.py
Expand Up @@ -226,6 +226,32 @@ def test_and_or_filters(self):
r"<telegram.ext.filters.(Filters.)?_Forwarded object at .*?> or "
r"<telegram.ext.filters.(Filters.)?entity object at .*?>>>")

def test_inverted_filters(self):
self.message.text = '/test'
self.assertTrue((Filters.command)(self.message))
self.assertFalse((~Filters.command)(self.message))
self.message.text = 'test'
self.assertFalse((Filters.command)(self.message))
self.assertTrue((~Filters.command)(self.message))

def test_inverted_and_filters(self):
self.message.text = '/test'
self.message.forward_date = 1
self.assertTrue((Filters.forwarded & Filters.command)(self.message))
self.assertFalse((~Filters.forwarded & Filters.command)(self.message))
self.assertFalse((Filters.forwarded & ~Filters.command)(self.message))
self.assertFalse((~(Filters.forwarded & Filters.command))(self.message))
self.message.forward_date = None
self.assertFalse((Filters.forwarded & Filters.command)(self.message))
self.assertTrue((~Filters.forwarded & Filters.command)(self.message))
self.assertFalse((Filters.forwarded & ~Filters.command)(self.message))
self.assertTrue((~(Filters.forwarded & Filters.command))(self.message))
self.message.text = 'test'
self.assertFalse((Filters.forwarded & Filters.command)(self.message))
self.assertFalse((~Filters.forwarded & Filters.command)(self.message))
self.assertFalse((Filters.forwarded & ~Filters.command)(self.message))
self.assertTrue((~(Filters.forwarded & Filters.command))(self.message))

def test_faulty_custom_filter(self):

class _CustomFilter(BaseFilter):
Expand Down