most in Chinese, rest in English

0%

Python中有用的package和tricks

本文介绍一下Python 3 中的一些自己平时用到觉得不错的package。以及一些可能会有用的function以及tricks。更多内容可见本人github code repository

Python的管理工具和打包发布

Python 包管理工具

  1. distutils:distutils 是 python 标准库的一部分,2000年发布。使用它能够进行 python 模块的 安装 和 发布。setup.py 就是利用 distutils 的功能写成。
  2. setuptools 和 distribute:setuptools 是一个为了增强 distutils 而开发的集合,2004年发布。它包含了 easy_install 这个工具。ez_setup.py 是 setuptools 的安装工具。ez 就是 easy 的缩写。简单的说,setuptools 是一个项目的名称,是基础组件。而 easy_install 是这个项目中提供的工具,它依赖基础组件工作。使用 setuptools 可以自动 下载、构建、安装和管理 python 模块。比如
    1
    easy_install SQLObject
    distribute 是 setuptools 的一个分支版本。分支的原因可能是有一部分开发者认为 setuptools 开发太慢了。但现在,distribute 又合并回了 setuptools 中。因此,我们可以认为它们是同一个东西。事实上,如果你查看一下 easy_install 的版本,会发现它本质上就是 distribute 。
  3. distutils2 被设计为 distutils的替代品。从2009年开发到2012年。它包含更多的功能,并希望以 packaging 作为名称进入 python 3.3 成为标准库的一部分。但这个计划 后来停滞了 。distlib 是 distutils2 的部分,它为 distutils2/packaging 提供的低级功能增加高级 API,使其便于使用。
  4. pip 是目前 python 包管理的事实标准,2008年发布。它被用作 easy_install 的替代品,但是它仍有大量的功能建立在 setuptools 组件之上。
    pip 希望不再使用 Eggs 格式(虽然它支持 Eggs),而更希望采用“源码发行版”(使用 python setup.py sdist 创建)。这可以充分利用 Requirements File Format 提供的方便功能。
    pip 可以利用 requirments.txt 来实现在依赖的安装。在 setup.py 中,也存在一个 install_requires 表来指定依赖的安装。与easy_install相比, pip 明显优势更大。

    egg 和 whl 的关系

  • Eggs 格式是 setuptools 引入的一种文件格式,它使用 .egg 扩展名,用于 Python 模块的安装。 setuptools 可以识别这种格式。并解析它,安装它。
  • wheel 本质上是一个 zip 包格式,它使用 .whl 扩展名,用于 python 模块的安装,它的出现是为了替代 Eggs。wheel 还提供了一个 bdist_wheel 作为 setuptools 的扩展命令,这个命令可以用来生成 wheel 包。

brew 和conda

conda是一个包,依赖和环境管理工具,适用于多种语言,conda默认随miniconda或anaconda发行,因此要安装conda,只需要安装miniconda或anconda即可。而brew是Homebrew的管理工具。两者都是用来自动下载并安装包的,并不仅限于安装Python 依赖包。这是两者与pip的区别。

安装package

全自动: pip install xxx
半自动:一些python package 有setup.py xx install . 可以follow 这些instruction
如果没有write access 到site-package 这种系统路径,需要把package 安装到自己设定的路径下面去,具体参考网页https://docs.python.org/2.7/install/index.html#alternate-installation
举例来说,
python setup.py install —home=/home/ec2-user/workspaces/hoverboard-workspaces/lib

此时,需要把当前目录装在PYTHONPATH里面去,
vim ~/.bashrc
PYTHONPATH=~/one/location:$PYTHONPATH
PYTHONPATH=~/second/location:$PYTHONPATH
export PYTHONPATH
source ~/.bashrc
如果有其他依赖的话,还需要安装其他依赖package.
如果在断网的情况下pip install package, 为了避免平台问题(比如amazon hoverboard),需要先把package 所有依赖的源代码下载下来,然后再安装。具体见:
https://www.cnblogs.com/jay54520/p/8330994.html
https://www.jianshu.com/p/f8a3f3a07aff
https://blog.csdn.net/pierre_/article/details/54234151
http://imshuai.com/python-pip-install-package-offline-tensorflow/
https://woshigaozheng.blogspot.com/2019/06/python-package.html

如果感觉没有问题,比如wheel 文件适合当前平台的话,可以download 对应的whl文件,然后通过
pip install —user -v —no-index “${WHEELS_LOC}certifi-2018.11.29-py2.py3-none-any.whl”
进行安装

Package篇

transformers

非常强大的预训练模型:https://huggingface.co/models?search=chinese
如果出现如下error https://github.com/huggingface/transformers/issues/2831 ,安装tokenizers:

1
pip install tokenizers

否则直接install transformers
1
pip install transformers

flair

gephi

tqdm

GluonNLP

Based on MXNet: https://gluon-nlp.mxnet.io/
示例代码:

1
2
3
4
from tqdm import tqdm
from time import sleep
for i in tqdm(range(1, 500)):
sleep(0.01)

logging

pdb

argparse

Tricks篇

打印输出

1
2
3
print("current line count %d"%(line_count))
print(f"current line count {line_count}")
print("current line count {}".format(line_count))

exception, raise, assert, 和warning

init.py的作用

init, new, call 函数解析

all 变量用法

类方法和静态方法区别

类变量与实例变量

代码换行

args *kwargs装饰器

列表、元组或字典前面的星号

列表、元组前面加星号作用是将列表解开成两个独立的参数,传入函数,字典前面加两个星号,是将字典解开成独立的元素作为形参。详情可见 https://www.pythonf.cn/read/90543 讲解。

装饰器@用法: property

lambda函数

all, any函数

map函数

yield函数

yield是用于生成器。什么是生成器,你可以通俗的认为,在一个函数中,使用了yield来代替return的位置的函数,就是生成器。它不同于函数的使用方法是:函数使用return来进行返回值,每调用一次,返回一个新加工好的数据返回给你;yield不同,它会在调用生成器的时候,把数据生成object,然后当你需要用的时候,要用next()方法来取,同时不可逆。你可以通俗的叫它”轮转容器”,可用现实的一种实物来理解:水车,先yield来装入数据、产出generator object、使用next()来释放;好比水车转动后,车轮上的水槽装入水,随着轮子转动,被转到下面的水槽就能将水送入水道中流入田里。

condition的表达

最大值最小值的表达

float(“inf”) + 1还是float(“inf”), 但是sys.maxsize + 1就不是sys.maxsize了, 某些需要判断+1以后是不是还是max的时候要注意

python write to file buffer

https://stackoverflow.com/questions/18984092/python-2-7-write-to-file-instantly
https://stackoverflow.com/questions/3167494/how-often-does-python-flush-to-a-file
Use flush() or

For file operations, Python uses the operating system’s default buffering unless you configure it do otherwise. You can specify a buffer size, unbuffered, or line buffered.
For example, the open function takes a buffer size argument.
http://docs.python.org/library/functions.html#open
“The optional buffering argument specifies the file’s desired buffer size: 0 means unbuffered, 1 means line buffered, any other positive value means use a buffer of (approximately) that size. A negative buffering means to use the system default, which is usually line buffered for tty devices and fully buffered for other files. If omitted, the system default is used.”
bufsize = 0
f = open(‘file.txt’, ‘w’, bufsize)

其他函数

  • list: copy (深度copy 需要copy package)
  • deque: 从两端都可以加入
  • sorted
  • map
  • zip
  • set
  • OrderedDict
  • defaultdict
  • heapq
  • bisect
  • Counter
  • String: ord, char
  • itertools: permutations, combinations, groupby
  • functools

待整理部分

If you can’t see the virtualenvwrapper.sh. First uninstall virtualenvwrapper. It will show messages about the location of the virtualenvwrapper.sh file.

Check all python’ location

type -a python

首选要确定哪个版本的python 要使用(不同的python版本可能存在在不同的地方)
可以用module load python/2.7 用module查看有几个版本的python

首先install virtualenv:
pip install virtualenv

新建一个sandbox of virtual environment:
virtualenv —python=/l/python2.7/bin/python env1 (silo 上面 module load的 python 不是系统自带的,也不是anaconda上面的)

有些python 用的是anaconda的,所以需要指定python路径

把当前路径设为指定的virtual envir:

cd env1/
source bin/activate
which python
pip list //看一下安装的包都有什么
pip freeze// python 所有依赖的包

退出虚拟环境:
deactivate

如果你的虚拟环境想用系统已有的包,when you create it, you need to use
virtualenv —system-site-packages venv

Otherwise, if you don’t want to use system package, create it using:

virtualenv venv –no-site-packages (和以上的基本一样)

2.3. 指定PYTHON版本
可以使用-p PYTHON_EXE选项在创建虚拟环境的时候指定python版本

创建python2.7虚拟环境

➜ Test git:(master) ✗ virtualenv -p /usr/bin/python2.7 ENV2.7
Running virtualenv with interpreter /usr/bin/python2.7
New python executable in ENV2.7/bin/python
Installing setuptools, pip…done.

创建python3.4虚拟环境

➜ Test git:(master) ✗ virtualenv -p /usr/local/bin/python3.4 ENV3.4
Running virtualenv with interpreter /usr/local/bin/python3.4
Using base prefix ‘/Library/Frameworks/Python.framework/Versions/3.4’
New python executable in ENV3.4/bin/python3.4
Also creating executable in ENV3.4/bin/python
Installing setuptools, pip…done.

作者:Andrew_liu
链接:http://www.jianshu.com/p/08c657bd34f1
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

使用VIRTUALENVWRAPPER (更简单的方法)

http://codingpy.com/article/virtualenv-must-have-tool-for-python-development/
http://qicheng0211.blog.51cto.com/3958621/1561685
http://www.jianshu.com/p/44ab75fbaef2
http://liuzhijun.iteye.com/blog/1872241

man 在Linux中可以有文档查看的功能 i.e.
man ls
man python
参考文献