-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Allow filters to have a name. #675
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
Conversation
So their string representation is user friendly.
So just remove it for now. Better than doing annoying stuff with parsing a **kwargs dict in my opinion. It didn't even *really* need to be kwarg only anyways I guess...
Increases coverage by 1 (one) line! Woo :D
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looks good in general. comments on code with some ideas on how to make the code more fluent.
telegram/ext/filters.py
Outdated
self.name = self.__class__.__name__ | ||
return self.name | ||
|
||
__repr__ = __str__ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's sufficient to implement __repr__
. It will implicitly get __str__
working as you want
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alright thanks :)
telegram/ext/filters.py
Outdated
self.f = f | ||
|
||
def filter(self, message): | ||
return not self.f(message) | ||
|
||
def __str__(self): | ||
return "<telegram.ext.filters.InvertedFilter inverting {}>".format(self.f) | ||
return "<inverted {}>".format(self.f) | ||
|
||
__repr__ = __str__ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same comment about __repr__
vs __str__
telegram/ext/filters.py
Outdated
@@ -65,6 +68,15 @@ def __or__(self, other): | |||
def __invert__(self): | |||
return InvertedFilter(self) | |||
|
|||
def __str__(self): | |||
# Do not rely on classes overwriting __init__ to set a name | |||
# so we can keep backwards compatibility |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what about defining name
as class attribute which defaults to '<user_filter>'
(or something of the sort)?
then we can save the ugly code below...
Following that idea, you can then add name
attribute to each of the inheriting classes which will reduce the need to call super().__init__()
and modify all the lines instantiating the filters.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
on a second thought: what's wrong with using self.__class__.__name__
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Currently it's usingself.__class__.__name__
only if no other name is found. The reason we don't use that is that telegram.ext.Filters.status_update.migrate.__class__.__name__
is _Migrate
and not migrate
as we want. So we have to overwrite it.
Or did I misunderstand your question?
I would prefer if it did use self.__class__.__name__
when a better name is not found though. So if we had name as a class attribute and defaulted it to None (where it would then do self.__class__.__name__
) would that work?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
so back to my first suggest. name = <user_filter>
by default
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But I'd rather have it be self.__class__.__name__
by default though...?
telegram/ext/filters.py
Outdated
@@ -53,6 +53,9 @@ class BaseFilter(object): | |||
(so remember to initialize your filter classes). | |||
""" | |||
|
|||
def __init__(self, name=None): | |||
self.name = name | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can be avoided, see my comment in __str__
below
telegram/ext/filters.py
Outdated
" {} {} {}>").format(self.base_filter, "and" if self.and_filter else "or", | ||
self.and_filter or self.or_filter) | ||
return "<{} {} {}>".format(self.base_filter, "and" if self.and_filter else "or", | ||
self.and_filter or self.or_filter) | ||
|
||
__repr__ = __str__ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same comment about __repr__
vs __str__
So their string representation is user friendly.
Compare:
Old
New