Skip to content

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

Merged
merged 8 commits into from
Jun 21, 2017
Merged

Allow filters to have a name. #675

merged 8 commits into from
Jun 21, 2017

Conversation

jsmnbom
Copy link
Member

@jsmnbom jsmnbom commented Jun 18, 2017

So their string representation is user friendly.

Compare:

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

Old

<telegram.ext.filters.MergedFilter consisting of <telegram.ext.filters.MergedFilter
consisting of <telegram.ext.filters.MergedFilter consisting of
<telegram.ext.filters.MergedFilter consisting of <telegram.ext.filters.Filters._Text
object at 0x000002B28BDEFD68> or <telegram.ext.filters.Filters._Command
object at 0x000002B28BDEFE48>> and <telegram.ext.filters.Filters._Reply
object at 0x000002B28BDEFE10>> and <telegram.ext.filters.InvertedFilter
inverting <telegram.ext.filters.Filters._Forwarded object at 0x000002B28BDA66D8>>>
and <telegram.ext.filters.MergedFilter consisting of <telegram.ext.filters.Filters.entity
object at 0x000002B28BDB3C50> or <telegram.ext.filters.Filters.entity object at
0x000002B28BDB3C88>>>

New

<<<<Filters.text or Filters.command> and Filters.reply> and <inverted Filters.forwarded>
and <Filters.entity(url) or Filters.entity(text_link)>>

jsmnbom added 2 commits June 18, 2017 15:28
So their string representation is user friendly.
jsmnbom added 4 commits June 18, 2017 16:22
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
@jsmnbom jsmnbom added the 📋 pending-review work status: pending-review label Jun 20, 2017
Copy link
Member

@tsnoam tsnoam left a 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.

self.name = self.__class__.__name__
return self.name

__repr__ = __str__
Copy link
Member

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

Copy link
Member Author

Choose a reason for hiding this comment

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

Alright thanks :)

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__
Copy link
Member

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__

@@ -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
Copy link
Member

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.

Copy link
Member

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__?

Copy link
Member Author

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?

Copy link
Member

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

Copy link
Member Author

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...?

@@ -53,6 +53,9 @@ class BaseFilter(object):
(so remember to initialize your filter classes).
"""

def __init__(self, name=None):
self.name = name

Copy link
Member

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

" {} {} {}>").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__
Copy link
Member

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__

@tsnoam tsnoam merged commit 3ea16cb into master Jun 21, 2017
@tsnoam tsnoam deleted the name-filters branch June 21, 2017 20:11
@github-actions github-actions bot locked and limited conversation to collaborators Aug 23, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
📋 pending-review work status: pending-review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 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