Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
devwiki:python [2022/05/02 14:34] – [system command modules] ying | devwiki:python [2024/03/25 06:36] (current) – [Python and interaction with other API] 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 Python3 on mac ===== | ||
+ | |||
+ | * Mac has its own python install, very old, only for basic usage | ||
+ | * to install Python3 from official python website, | ||
+ | - download python 3 for mac installer | ||
+ | - install as usual and done | ||
+ | - run python3 in cmd | ||
+ | - then it pops: " | ||
+ | - 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 | ||
+ | |||
+ | ===== 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 26: | Line 71: | ||
reload = importlib.reload # add reload | reload = importlib.reload # add reload | ||
raw_input = input # add raw_input | raw_input = input # add raw_input | ||
+ | xrange = range # range | ||
+ | long = int # int | ||
+ | unicode = str # str | ||
</ | </ | ||
+ | |||
+ | * integer division difference in py3 vs py2, a/b | ||
+ | * ref: https:// | ||
+ | * for safe, use < | ||
+ | 35// | ||
+ | |||
+ | 35/60 = 0.583 #py3 | ||
+ | 35//60 = 0 # py3 | ||
+ | </ | ||
+ | |||
====== Python basics ====== | ====== Python basics ====== | ||
Line 87: | Line 145: | ||
===== OS system operation ===== | ===== OS system operation ===== | ||
- | * get user folder <code python> | + | * get user/home folder <code python> |
+ | os.path.expanduser(' | ||
+ | os.getenv(" | ||
+ | os.getenv(" | ||
+ | </ | ||
* make python code able to print unicode directly without .encode(' | * make python code able to print unicode directly without .encode(' | ||
Line 106: | 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 426: | Line 493: | ||
</ | </ | ||
+ | * to use non-ascii character in python file, you need to put encoding in first line of code, or you will get " | ||
+ | # coding: utf-8 | ||
+ | my_special_string = u" | ||
+ | print(u" | ||
+ | </ | ||
+ | * remove extra space from a string, also remove end single space< | ||
+ | tidy_name = ' ' | ||
+ | |||
+ | # just remove extra space | ||
+ | tidy_name = re.sub(' | ||
+ | tidy_name = re.sub(' | ||
+ | </ | ||
===== control flow operation ===== | ===== control flow operation ===== | ||
Line 447: | Line 526: | ||
not x | not x | ||
</ | </ | ||
+ | * try operation, note try is not a block, variable is same scope before try <code python> | ||
+ | try: | ||
+ | val = test(A) | ||
+ | except: | ||
+ | pass | ||
+ | # ref: https:// | ||
+ | </ | ||
===== data operation ===== | ===== data operation ===== | ||
Line 859: | 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 907: | 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 917: | 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 1042: | 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 1092: | Line 1199: | ||
info=subprocess.Popen(' | info=subprocess.Popen(' | ||
out, err = info.communicate() | out, err = info.communicate() | ||
+ | #out = str(out) # for py3 byte convert | ||
+ | out = out.decode(' | ||
result = [ x for x in out.replace('"','' | result = [ x for x in out.replace('"','' | ||
if len(result)> | if len(result)> | ||
Line 1135: | Line 1244: | ||
subprocess.Popen([r' | subprocess.Popen([r' | ||
</ | </ | ||
- | | + | |
# hide popup console for win platform only | # hide popup console for win platform only | ||
startupinfo = subprocess.STARTUPINFO() | startupinfo = subprocess.STARTUPINFO() | ||
Line 1143: | Line 1252: | ||
info=subprocess.Popen( ' | info=subprocess.Popen( ' | ||
out, err = info.communicate() | out, err = info.communicate() | ||
+ | out = out.decode(' | ||
</ | </ | ||
==== string related ==== | ==== string related ==== | ||
Line 1227: | Line 1337: | ||
* 3rd party module, html get post request response operation library | * 3rd party module, html get post request response operation library | ||
+ | * header list: https:// | ||
* **Single Get web link content** <code python> | * **Single Get web link content** <code python> | ||
Line 1253: | 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 1258: | Line 1376: | ||
* a html info extractor, best works with another parser module " | * a html info extractor, best works with another parser module " | ||
+ | * ref: https:// | ||
+ | * parser is a html interprater, | ||
* example code <code python> | * example code <code python> | ||
from bs4 import BeautifulSoup | from bs4 import BeautifulSoup | ||
Line 1458: | Line 1577: | ||
def get_win_order_all(): | def get_win_order_all(): | ||
# ctype: grab folder windows in z order, return [(title, class, [x,y,w,h], hwnd int)] | # ctype: grab folder windows in z order, return [(title, class, [x,y,w,h], hwnd int)] | ||
+ | from ctypes import wintypes | ||
order_list = [] | order_list = [] | ||
result_list = [] | result_list = [] | ||
Line 1470: | Line 1590: | ||
r=ctypes.wintypes.RECT() | r=ctypes.wintypes.RECT() | ||
ctypes.windll.user32.GetWindowRect(top, | ctypes.windll.user32.GetWindowRect(top, | ||
- | result_list.append( [buff.value, | + | |
+ | if isinstance(class_name.value, | ||
+ | | ||
+ | else: | ||
+ | result_list.append( [buff.value, | ||
order_list.append(top) | order_list.append(top) | ||
while True: | while True: | ||
Line 1483: | Line 1607: | ||
r=ctypes.wintypes.RECT() | r=ctypes.wintypes.RECT() | ||
ctypes.windll.user32.GetWindowRect(next, | ctypes.windll.user32.GetWindowRect(next, | ||
- | result_list.append( [buff.value, | + | |
+ | if isinstance(class_name.value, | ||
+ | | ||
+ | else: | ||
+ | result_list.append( [buff.value, | ||
order_list.append(next) | order_list.append(next) | ||
return result_list | return result_list | ||
Line 1508: | Line 1636: | ||
class_name = ctypes.create_string_buffer(200) | class_name = ctypes.create_string_buffer(200) | ||
ctypes.windll.user32.GetClassNameA(hwnd, | ctypes.windll.user32.GetClassNameA(hwnd, | ||
- | if class_name.value == ' | + | |
- | window_list.append( [ buff.value, | + | |
+ | t_class_name = class_name.value.decode(' | ||
+ | if t_class_name | ||
+ | window_list.append( [ buff.value, | ||
return True | return True | ||
procObj = ctypes.WINFUNCTYPE(ctypes.c_bool, | procObj = ctypes.WINFUNCTYPE(ctypes.c_bool, | ||
Line 1567: | Line 1698: | ||
class_name = ctypes.create_string_buffer(200) | class_name = ctypes.create_string_buffer(200) | ||
ctypes.windll.user32.GetClassNameA(hwnd, | ctypes.windll.user32.GetClassNameA(hwnd, | ||
- | | + | |
+ | if not isinstance(t_class_name, | ||
+ | t_class_name = class_name.value.decode(' | ||
+ | return t_class_name | ||
def is_win_top(hwnd): | def is_win_top(hwnd): | ||
top_hwnd = ctypes.windll.user32.GetTopWindow(None) | top_hwnd = ctypes.windll.user32.GetTopWindow(None) | ||
Line 2203: | Line 2337: | ||
* when first run, allow Access folder right. | * when first run, allow Access folder right. | ||
* to make command extension file runable, do bash for the command extension launcher <code bash> | * to make command extension file runable, do bash for the command extension launcher <code bash> | ||
- | ====== Python Cool Tricks ====== | + | ====== Python Cool Tricks |
* start HTML and FILE server in current directory <code dos> | * start HTML and FILE server in current directory <code dos> | ||
+ | * decimal vs float for real life accounting usage: | ||
+ | * decimal wont have those float annoying issue with tons of 0s after . | ||
+ | * decimal deal number like human do instead of infinite computing accuracy | ||
+ | * https:// | ||
+ | * https:// | ||
+ | * < | ||
+ | |||
+ | |||
+ | * python and whatsapp msg | ||
+ | * ref: https:// | ||
+ | * **pywhatkit**: | ||
+ | * because it use automation, so interaction with browser can be buggy, no quick repeat sending (since automate like human action, need wait response of other app update), send group chat may send to wrong group if you also interact with browser | ||
+ | * example< | ||
+ | import pywhatkit | ||
+ | pywhatkit.sendwhatmsg_instantly(" | ||
+ | # wait 10s to ui response, close tab true after 3s, | ||
+ | # you need to close tab as whatsapp web only allow 1 window active | ||
+ | </ | ||
+ | * **selenium**: | ||
+ | * example setup and login manually for first time <code python> | ||
+ | my_drive = None | ||
+ | def drive_setup(): | ||
+ | from selenium import webdriver | ||
+ | from selenium.webdriver.common.keys import Keys | ||
+ | from selenium.webdriver.common.by import By | ||
+ | | ||
+ | my_drive = webdriver.Chrome() | ||
+ | phone_no = " | ||
+ | my_drive.get(f' | ||
+ | |||
+ | def drive_msg(in_text): | ||
+ | from selenium.webdriver.common.by import By | ||
+ | if my_drive is None: | ||
+ | return | ||
+ | else: | ||
+ | chat_box = my_drive.find_element(by=By.XPATH, | ||
+ | chat_box.send_keys(in_text) | ||
+ | send_button = my_drive.find_element(by=By.CSS_SELECTOR, | ||
+ | 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, | ||
+ | </ | ||
+ | |||
+ | ====== Python and Networking related ====== | ||
+ | |||
+ | * above is python interact with a REST API server, you can also use python to build your own REST API server to handle requests from other application, | ||
+ | * implement REST API into python, you may need to use some python module like Flash, FastAPI, then you start another thread from your python application to run your REST API server. | ||
+ | * while using Sockets method may be simpler if you just want to send some simple message between applications, | ||
+ | * using REST API allow your application to talk cross different application and difference device, and provide a safe gate to your important data handled by your application. | ||