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
Update Examples with the str.format() syntax. #870
Update Examples with the str.format() syntax. #870
Conversation
conversationbot.py: Use `str.format()` conversationbot2.py: Use `str.format()` make the states dict flake8 compatible. echobot.py: Change Docstring to a Docstring using triple quotes. Add Docstrings to `main()` and `echo()` echobot2.py: Add File Description to Docstring Add License notice to Docstring Add Docstrings to `start()`, `help()`, `echo()` and `main()` inlinebot.py: Use `str.format()` Add File Description to Docstring Add License notice to Docstring Add Docstring to `start()`, `help()`, `inlinequery()`, `error()` Create the list "results" with the Inline Results instead of appending them. inlinekeyboard.py: Use `str.format()` Put the Update in a `main()` function. paymentbot.py: Use `str.format()` Shorten comment on how to get a provider token to avoid the comment being longer than 99 characters timerbot.py: Use `str.format()` Add Docstrings to `alarm()`, `set_timer()`, `unset()` Rename `set()` to `set_timer()` to avoud shadowing the built-in `set()`
Codecov Report
@@ Coverage Diff @@
## master #870 +/- ##
==========================================
+ Coverage 91.44% 91.79% +0.35%
==========================================
Files 101 101
Lines 3985 3985
Branches 603 603
==========================================
+ Hits 3644 3658 +14
+ Misses 195 189 -6
+ Partials 146 138 -8
Continue to review full report at Codecov.
|
Thanks to @KamranMackey for making me aware of the fact that Log messages shouldnt use str.format() according to this article: http://reinout.vanrees.org/weblog/2015/06/05/logging-formatting.html
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.
Hi @SitiSchu
Thanks for your contribution. In general looks good (I like str.format()) but there are some changes which I must ask you to do. Please see CR comments on the code.
examples/conversationbot.py
Outdated
@@ -46,7 +46,7 @@ def start(bot, update): | |||
|
|||
def gender(bot, update): | |||
user = update.message.from_user | |||
logger.info("Gender of %s: %s" % (user.first_name, update.message.text)) | |||
logger.info("Gender of {}: {}".format(user.first_name, update.message.text)) |
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.
Good catch here, we shouldn't be formatting logs ourselves but rather let logging do it. Unfortunately logging only work with %s
notation. So please change to:
logger.info("Gender of %s: %s", user.first_name, update.message.text)
edit: I see that part of the logging occurrences you've done that way.
examples/conversationbot.py
Outdated
@@ -58,7 +58,7 @@ def photo(bot, update): | |||
user = update.message.from_user | |||
photo_file = bot.get_file(update.message.photo[-1].file_id) | |||
photo_file.download('user_photo.jpg') | |||
logger.info("Photo of %s: %s" % (user.first_name, 'user_photo.jpg')) | |||
logger.info("Photo of {}: {}".format(user.first_name, 'user_photo.jpg')) |
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 logging
examples/conversationbot.py
Outdated
@@ -67,7 +67,7 @@ def photo(bot, update): | |||
|
|||
def skip_photo(bot, update): | |||
user = update.message.from_user | |||
logger.info("User %s did not send a photo." % user.first_name) | |||
logger.info("User {} did not send a photo.".format(user.first_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.
same comment about logging
examples/conversationbot.py
Outdated
logger.info("Location of %s: %f / %f" | ||
% (user.first_name, user_location.latitude, user_location.longitude)) | ||
logger.info("Location of {}: {} / {}".format( | ||
user.first_name, user_location.latitude, user_location.longitude)) |
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 logging
examples/conversationbot2.py
Outdated
reply_markup=markup) | ||
"Hi! My name is Doctor Botter. I will hold a more complex conversation with you. " | ||
"Why don't you tell me something about yourself?", | ||
reply_markup=markup) |
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.
why did you change the indentation? please revert.
examples/conversationbot.py
Outdated
update.message.reply_text('Thank you! I hope we can talk again some day.') | ||
|
||
return ConversationHandler.END | ||
|
||
|
||
def cancel(bot, update): | ||
user = update.message.from_user | ||
logger.info("User %s canceled the conversation." % user.first_name) | ||
logger.info("User {} canceled the conversation.".format(user.first_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.
same comment about logging
examples/conversationbot2.py
Outdated
}, | ||
|
||
fallbacks=[RegexHandler('^Done$', done, pass_user_data=True)] | ||
) |
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.
why did you change the indentation here? please revert
examples/inlinebot.py
Outdated
title="Italic", | ||
input_message_content=InputTextMessageContent( | ||
"_{}_".format(escape_markdown(query)), | ||
parse_mode=ParseMode.MARKDOWN))] |
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.
why did you change the indentation here? please revert.
examples/paymentbot.py
Outdated
@@ -31,7 +33,7 @@ def start_with_shipping_callback(bot, update): | |||
description = "Payment Example using python-telegram-bot" | |||
# select a payload just for you to recognize its the donation from your bot | |||
payload = "Custom-Payload" | |||
# get your provider_token at @botfather, see https://core.telegram.org/bots/payments#getting-a-token | |||
# to get your provider_token see https://core.telegram.org/bots/payments#getting-a-token |
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.
If we're into grammar, "In order to get a provider_token ..." would be more appropriate.
examples/paymentbot.py
Outdated
@@ -55,7 +57,7 @@ def start_without_shipping_callback(bot, update): | |||
description = "Payment Example using python-telegram-bot" | |||
# select a payload just for you to recognize its the donation from your bot | |||
payload = "Custom-Payload" | |||
# get your provider_token at @botfather, see https://core.telegram.org/bots/payments#getting-a-token | |||
# check https://core.telegram.org/bots/payments#supported-currencies for more details |
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 grammar.
Removed the Extra Indent in the start() function.
Codecov Report
@@ Coverage Diff @@
## master #870 +/- ##
==========================================
+ Coverage 91.44% 91.79% +0.35%
==========================================
Files 101 101
Lines 3985 3985
Branches 603 603
==========================================
+ Hits 3644 3658 +14
+ Misses 195 189 -6
+ Partials 146 138 -8
Continue to review full report at Codecov.
|
Codecov Report
@@ Coverage Diff @@
## master #870 +/- ##
==========================================
+ Coverage 91.44% 91.73% +0.29%
==========================================
Files 101 101
Lines 3985 4055 +70
Branches 603 620 +17
==========================================
+ Hits 3644 3720 +76
- Misses 195 197 +2
+ Partials 146 138 -8
Continue to review full report at Codecov.
|
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.
Thanks for all the changes. Unfortunately, I'm afraid I'll have to ask you a few more changes, mostly regarding indentation.
examples/conversationbot2.py
Outdated
reply_markup=markup) | ||
"{}" | ||
"You can tell me more, or change your opinion on something.".format( | ||
facts_to_str(user_data)), reply_markup=markup) |
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.
Indentation here still needs fixing.
Try using autopep8 on the file.
examples/conversationbot2.py
Outdated
@@ -129,10 +130,10 @@ def main(): | |||
received_information, | |||
pass_user_data=True), | |||
], | |||
}, | |||
}, |
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.
autopep8 will also fix indentation here.
examples/conversationbot2.py
Outdated
|
||
fallbacks=[RegexHandler('^Done$', done, pass_user_data=True)] | ||
) | ||
) |
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.
autopep8 will also fix indentation here.
examples/inlinebot.py
Outdated
title="Italic", | ||
input_message_content=InputTextMessageContent( | ||
"_{}_".format(escape_markdown(query)), | ||
parse_mode=ParseMode.MARKDOWN))] |
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.
autopep8 can be of assistant here as well.
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.
examples/paymentbot.py
Outdated
@@ -55,7 +57,8 @@ def start_without_shipping_callback(bot, update): | |||
description = "Payment Example using python-telegram-bot" | |||
# select a payload just for you to recognize its the donation from your bot | |||
payload = "Custom-Payload" | |||
# get your provider_token at @botfather, see https://core.telegram.org/bots/payments#getting-a-token | |||
# In order to get a provider_token to get your provider_token see |
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.
In order to get a provider_token to get your provider_token see https://core.telegram.org/bots/payments#getting-a-token
Use autopep8 on the conversationbot2.py Fix the provide token comment (duplicated some words)
Did all changes except the one to inlinebot.py:inlinequery() since I'm not sure if autopep8 makes it better. |
I formated the InlineQuery results in inlinebot.py like @jh0ker suggested with a linebreak before the |
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.
replace logger.warn
with logger.warning
as per https://docs.python.org/3/library/logging.html#logging.Logger.warning
examples/conversationbot.py
Outdated
update.message.reply_text('Bye! I hope we can talk again some day.', | ||
reply_markup=ReplyKeyboardRemove()) | ||
|
||
return ConversationHandler.END | ||
|
||
|
||
def error(bot, update, error): | ||
logger.warn('Update "%s" caused error "%s"' % (update, error)) | ||
logger.warn('Update "%s" caused error "%s"', update, error) |
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.
Can you change this to logger.warning
? Logger.warn
is deprecated.
https://docs.python.org/3/library/logging.html#logging.Logger.warning
examples/conversationbot2.py
Outdated
|
||
user_data.clear() | ||
return ConversationHandler.END | ||
|
||
|
||
def error(bot, update, error): | ||
logger.warn('Update "%s" caused error "%s"' % (update, error)) | ||
logger.warn('Update "%s" caused error "%s"', update, error) |
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.
Can you change this to logger.warning
? Logger.warn
is deprecated.
https://docs.python.org/3/library/logging.html#logging.Logger.warning
examples/echobot2.py
Outdated
update.message.reply_text(update.message.text) | ||
|
||
|
||
def error(bot, update, error): | ||
logger.warn('Update "%s" caused error "%s"' % (update, error)) | ||
logger.warn('Update "%s" caused error "%s"', update, error) |
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.
Can you change this to logger.warning
? Logger.warn
is deprecated.
https://docs.python.org/3/library/logging.html#logging.Logger.warning
examples/inlinebot.py
Outdated
|
||
update.inline_query.answer(results) | ||
|
||
|
||
def error(bot, update, error): | ||
logger.warning('Update "%s" caused error "%s"' % (update, error)) | ||
logger.warn('Update "%s" caused error "%s"', update, error) |
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.
Can you change this back to logger.warning
? Logger.warn
is deprecated.
https://docs.python.org/3/library/logging.html#logging.Logger.warning
examples/inlinekeyboard.py
Outdated
@@ -36,20 +36,26 @@ def help(bot, update): | |||
|
|||
|
|||
def error(bot, update, error): | |||
logging.warning('Update "%s" caused error "%s"' % (update, error)) | |||
logger.warn('Update "%s" caused error "%s"', update, error) |
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.
Can you change this back to logger.warning
? Logger.warn
is deprecated.
https://docs.python.org/3/library/logging.html#logging.Logger.warning
examples/paymentbot.py
Outdated
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def error(bot, update, error): | ||
logger.warn('Update "%s" caused error "%s"' % (update, error)) | ||
logger.warn('Update "%s" caused error "%s"', update, error) |
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.
Can you change this to logger.warning
? Logger.warn
is deprecated.
https://docs.python.org/3/library/logging.html#logging.Logger.warning
examples/timerbot.py
Outdated
@@ -73,10 +73,11 @@ def unset(bot, update, chat_data): | |||
|
|||
|
|||
def error(bot, update, error): | |||
logger.warning('Update "%s" caused error "%s"' % (update, error)) | |||
logger.warn('Update "%s" caused error "%s"', update, error) |
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.
Can you change this back to logger.warning
? Logger.warn
is deprecated.
https://docs.python.org/3/library/logging.html#logging.Logger.warning
@SitiSchu A bit more nitpicking :P |
@jh0ker PyCharm told me about this but I wasn't sure if Py2 supports it. I checked and it does support it so I'll change it today ^^. |
@SitiSchu Awesome, thanks :) |
I updated all error functions to the new |
examples/inlinebot.py
Outdated
@@ -44,7 +45,7 @@ def help(bot, update): | |||
|
|||
|
|||
def escape_markdown(text): |
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.
Can we replace this as well? We have telegram.utils.helpers.escape_markdown
now.
@jh0ker Done. |
Any updates on this PR? :) |
LGTM @tsnoam any more changes requested? |
@SitiSchu Thankyou for your contribution. |
conversationbot.py:
Use
str.format()
conversationbot2.py:
Use
str.format()
make the states dict flake8 compatible.
echobot.py:
Change Docstring to a Docstring using triple quotes.
Add Docstrings to
main()
andecho()
echobot2.py:
Add File Description to Docstring
Add License notice to Docstring
Add Docstrings to
start()
,help()
,echo()
andmain()
inlinebot.py:
Use
str.format()
Add File Description to Docstring
Add License notice to Docstring
Add Docstring to
start()
,help()
,inlinequery()
,error()
Create the list "results" with the Inline Results instead of appending them.
inlinekeyboard.py:
Use
str.format()
Put the Update in a
main()
function.paymentbot.py:
Use
str.format()
Shorten comment on how to get a provider token to avoid the comment being longer than 99 characters
timerbot.py:
Use
str.format()
Add Docstrings to
alarm()
,set_timer()
,unset()
Rename
set()
toset_timer()
to avoud shadowing the built-inset()