Last active
February 23, 2020 15:51
-
-
Save qwadratic/7758aa87f8493736d759c148b76eb63e to your computer and use it in GitHub Desktop.
Notes from Python Masterclass
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Если что-то не получается на любом шаге - не теряем время и идем в repl.it | |
| 1. Открываем консоль: | |
| - windows: WIN+R, там пишем cmd и жмем Enter | |
| - mac/linux: открываем приложение Terminal | |
| 2. Пишем "python" и жмем Enter | |
| 3. Если запустился питон версии 3.6 или выше () - все ок, можем продолжать. | |
| Выходим из python (для этого наберите "exit()" и нажмите Enter) | |
| 4. в консоли набираем "pip install python-telegram-bot" и жмем Enter | |
| 5. Когда все завершится успехом (скорее всего): pip install flask |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def todo_create(text): | |
| return { | |
| 'text': text, | |
| 'done': False | |
| } | |
| def todo_mark_done(todo): | |
| todo['done'] = True | |
| def todo_display(todolist): | |
| if len(todolist) == 0: | |
| return 'No to do.' | |
| display = 'To do:\n' | |
| todo_template = ' {n} {is_done} {text}\n' | |
| for i, todo in enumerate(todolist, 1): | |
| done_mark = '+' if todo['done'] else '-' | |
| display += todo_template.format(n=i, is_done=done_mark, text=todo['text']) | |
| return display |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from os import system | |
| from todolib import todo_display, todo_mark_done, todo_create | |
| TODO_LIST = [] | |
| def main(): | |
| while True: | |
| print(todo_display(TODO_LIST)) | |
| request = input('Add todo or write any number to mark it done: ') | |
| if request.isnumeric() and int(request) <= len(TODO_LIST): | |
| todo_mark_done(TODO_LIST[int(request) - 1]) | |
| else: | |
| todo = todo_create(request) | |
| TODO_LIST.append(todo) | |
| system('clear') | |
| main() | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 1. Открываем переписку с @BotFather в Телеграме | |
| 2. /newbot | |
| 3. Вводим имя бота | |
| 4. Вводим никнейм, по которому будет доступен бот. Он должен выглядеть как ******bot | |
| 5. Никнеймы часто бывают заняты. Чтобы долго не подбирать свободное имя - введите gjhfgjhgfjh_bot :) | |
| 6. После настройки никнейма, вы получите длинное сообщение, в нем будет "токен" бота. выглядит примерно так | |
| 123456789:AAHM5h24HJGKJHkgsd879325RQMjl9 | |
| Эта строчка нам понадобится |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from telegram.ext import Updater, CommandHandler, MessageHandler, Filters | |
| from todolib import todo_mark_done, todo_display, todo_create | |
| TOKEN = '123456789:AAHM5h24HJGKJHkgsd879325RQMjl9' | |
| HELP_TEXT = """ | |
| Напиши любое сообщение чтобы добавить задачу в список. | |
| Используй /done <номер задачи>, чтобы отметить задачу как выполненную. | |
| """ | |
| TODO_LIST = [] | |
| def start(update, context): | |
| update.message.from_user.send_message(HELP_TEXT) | |
| def todo_done(update, context): | |
| if len(context.args) == 0: | |
| return | |
| request = context.args[0] | |
| if not request.isnumeric() or int(request) > len(TODO_LIST): | |
| return | |
| todo_mark_done(TODO_LIST[int(request) - 1]) | |
| update.message.from_user.send_message(todo_display(TODO_LIST)) | |
| def todo_add(update, context): | |
| text = update.message.text | |
| todo = todo_create(text) | |
| TODO_LIST.append(todo) | |
| update.message.from_user.send_message(todo_display(TODO_LIST)) | |
| def main(): | |
| updater = Updater(TOKEN, use_context=True) | |
| dispatcher = updater.dispatcher | |
| dispatcher.add_handler(CommandHandler('start', start)) | |
| dispatcher.add_handler(CommandHandler('done', todo_done)) | |
| dispatcher.add_handler(MessageHandler(Filters.text, todo_add)) | |
| updater.start_polling() | |
| main() | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from flask import Flask, request, redirect | |
| from todolib import todo_display, todo_create, todo_mark_done | |
| app = Flask(__name__) | |
| TODO_LIST = [] | |
| @app.route('/') | |
| def main(): | |
| return 'Hello world' | |
| @app.route('/todolist') | |
| def todo_list(): | |
| todo_formatted = todo_display(TODO_LIST) | |
| html = ''' | |
| {todos} | |
| <br><br>Add todo:<br> | |
| <form method="post" action="/add"> | |
| <input type="text" name="text"> | |
| <input type="submit"> | |
| </form> | |
| <br>Mark done:<br> | |
| <form method="post" action="/done"> | |
| <input type="text" name="todo_id"> | |
| <input type="submit"> | |
| </form> | |
| ''' | |
| return html.format(todos=todo_formatted) | |
| @app.route('/add', methods=['POST']) | |
| def todo_add(): | |
| text = request.form.get('text') | |
| if not text: | |
| return redirect('/todolist') | |
| todo = todo_create(text) | |
| TODO_LIST.append(todo) | |
| return redirect('/todolist') | |
| @app.route('/done', methods=['POST']) | |
| def todo_done(): | |
| todo_id = int(request.form.get('todo_id')) | |
| todo_mark_done(TODO_LIST[todo_id - 1]) | |
| return redirect('/todolist') | |
| app.run('0.0.0.0', 5000, debug=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment