Python tips

2023-01-31 03:01:01 python Tips

0、python Enhancement Proposal。(PEP,Python增强建议书)

0.1、Python中的注释。

0.2、Python之禅。(import this

0.3、Python Cookbook 3rd Edition Documentation

0.4、第三方二进制扩展库:Unofficial windows Binaries for Python Extension Packages

0.5、PyTutor

0.6、Google Python语言规范、Python风格规范。

0.7、pip 镜像。

# 豆瓣
pip3 install psutil -i https://pypi.doubaNIO.com/simple/
# 阿里云
pip3 install psutil -i Https://mirrors.aliyun.com/pypi/simple/

1、Python用print打印html文档时,若不打印协议首部,可能无法输出html文档。

print('Content-type: text/html\r\n')

2、Python2.7 搭建简单http server,只能解析静态文件。

python2.7  -m  SimpleHTTPServer 5678

3、python3 搭建简单http server,只能解析静态文件。

python3 -m http.server 5678

4、Python2.7 搭建能处理python脚本的http server。

python2.7 -m CGIHTTPServer 5678

5、Python3 搭建能处理python脚本的http server。

from http.server import HTTPServer, CGIHTTPRequestHandler
port = 5678
httpd = HTTPServer(('', port), CGIHTTPRequestHandler)
print("Starting simple_httpd on port: " + str(httpd.server_port))
httpd.serve_forever()

6、Python的三种数据类型字典、列表、元组,分别用花括号、中括号、小括号表示。如:

字典:dic={'a':12, 'b':34}
列表:li=[1, 2, 3, 3]
集合:s = {1, 2, 3, 4}         #set是无序的无重复元素的列表
元组:tup=(1, 2, 3, 4)    #元组是不可更改的列表

8、Python打印不换行

(1)、通用方法

import sys
sys.stdout.write("no new line")

(2)、Python2 print 不换行(加逗号):print 'no new line',

(3)、Python3 print 不换行:print('no new line', end='')

9、Python 2.x 在使用help函数时,对内置函数一定要加引号

help(print)    #wrong
help('print')  #right

10、Python 模块的一般安装方法:

python setup.py install

12、全局变量若在函数内部被修改,会被编译器认为是局部变量,解决办法是在函数内用global声明这个变量。(参考这里)

13、Python打印异常信息。

try:
  #do someting
except:
  print sys.exc_info()[0],sys.exc_info()[1]  #一般来说这样就足够了

这样更好:

try:
    #do someting
except:
    import traceback
    print(traceback.fORMat_exc())
    traceback.print_exc()    #约等于上句


14、TypeError: 'str' object is not callable ,可能解决方案。

15、以Windows Service的方式运行Python程序。

16、Python 字符串操作(string替换、删除、截取、复制、连接、比较、查找、包含、大小写转换、分割等)。

17、PyQt学习入门

18、Python读取ini文件 。

22、python用win32com模拟浏览器,no module named win32com.client错误解决,InternetExplorer.Application的成员。

24、将qt4的图形界面文件转化为python格式。

25、python中的urlencode与urldecode。

26、py2exe打包选项。

27、Win7 64位编译Python扩展解决”error: Unable to find vcvarsall.bat”问题

28、几个Python配置工具简介:setuptools、pip、virtualenv 

28.1、Python 包管理工具解惑 。

29、python中获取python版本号的方法

30、查看python的搜索路径。

>>> import sys
>>> print sys.path

任何情况下都只认 sys.path!参见:分别描述python2.x和python3.x import 包时的路径搜索顺序!

当你导入一个模块,Python 解析器对模块位置的搜索顺序是:
1、当前目录
2、如果不在当前目录,Python 则搜索在 shell 变量 PYTHONPATH 下的每个目录。
3、如果都找不到,Python会察看默认路径。UNIX下,默认路径一般为/usr/local/lib/python/。
模块搜索路径存储在 system 模块的 sys.path 变量中。变量里包含当前目录,PYTHONPATH和由安装过程决定的默认目录。

31、python 的日志logging模块。

33、Python中计时器/定时器/计划任务,Timer/sched/APScheduler,参考这里。注意这篇博文中所用APScheduler应该为V2,V3的APScheduler有很多改动。

34、2015-5-7决定向Python3迁移。

35、Python3中str与bytes转换:The bytes/str dichotomy in Python 3

36、安装IPython

pip install ipython

安装IPython的Tab补全插件

pip install pyreadline

37、Python自定义排序

(1)、python 内建排序 HOW TO

(2)、Python中sorted()方法的用法

38、Python中configparser的bom问题,将'utf8'换为'utf-8-sig'即可。参见:configparser读取含有中文的配置(Windows)

39、whell文件(名)的格式:PEP 0427 -- The Wheel Binary Package Format 1.0

39.1、本机python的兼容性可以用这样查看:({python tag}-{abi tag}-{platform tag})

>>> import pip
>>> from pprint import pprint
>>> pprint(pip.pep425tags.get_supported())
[('cp34', 'none', 'win_amd64'),
 ('py3', 'none', 'win_amd64'),
 ('cp34', 'none', 'any'),
 ('cp3', 'none', 'any'),
 ('cp33', 'none', 'any'),
 ('cp32', 'none', 'any'),
 ('cp31', 'none', 'any'),
 ('cp30', 'none', 'any'),
 ('py34', 'none', 'any'),
 ('py3', 'none', 'any'),
 ('py33', 'none', 'any'),
 ('py32', 'none', 'any'),
 ('py31', 'none', 'any'),
 ('py30', 'none', 'any')]

40、Python内置模块/函数C代码查看:https://hg.python.org/cpython/branches

41、Python内存相关(GC):gc模块–Python内存释放、Python深入06 Python的内存管理 。

42、Python 更改cmd中的字色 。

43、好玩的运算精度问题。

>>> 33/22
1.5
>>> 3.3/2.2
1.4999999999999998
>>> 33/15
2.2
>>> 3.3/1.5
2.1999999999999997
>>> 2-1.1
0.8999999999999999

44、Python not 对象True/False 的问题:Why is “if not someobj:” better than “if someobj == None:” in Python?

45、怎样忽略警告(不打印烦人的警告)。warnings.filterwarnings

import warnings
warnings.filterwarnings("ignore")

46、Python打印到终端同时记录到文件(tee)。(How do I duplicate sys.stdout to a log file in python?)

class Tee(object):
	def __init__(self):
		self.terminal = sys.stdout
		self.log = open("log.log", "a")
		
	def __del__(self):
		sys.stdout = self.terminal
		self.log.close()

	def write(self, message):
		self.terminal.write(message)
		self.log.write(message)  
		self.log.flush()
		
sys.stdout = Tee()

print('HaHaHa')


N、...


*** walker * Updated 2017-05-07 ***


相关文章