Python
问题处理与技巧
国际化
程序中使用:[1]
import os
import gettext
APP_NAME = "程序名"
LOCALE_DIR = os.path.abspath("locale")
if not os.path.exists(LOCALE_DIR):
LOCALE_DIR = "/usr/share/locale"
gettext.bindtextdomain(APP_NAME, LOCALE_DIR)
gettext.textdomain(APP_NAME)
_ = gettext.gettext
有用的模块调用命令
简单的 HTTP 文件共享:(在8000端口共享当前文件夹)
python2 -m SimpleHTTPServer
python3 -m http.server
python -m smtpd -n -c DebuggingServer localhost:2525
python -m pickle FILES
当前行号与函数名
sys._getframe().f_code.co_name
sys._getframe().f_back.f_code.co_name
Windows 相关
BOM 的处理
使用 utf-8-sig
编码可在存在 BOM 时将其跳过,写文件时会自动添加 BOM。
subprocess 调用程序的搜索路径
不会使用 %PATH%
环境变量,见 Windows API#CreateProcess。可使用os.system
来绕过此问题。
代码页
Python 的默认编码(locale.getpreferredencoding()
)使用 GetACP() 函数来取得系统的代码页。此代码页不会被 chcp 命令改变。
模块和导入
导入指定 Python 文件,作为指定包的子模块:
import importlib.machinery
import package
file = 'path/to/file'
loader = importlib.machinery.SourceFileLoader('package.name', file)
mod = loader.load_module()
# necessary, or can't find name when from-importing and using via attribute
package.name = mod
自定义模块导入
从当前目录下导入 JSON 文件:
import os
import sys
import json
import importlib.abc
class JSONFinder(importlib.abc.PathEntryFinder):
def find_module(self, fullname, path=None):
if os.path.isfile(fullname + '.json'):
return JSONLoader(fullname + '.json')
class JSONLoader(importlib.abc.Loader):
def __init__(self, filepath):
self.filepath = filepath
def exec_module(self, mod):
with open(self.filepath) as f:
data = json.load(f)
mod.__dict__.update(data)
mod.__file__ = self.filepath
mod.__spec__.origin = self.filepath
mod.__spec__.has_location = True
sys.meta_path.append(JSONFinder())
import config
print(config, vars(config))
设置
# 在命令行中,Tab实现补全功能
import rlcompleter, readline
readline.parse_and_bind('tab: complete')
调用 readline 中的 rl_callback_read_char
可以使用ctypes来调用,并且可以同时使用 readline 标准库。
#!/usr/bin/env python3
# vim:fileencoding=utf-8
import sys
import readline
import ctypes
import ctypes.util
rllib_path = ctypes.util.find_library('readline')
rllib = ctypes.CDLL(rllib_path)
def callback(s):
if s is None:
rllib.rl_callback_handler_remove()
sys.stdout.write('\n')
sys.exit()
elif not s:
pass
else:
print('%s.' % s.decode())
# 这样也可以
# print(readline.get_line_buffer()+'.')
cbfunc = ctypes.CFUNCTYPE(None, ctypes.c_char_p)
rllib.rl_callback_handler_install.restype = None
rllib.rl_callback_handler_install(ctypes.c_char_p(b">> "), cbfunc(callback))
while True:
rllib.rl_callback_read_char()
对象系统
在 Python 2.7 中,无法在非该类的实例上调用该类的方法,而在 Python 3(已测试: 3.2、3.3)中可以。示例:
class T(object):
def m(self):
print(self)
a = object()
T.m(a)
结果如下:
$ python2 ~tmp/t.py Traceback (most recent call last): File "/home/lilydjwg/tmpfs/t.py", line 8, in <module> T.m(a) TypeError: unbound method m() must be called with T instance as first argument (got object instance instead) $ python3 ~tmp/t.py <object object at 0x7f92f2424080>
环境变量
PYTHONSTARTUP
- 交互式解释器载入时加载的配置文件
PYTHONPATH
- 查找库文件的路径(目录),与
PATH
变量一样以冒号分隔
编译与安装
Python 3.7
#常用模块依赖
sudo apt-get install libreadline-dev libexpat1-dev libsqlite3-dev libssl-dev libncursesw5-dev
sudo apt-get install libbz2-dev liblzma-dev
../configure --enable-shared --with-threads --with-computed-gotos --enable-ipv6 --with-system-expat --with-dbmliborder=gdbm:ndbm --with-system-ffi --with-system-libmpdec --enable-loadable-sqlite-extensions --without-ensurepip --with-optimizations
make
sudo make install
# 或者不覆盖同系列
# sudo make altinstall
sudo ldconfig
Python 2.7
(安装于 CentOS 6,尚不完整)
yum install -y expat-devel
wget https://www.python.org/ftp/python/$ver/Python-$ver.7.11.tar.xz
tar xf Python-$ver.tar.xz
cd Python-$ver
./configure --enable-shared --with-threads --with-computed-gotos --enable-ipv6 --with-valgrind --with-wide-unicode --with-system-expat --with-system-ffi
make -j$(nproc)
make altinstall
echo /usr/local/lib > /etc/ld.so.conf.d/local.conf
ldconfig
# 安装 pip
wget https://bootstrap.pypa.io/get-pip.py
python2.7 get-pip.py
交叉编译
参见:交叉编译记录
编辑Modules/Setup.dist
,把需要用到的模块,去掉注释。注意模块依赖的库也要安装。注意 readline 中要去掉-ltermcap
。
配置:
# 根据实际情况配置
mkdir build-arm && cd build-arm
echo ac_cv_file__dev_ptmx=yes > config.site
echo ac_cv_file__dev_ptc=no >> config.site
export CONFIG_SITE=config.site
../configure --host=arm-dyne-linux-gnueabi --build=arm --enable-shared --disable-ipv6
开始编译:
make python
如果出现无法执行Parser/pgen
的情况,修改pyconfig.h
中SIZEOF_LONG
为正确值(在64位系统上是8
),然后编译:
rm Parser/*
make CC=gcc Parser/pgen
然后再把pyconfig.h
修改回原值并修改文件的时间戳:
touch -t 200001010000 pyconfig.h
再次进行编译。如果过程中有.o
文件格式不正确,可删除后重试:
rm Parser/*.o
touch -t 210001010000 Parser/pgen
如果最后链接时出现clock_getres
及crypt
符号找不到,可在最后的链接命令上加上-lrt -lcrypt
。
打包安装
可以将 Python 标准库打包为 zip 格式以节省空间:
# 清理不需要的文件
rm **/*.pyc?
perl-rename 's=__pycache__/([^.]+).cpython-33.pyo$=\1.pyo=' **/*.pyo
rmdir **/*(/)
zip -9r ../python33.zip .
二进制文件后缀
依据不同的编译参数,Python3.2 的可执行文件和库文件有一些后缀(按同时包含时出现的顺序): [4]
d
--with-pydebug
m
--with-pymalloc
u
--with-wide-unicode
使用sys.abiflags
可以得到这个值。
参见
相关软件
- cx_Freeze,Python编译工具,跨平台。Source Forge 上的主页
- Simplified Wrapper and Interface Generator,a software development tool that connects programs written in C and C++ with a variety of high-level programming languages.
- sage,数学工具集合
- uber/pyflame: Pyflame: A Ptracing Profiler For Python
- benfred/py-spy: Sampling profiler for Python programs
JavaScript / 浏览器端工具
- Brython, A Python 3 implementation for client-side web programming. 不需要手工编译。
- pythonium, Python 3 to Javascript translator written in Python. 需要编译,需要引入自身的代码。与 JavaScript 间的数据结构不透明。
基于 Python 平台的语言
外部链接
资源
教程
- Python中文网
- Python 官方文档中文站
- python入门指引 | shell's home
- 简明 Python 教程
- 笨办法学 Python (Learn Python The Hard Way)
- The Hitchhikerâ’s Guide to Python! — The Hitchhiker's Guide to Python
This opinionated guide exists to provide both novice and expert Python developers a best-practice handbook to the installation, configuration, and usage of Python on a daily basis.
- 用Python做科学计算
- 使用C写Python的模块 » 进出自由才是游戏者的生存之道(Python 2.x)
- All About Python and Unicode | boodebr.org(Python 2.x)
- 使用Python來控制MS Word
- 手动制作python的exe可执行程序
- Another Do-It-Yourself Framework — WebOb 1.2b2 documentation
- How To Use Linux epoll with Python (Python 3)
- Unicode之痛 — PyCoder's Weelky CN
- 使用C写Python的模块 - 进出自由,我的分享
- {翻译} 探索 Python 代码对象
- Welcome to Python by Example! — Python by Example
- [1] 依云在 python-cn 列表关于列表复制和引用的解释
- Python的C扩展 argument 1 must be string without null bytes, not str - SegmentFault, 依云对 Python 3 中 str 和 bytes 和 C 的 char* 所作的解释。
- Python list内容的改变问题 - SegmentFault
- shell909090/learn-python: 开放式的Python课程 (Jupyter 编写)
- 莫烦 Python: Python 基础、机器学习、数据处理,视频教程
- Python 工匠:善用变量来改善代码质量 | Piglei
Python 3
- 那些在 Python 3 中闪亮的 « K.I.S.S. – 简单哲学
- 10 awesome features of Python that you can't use because you refuse to upgrade to Python 3
- Steve Dower: What's coming in Python 3.5 and why you should be excited - YouTube
- Instagram 在 PyCon 2017 的演讲摘要 | Piglei. Instagram 迁移到 Python 3 的故事
设计
- Python and the Principle of Least Astonishment | Armin Ronacher's Thoughts and Writings
- python插件技巧 | shell's home
- The History of Python: The story of None, True and False (and an explanation of literals, keywords and builtins thrown in)
- The History of Python: The history of bool, True and False
- Why Python’s whitespace rule is right | Unspecified Behaviour
- The Fate of Lambda in Python 3000 and Scheme v300
- The Python I Would Like To See | Armin Ronacher's Thoughts and Writings, Revenge of the Types | Armin Ronacher's Thoughts and Writings
- Deciphering Glyph :: Unyielding: why threads and green threads are bad and explicite yielding is good.
- Chris's Wiki :: Function definition order in Python
- Timeouts and cancellation for humans — njs blog
其它
TODO
- Python程序员的进化 | 酷壳 - CoolShell.cn
- 如何成为Python高手 | 外刊IT评论
- Flask, Tornado, GEvent, 以及它们的结合的性能比较 | 吃吃喝喝
- Python 快速通介 (powered by S5)
- Try IPython from your browser: PythonAnywhere
- 几个模板系统的性能对比 | shell's home
- Python for Ruby Programmers
- Python Source Obfuscation using ASTs | Development & Security
- Ned Batchelder: Eval really is dangerous
- Restricted "safe" eval « Python recipes « ActiveState Code(可能已经过时)
- Openstack Profiler - gtt116 - 博客频道 - CSDN.NET,使用 line_profiler 和 memory_profiler profile CPU 和内存的使用
- Python性能分析指南 - 技术翻译 - 开源中国社区
- Python Is Not Java (dirtSimple.org)
- How We Deploy Python Code
- Fixing Python Memory Leaks - CloudQuant (using LD_PRELOAD and signals)