devwiki:python

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
Last revisionBoth sides next revision
devwiki:python [2023/07/07 07:35] – [Manage and handle multiple versions of python case] yingdevwiki:python [2024/03/21 08:53] – [Online Python run] ying
Line 1: Line 1:
  
 +
 +====== Modern Python Practice ======
 +
 +  * nowaday, now one use python 2.x anymore, use python 3 standard, use this
 +    * use pathlib instead of os.path
 +    * use f"{keyword}" instead of "{0}".format(keyword), unless it is easier
 +
 +
 +====== Online Python run ======
 +
 +  * https://jupyter.org/try-jupyter/retro/notebooks/?path=notebooks/Intro.ipynb
 +  * to install locally: <code>pip3 install --upgrade pip</code>
  
 ====== Install related ====== ====== Install related ======
Line 932: Line 944:
 except OSError: except OSError:
     pass     pass
 +    
 +# method 3: all in one check and create if needed, like method 2 but not need try and except since it will be ok for exist
 +os.makedirs(os.path.dirname(output_path), exist_ok=True)
 </code> </code>
   * write python data<code python>   * write python data<code python>
Line 1350: Line 1365:
 result.text # is html in unicode text result.text # is html in unicode text
 </code> </code>
 +  * disable non secure cert warning <code python>requests.packages.urllib3.disable_warnings(requests.packages.urllib3.exceptions.InsecureRequestWarning)</code>
 ===== PIL and Pillow===== ===== PIL and Pillow=====
  
Line 2365: Line 2380:
         send_button.click()         send_button.click()
 </code> </code>
 +
 +====== Python and interaction with other API ======
 +
 +Telegram
 +  - search @BotFather on telegram
 +    * manual: https://core.telegram.org/bots/api
 +  - in the chat, type /newbot, then type your bot "botname", then type your bot "botusername_bot"
 +  - now, your bot api key will show up, copy and save it
 +  - now search your bot "botusername_bot" to start chat with it
 +  - in python, use requests to get the chat from the bot, try one more time if no result <code python>
 +import requests
 +the_key = "api_key"
 +url = "https://api.telegram.org/bot{0}/getUpdates".format(the_key)
 +response = requests.get(url)
 +result = response.json()
 +print(result)
 +</code>
 +  - once you got result in json, you will find the chat id. <code python>result['result']
 +# the list of all chat spec your bot received, yours is likely the first one.
 +my_chat_info = result['result']
 +my_msg_info = my_chat_info[0]['message']
 +# it got info of: chat, date, from, message_id, text
 +my_chat_id = my_msg_info['chat']['id']
 +</code>
 +  - to send a text to that chat from your bot side <code python>
 +reply_text = 'hello world'
 +url = "https://api.telegram.org/bot{key}/sendMessage?chat_id={id}&text={reply}".format(**{'key':the_key, 'id':my_chat_id,'reply':reply_text})
 +response = requests.get(url)
 +</code>
 +  - now, you should have got the reply from your bot <code python>
 +result = response.json()
 +send_state = result['ok']
 +send_result = result['result']
 +# chat (first_name, id, type); date:int; message_id; text
 +</code>
 +
  • devwiki/python.txt
  • Last modified: 2024/03/25 06:36
  • by ying