python
python的相关问题
python编码规范
import this
The Zen of Python
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
----
python之禅
优美胜于丑陋(Python 以编写优美的代码为目标)
明了胜于晦涩(优美的代码应当是明了的,命名规范,风格相似)
简洁胜于复杂(优美的代码应当是简洁的,不要有复杂的内部实现)
复杂胜于凌乱(如果复杂不可避免,那代码间也不能有难懂的关系,要保持接口简洁)
扁平胜于嵌套(优美的代码应当是扁平的,不能有太多的嵌套)
间隔胜于紧凑(优美的代码有适当的间隔,不要奢望一行代码解决问题)
可读性很重要(优美的代码是可读的)
即便假借特例的实用性之名,也不可违背这些规则(这些规则至高无上)
不要包容所有错误,除非你确定需要这样做(精准地捕获异常,不写 except:pass 风格的代码)
当存在多种可能,不要尝试去猜测
而是尽量找一种,最好是唯一一种明显的解决方案(如果不确定,就用穷举法)
虽然这并不容易,因为你不是 Python 之父(这里的 Dutch 是指 Guido )
做也许好过不做,但不假思索就动手还不如不做(动手之前要细思量)
如果你无法向人描述你的方案,那肯定不是一个好方案;反之亦然(方案测评标准)
命名空间是一种绝妙的理念,我们应当多加利用(倡导与号召)python 编码规范
python数据结构时间复杂度
按字符处理字符串
常用字符串
isinstance可以判断多个类型
对齐
join
将字符串按照自己定义的间隔分割
split分割字符串
对象比较
同行间隔字符串会自动连接
三个引号注意最后的引号
万物皆对象
切片
创建有默认值的dict
格式化字符串
r字符的一些问题
python新旧class
函数式
from XXX import *
类方法,静态方法,普通方法
python私有方法
异常
python解析
filter为真的list
property
getattr getattribute getattr
__setattr__的递归问题
向文件添加一行 print
python cook
groupby的用法
例如获取一个已经排序的list的每个group的最新一条
wraps
使用wrap写装饰器的时候会把doc之类的东西带上
Python: Why to use @wraps with decorators? http://artemrudenko.wordpress.com/2013/04/15/python-why-you-need-to-use-wraps-with-decorators/
future
yield
协程
contextmanager 实际上是你被修饰的func yied出某变量,其他的地方就可以使用with了。 上问就是yield前的东西,下文就是yield后的东西。
@contextlib.contextmanager
文档def some_generator(): try: yield finally: with some_generator() as : 也就是:
例子lock = threading.Lock() @contextmanager ... def openlock(): ... print('Acquire') ... lock.acquire() ... yield ... print('Releasing') ... lock.release() ... with openlock(): ... print('Lock is locked: {}'.format(lock.locked())) ... print 'Do some stuff' ... Acquire Lock is locked: True Do some stuff Releasing
PIL
mysqldb intall problem
problem: mysql_config not found when installing mysqldb python interface
scrapy intall problem
problem: cryptography安装问题
python 动态导入 importlib
有的时候需要使用字符串动态导入一些东西,使用python自带的importlib可以实现这个需求 这种变态的需求我只遇到过一次,是在以前写教程的时候做的,mark。
python 脚本退出时执行某方法
使用atexit
查找文件
glob
glob2
编码问题
将已经是unicode的字符串直接搞成str,就是下面的ss去掉前面的u
发布package到pypi
首先发布的官方文档, setup.py的文档
首先请到testpypi和pypi注册,注意注册后需要去邮箱激活邮件,那时才算注册成功,否则会401的。
https://packaging.python.org/guides/using-testpypi/
https://packaging.python.org/guides/migrating-to-pypi-org/
testpypi是你正式发布到pypi的一个测试服务器,可以帮你check你是不是可以成功发布。
pypi发布的配置文件如下,需要将他cp到 ~/.pypirc
package发布时的文件结构,参考这个脚手架
LICENSE可以用lice生成
setup.cfg 是指明你README的
requirements.txt是指你项目的依赖
setup.py安装脚本, 找个其他项目参考写一下就可以.
发布命令
还有一种方式
with open两个或者多个文件
Mysql-python==1.2.4安装问题
有的时候装不上 问题是卡在了 distribute这里, 需要一个一个装下面的东西
setuptools==0.9.8
distribute==0.6.29
Mysql-python==1.2.4
flake8
flake8的配置是放在 ~/.config/flake8这个位置的,尴尬的是一开始加了.
时间戳
有些地方时间存储是用时间戳来做的,查数据的时候经常需要转换,贴在这倒是复制粘贴替换即可
exception 带错误堆栈
logging.error(e, exc_info=True)
程序退出执行某个方法
用 atexit 的 register 方法 import atexit
查询一个对象是否可以迭代
python calendar 日期转化
分段遍历 list
一段超级常用的代码
gevent 安装
pip install --upgrade setuptools
supervisor更新配置
conf文件更新后需要执行
reread, 然后执行update, 这样不需要重启进程
直接执行reload也可以,不过需要重启进程
celery 版本问题
celery==3.1.25对 gevent 有依赖 bug 需要首先 pip install celery[gevent] zsh 的话pip install celery[gevent] 然后 pip install gevent<1.3 zsh 的话 pip install gevent<1.3
ipython editor to vim
使用三方镜像快速安装一个包
Last updated
Was this helpful?