Table of Contents

Modern Python Practice

Online Python run

Install related

install Python3 on mac

Manage and handle multiple versions of python case

Situation:

Solution:

Python 3 Changes and How to Make 2.7 Code Works

Python basics

My Wiki Page related

to read:

Python vs .

Python Comment Tips

Launch Python script

OS system operation

List File and sub-folders - the Correct Way

import glob
def getDirData(top, maxDepth=1):
    res = []
    for d in range( 1, maxDepth+2 ):
        maxGlob = "/".join( "*" * d )
        topGlob = os.path.join( top, maxGlob )
        allFiles = glob.glob( topGlob )
        res.extend( allFiles )
        #someFiles.extend( [ f for f in allFiles if fnmatch.fnmatch( os.path.basename( f ), fnMask ) ] )
    return res
 
import os
def getDirData( top, maxDepth=1):
    # ref: https://stackoverflow.com/questions/7159607/list-directories-with-a-specified-depth-in-python
    # py3 option: better with scandir, 
    # py2 has: https://github.com/benhoyt/scandir
    top = os.path.normpath(top)
    res = []
    root_len = len(top) #+len(os.path.sep)
    for root,dirs,files in os.walk(top, topdown=True):
        depth = root[root_len:].count(os.path.sep)
        if depth < maxDepth:
            res += [os.path.join(root, d) for d in dirs]
            res += [os.path.join(root, f) for f in files]
        elif depth == maxDepth:
            res += [os.path.join(root, d) for d in dirs]
            res += [os.path.join(root, f) for f in files]
            dirs[:] = [] # Don't recurse any deeper
    return res
 
# detail inf: https://benhoyt.com/writings/scandir/
# can use with maya 2017 by zip out _scandir.pyd and scandir.py: (scandir-1.10.0-cp27-cp27m-win_amd64.whl) https://pypi.org/project/scandir/1.10.0/#files
tmp_path = r'D:\PyLib\scandir27x64'
tmp_path in sys.path or sys.path.append(tmp_path)
import scandir
def getDirData(top, maxDepth=1):
    # py3.5 has built in, only for 2.7
    # https://github.com/benhoyt/scandir
    top = os.path.normpath(top)
    res = []
    root_len = len(top)#+len(os.path.sep)
    for root,dirs,files in scandir.walk(top, topdown=True):
        depth = root[root_len:].count(os.path.sep)
        if depth < maxDepth:
            res += [os.path.join(root, d) for d in dirs]
            res += [os.path.join(root, f) for f in files]
        elif depth == maxDepth:
            res += [os.path.join(root, d) for d in dirs]
            res += [os.path.join(root, f) for f in files]
            dirs[:] = [] # Don't recurse any deeper
    return res
 
# test
import time
start = time.time()
result = getDirData(r'R:\ServerFolder\RootDir',5)
end = time.time()
print('used: {}s'.format(end - start))

Copy File - the Correct Way

string operation

control flow operation

data operation

Data Type

turn a file into a module

turn a folder into a module

check module installed

cross python file reference

file operation

thread operation

module operation

useful module list

useful HTML modules

useful automation modules

useful Graphic modules

useful audio module

database and file modules

system info modules

system command modules

compile and de-compile module

cx_Freeze: compile to exe, app, bin

PyInstaller: compile py into binary, deeper than cx_Freeze,

# cmd install
python -m pip install pyinstaller

# compile, cd to your py file first, you may need copy your supporting folder to dist path after compile
python.exe -m PyInstaller MyApp.py

# no terminal version
python.exe -m PyInstaller MyApp.py --noconsole
# set app binary icon
python.exe -m PyInstaller MyApp.py --icon=icons/MyApp.ico

# exclude tk and tcl library
python.exe -m PyInstaller MyApp.py --icon=icons/MyApp.ico --exclude-module=FixTk --exclude-module=tk --exclude-module=tcl --exclude-module=_tkinter --exclude-module=tkinter --exclude-module=Tkinter

# for mac, if failed to build on pyconfig.h
# - fix pyconfig.h; by cmd: sudo touch /Library/Python/3.8/include/pyconfig.h
python.exe -m PyInstaller MyApp.py --icon=icons/MyApp.icns

python.exe -m PyInstaller MyApp.py --icon=icons/MyApp.icns -w --onefile

# cmd+i to check app info, and drag icns file to app/folder icon logo to set the icon
# manually turn folder into app, just add .app behind folder name, folder name must be same as main binary file

# when run result mac app on other computer, if security stop it run like "app is damaged", run this command on created app or app folder
xattr -cr "/Applications/MyAppFileorMyAppFolder.app"

uncompyle2: convert pyc to py (python 2.7 code support only)

https://github.com/wibiti/uncompyle2

Python Important Modules

pip

re

all data file
(?!(.*[.]db)|(^Keyboard$)|(^[.].*))
all maya file
[^.].+(.ma|.mb|.abc|.xml|.fbx)

requests

PIL and Pillow

BeautifulSoup

selenium

PyAutoGUI

SikuliX

pyautoit

pywinauto

pywin32 - win32com

  * tip and ref:

ctypes

file and folder operation

twisted

django

openpyxl

Python by Reference or by Value

Common Python syntax

comment #
print print
string var=“string”
long string varL='''\ whatever '''
raw string r“Any dangerous Character here”
os navigation file operation string operation lib rename
os.chdir('/path') cd tfile=open('t.txt','w') str.replace('o','n') replace os system prefix_remove_fixed
os.getcwd() tfile.write('hello') str[2:6] substring glob wildchar list prefix_add_fixed
os.ctermid() tfile.close() str1+str2 concatenate re regular expression end_remove_fixed
os.uname() tfile.read() str(100) string convert end_add_fixed
os.listdir('/path') tfile.writeline() prefix_remove_vary
os.mkdir('/path') tfile.readline() [a-c] [abc] class
os.removedirs('/path') [^ abc] not in class
os.rename(src,dst) a* may have
os.getenv('HOME') a+ must have one
a? may have one
a{1,3} May 1~3 one
r”str” raw string
regular expression

http://docs.python.org/howto/regex.html#regex-howto

Python based tool and application

Python web applications

Python GUI

wxPython

wxPython GUI concept quick guide

wxWidget - wxPython

Common wxPython syntax

PyQt

Python Portable for Windows

Workflow on portable python

  1. Prepare python 2.7/3.5, by download the official python exe, and install it to D drive
      • for Python3.5.2: Windows x86 executable installer
      • for Python2.7.12: Windows x86 MSI installer
  2. update: for PyQt and PySide, just cd to that python directory and .\python.exe -m pip install PySide or PyQt4 to install that module with pip or ./python for mac/linux
  3. Prepare PyQt4 library for py2.7/3.5 by download the official, and install to the correct PythonX.X folder
  4. other library:
    • cx_Freeze

Python Portable for Mac

Standard Install Python 3.8

Python Cool Tricks and Notes

Python and interaction with other API

Telegram

  1. search @BotFather on telegram
  2. in the chat, type /newbot, then type your bot “botname”, then type your bot “botusername_bot”
  3. now, your bot api key will show up, copy and save it
  4. now search your bot “botusername_bot” to start chat with it
  5. in python, use requests to get the chat from the bot, try one more time if no result
    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)
  6. once you got result in json, you will find the chat id.
    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']
  7. to send a text to that chat from your bot side
    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)
  8. now, you should have got the reply from your bot
    result = response.json()
    send_state = result['ok']
    send_result = result['result']
    # chat (first_name, id, type); date:int; message_id; text

Python and Networking related