Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revisionLast revisionBoth sides next revision | ||
devwiki:python [2023/02/06 08:51] – [system command modules] ying | devwiki: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" | ||
+ | |||
+ | |||
+ | ====== Online Python run ====== | ||
+ | |||
+ | * https:// | ||
+ | * to install locally: < | ||
====== Install related ====== | ====== Install related ====== | ||
Line 12: | Line 24: | ||
- if you already install XCode (the developer suite for mac), then no need; if not, you can just install the smaller " | - if you already install XCode (the developer suite for mac), then no need; if not, you can just install the smaller " | ||
- go Apple developer downloads page, search command line tools for xcode, download that and install | - go Apple developer downloads page, search command line tools for xcode, download that and install | ||
+ | |||
+ | ===== Manage and handle multiple versions of python case ===== | ||
+ | |||
+ | Situation: | ||
+ | * sometimes, you may have to keep both python 2.7, python 3.x | ||
+ | * and sometimes you want to try python with AI, and AI need to install tons of AI related python modules | ||
+ | * sometimes you just to keep python module in the min form to test something | ||
+ | |||
+ | Solution: | ||
+ | * for those case, when install python, never add python to system path | ||
+ | * just download and make python folder a portable folder (you can install and copy the python whole folder to other machine as a portable app, better zip to copy, faster) | ||
+ | * then your computer cmd never know where is python and won't random get your python from somewhere | ||
+ | * then, dynamically set python into python in cmd session or batch file, or use full path to python.exe to run your script | ||
+ | * session based method (win as example) <code dos>SET PATH=D: | ||
+ | * batch file method <code dos test.batch> | ||
+ | @echo off | ||
+ | SetLocal EnableDelayedExpansion | ||
+ | set CustPython=D: | ||
+ | call !CustPython!python.exe my_script.py | ||
+ | </ | ||
Line 136: | Line 168: | ||
* get class name <code python> | * get class name <code python> | ||
* get basename of folder and file <code python> | * get basename of folder and file <code python> | ||
+ | * get file name without extension <code python> | ||
+ | file_name = os.path.basename(file_path).rsplit(' | ||
+ | import pathlib | ||
+ | file_name_2 = pathlib.Path(file_path).stem | ||
+ | </ | ||
* get expanded and un-symbolic path <code python> | * get expanded and un-symbolic path <code python> | ||
os.path.abspath(__file__) | os.path.abspath(__file__) | ||
Line 907: | 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), | ||
</ | </ | ||
* write python data< | * write python data< | ||
Line 955: | Line 995: | ||
os.mkdir(to_mkdir) | os.mkdir(to_mkdir) | ||
os.startfile(tgtPath) # show result folder | os.startfile(tgtPath) # show result folder | ||
+ | </ | ||
+ | |||
+ | ===== thread operation ===== | ||
+ | |||
+ | * run a process or python service/app into another thread, so it wont block current interaction <code python> | ||
+ | def app(opt): | ||
+ | if opt == ' | ||
+ | import threading | ||
+ | th = threading.Thread(target = run_noter) | ||
+ | th.start() | ||
+ | def run_noter(): | ||
+ | import Noter | ||
+ | Noter.main() | ||
</ | </ | ||
Line 965: | Line 1018: | ||
REM make sure that wheel is in 32/64bit matching your python 32/64bit version | REM make sure that wheel is in 32/64bit matching your python 32/64bit version | ||
pip install C:/ | pip install C:/ | ||
+ | </ | ||
+ | * How to install extra module into seperate directory < | ||
+ | python.exe -m pip install --target=D: | ||
</ | </ | ||
* How to install extra modules manually | * How to install extra modules manually | ||
Line 1090: | Line 1146: | ||
</ | </ | ||
+ | * data hashing, like compress data into a string, good for big data compare and record | ||
+ | * hash (built-in, not need import) 64bit, take a object and get a int for quick compare | ||
+ | * hashlib (built-in, need import) 128bit+ and more option like md5, sha for more secure and digital signiture | ||
==== system info modules ==== | ==== system info modules ==== | ||
Line 1193: | Line 1252: | ||
info=subprocess.Popen( ' | info=subprocess.Popen( ' | ||
out, err = info.communicate() | out, err = info.communicate() | ||
- | out = str(out) # for py3 byte convert | + | out = out.decode(' |
</ | </ | ||
==== string related ==== | ==== string related ==== | ||
Line 1305: | Line 1364: | ||
result.content # is html in bytes | result.content # is html in bytes | ||
result.text # is html in unicode text | result.text # is html in unicode text | ||
+ | </ | ||
+ | * disable non secure cert warning <code python> | ||
+ | ===== PIL and Pillow===== | ||
+ | |||
+ | * PIL, Python Imaging Library (Fork) = Pillow, install the lib < | ||
+ | * python code <code python> | ||
+ | from PIL import Image, ImageDraw, ImageOps | ||
</ | </ | ||
Line 2314: | Line 2380: | ||
send_button.click() | send_button.click() | ||
</ | </ | ||
+ | |||
+ | ====== Python and interaction with other API ====== | ||
+ | |||
+ | Telegram | ||
+ | - search @BotFather on telegram | ||
+ | * manual: https:// | ||
+ | - in the chat, type /newbot, then type your bot " | ||
+ | - now, your bot api key will show up, copy and save it | ||
+ | - now search your bot " | ||
+ | - in python, use requests to get the chat from the bot, try one more time if no result <code python> | ||
+ | import requests | ||
+ | the_key = " | ||
+ | url = " | ||
+ | response = requests.get(url) | ||
+ | result = response.json() | ||
+ | print(result) | ||
+ | </ | ||
+ | - once you got result in json, you will find the chat id. <code python> | ||
+ | # the list of all chat spec your bot received, yours is likely the first one. | ||
+ | my_chat_info = result[' | ||
+ | my_msg_info = my_chat_info[0][' | ||
+ | # it got info of: chat, date, from, message_id, text | ||
+ | my_chat_id = my_msg_info[' | ||
+ | </ | ||
+ | - to send a text to that chat from your bot side <code python> | ||
+ | reply_text = 'hello world' | ||
+ | url = " | ||
+ | response = requests.get(url) | ||
+ | </ | ||
+ | - now, you should have got the reply from your bot <code python> | ||
+ | result = response.json() | ||
+ | send_state = result[' | ||
+ | send_result = result[' | ||
+ | # chat (first_name, | ||
+ | </ | ||
+ |