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

Update Examples with the str.format() syntax. #870

Merged
merged 12 commits into from Oct 20, 2017
Merged

Update Examples with the str.format() syntax. #870

merged 12 commits into from Oct 20, 2017

Conversation

SitiSchu
Copy link
Contributor

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()

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

codecov bot commented Oct 14, 2017

Codecov Report

Merging #870 into master will increase coverage by 0.35%.
The diff coverage is n/a.

Impacted file tree graph

@@            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
Flag Coverage Δ
#Appveyor 86.85% <ø> (+0.6%) ⬆️
#Travis 91.41% <ø> (ø) ⬆️
Impacted Files Coverage Δ
telegram/message.py 96.01% <0%> (+4.78%) ⬆️
telegram/games/game.py 100% <0%> (+5.71%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 8a8b121...9e9b0bd. Read the comment docs.

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
@python-telegram-bot python-telegram-bot deleted a comment from codecov bot Oct 14, 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.

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.

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

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.

@@ -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'))
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 logging

@@ -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))
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 logging

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))
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 logging

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

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.

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))
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 logging

},

fallbacks=[RegexHandler('^Done$', done, pass_user_data=True)]
)
Copy link
Member

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

title="Italic",
input_message_content=InputTextMessageContent(
"_{}_".format(escape_markdown(query)),
parse_mode=ParseMode.MARKDOWN))]
Copy link
Member

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.

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

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.

@@ -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
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 grammar.

@codecov
Copy link

codecov bot commented Oct 14, 2017

Codecov Report

Merging #870 into master will increase coverage by 0.35%.
The diff coverage is n/a.

Impacted file tree graph

@@            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
Flag Coverage Δ
#Appveyor 86.85% <ø> (+0.6%) ⬆️
#Travis 91.41% <ø> (ø) ⬆️
Impacted Files Coverage Δ
telegram/message.py 96.01% <0%> (+4.78%) ⬆️
telegram/games/game.py 100% <0%> (+5.71%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 8a8b121...1d48a83. Read the comment docs.

@codecov
Copy link

codecov bot commented Oct 14, 2017

Codecov Report

Merging #870 into master will increase coverage by 0.29%.
The diff coverage is n/a.

Impacted file tree graph

@@            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
Flag Coverage Δ
#Appveyor 86.88% <ø> (+0.63%) ⬆️
#Travis 91.31% <ø> (-0.1%) ⬇️
Impacted Files Coverage Δ
telegram/bot.py 87.22% <0%> (-0.21%) ⬇️
telegram/inline/inputlocationmessagecontent.py 100% <0%> (ø) ⬆️
telegram/inline/inlinequeryresultlocation.py 100% <0%> (ø) ⬆️
telegram/chat.py 100% <0%> (ø) ⬆️
telegram/message.py 96.22% <0%> (+4.99%) ⬆️
telegram/games/game.py 100% <0%> (+5.71%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 8a8b121...7df6c08. Read the comment docs.

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.

Thanks for all the changes. Unfortunately, I'm afraid I'll have to ask you a few more changes, mostly regarding indentation.

reply_markup=markup)
"{}"
"You can tell me more, or change your opinion on something.".format(
facts_to_str(user_data)), reply_markup=markup)
Copy link
Member

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.

@@ -129,10 +130,10 @@ def main():
received_information,
pass_user_data=True),
],
},
},
Copy link
Member

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.


fallbacks=[RegexHandler('^Done$', done, pass_user_data=True)]
)
)
Copy link
Member

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.

title="Italic",
input_message_content=InputTextMessageContent(
"_{}_".format(escape_markdown(query)),
parse_mode=ParseMode.MARKDOWN))]
Copy link
Member

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

autopep8 did this to that part:
ss 2017-10-15 at 10 49 27

This is how it looked before:
ss 2017-10-15 at 10 49 36

imho the way it was before is more readable

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

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

Simon Schürrle added 2 commits October 15, 2017 22:54
Use autopep8 on the conversationbot2.py
Fix the provide token comment (duplicated some words)
@SitiSchu
Copy link
Contributor Author

Did all changes except the one to inlinebot.py:inlinequery() since I'm not sure if autopep8 makes it better.

@SitiSchu
Copy link
Contributor Author

SitiSchu commented Oct 16, 2017

I formated the InlineQuery results in inlinebot.py like @jh0ker suggested with a linebreak before the id=uuid4()
This looks better and autopep8 didnt change the formatting to something weird.

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.

replace logger.warn with logger.warning as per https://docs.python.org/3/library/logging.html#logging.Logger.warning

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

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


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

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

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

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


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

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

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

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


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

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

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

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

@jh0ker
Copy link
Member

jh0ker commented Oct 16, 2017

@SitiSchu A bit more nitpicking :P

@SitiSchu
Copy link
Contributor Author

@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 ^^.

@jh0ker
Copy link
Member

jh0ker commented Oct 16, 2017

@SitiSchu Awesome, thanks :)

@SitiSchu
Copy link
Contributor Author

I updated all error functions to the new warning syntax. I also made flake8 pass(except D103 Missing docstring in public function)

@@ -44,7 +45,7 @@ def help(bot, update):


def escape_markdown(text):
Copy link
Member

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.

@SitiSchu
Copy link
Contributor Author

@jh0ker Done.

@SitiSchu
Copy link
Contributor Author

Any updates on this PR? :)

@Eldinnie
Copy link
Member

LGTM @tsnoam any more changes requested?

@tsnoam tsnoam merged commit 38637ec into python-telegram-bot:master Oct 20, 2017
@tsnoam
Copy link
Member

tsnoam commented Oct 20, 2017

@SitiSchu Thankyou for your contribution.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants