0%

构建Python开源包

项目创建

按照需求,一般需要创建如下目录及文件:

1
2
3
4
5
6
7
$ tree .
.
├── LICENSE
├── README.md
├── docs
├── example
└── tests

搭建虚拟运行环境

在搭建自己的库的时候,如果希望有一个干净的项目环境的,可以使用virtualenv。
方便为后面生成私有项目的 requirement.txt 依赖包文件。

可以使用source ~/virtual/bin/activate进入环境,使用pip freeze > requirement.txt来生成依赖包文件。

编写项目代码

这一步主要为构建代码的原型架构以及代码,每个包都需要包含一个’init.py’文件。

编写安装脚本

在包的根目录创建文件setup.py,内容一般如下所示:

.. literalinclude:: ../../src/python-package-demo/setup.py

编写文档

进入docs文件夹,运行sphinx-quickstart,然后编写相应的rst文件即可。

默认的index.rst的内容如下:

.. literalinclude:: ../../../src/python-package-demo/docs/index.rst

Python 创建文件夹

os.mkdir() 方法

os.mkdir() 方法用于以数字权限模式创建目录。默认的模式为 0777 (八进制)。

如果目录有多级,则创建最后一级,如果最后一级目录的上级目录有不存在的,则会抛出一个 OSError。
语法

mkdir()方法语法格式如下:

1
os.mkdir(path[, mode])

参数

  • path – 要创建的目录,可以是相对或者绝对路径。
  • mode – 要为目录设置的权限数字模式。

返回值

该方法没有返回值。

实例
以下实例演示了 mkdir() 方法的使用:

.. literalinclude:: ../../src/python-mkdir.py

执行以上程序输出结果为:

1
Created

Git核弹级指令

核弹级这个概念应该是Pro Git的作者说的,这就说明了这个指令的强悍指出,除非特别清楚你准备干什么。

否则不要抱着尝试的想法来试试,除非你想尝试下核弹的威力。

其实用到这个指令主要是因为以前是自己做的局域网Git,文件的大小没有多少限制,比如几百兆的文件我也给放上去了。

但是碰到将这个repo公开到github上的时候,就碰到这个问题了。

1
remote : error: GH001: Large files detected.

当然处理方法还是有的,就是使用git的lfs,不过不太像折腾,索性使用核弹指令。

具体的命令如下,其中big_file为准备删除的文件,含义就是讲大文件从以前8次的提交中删除,
注意是从提交中删除,所以意思就是以前所有的提交都是可以修改的,OMG,这是什么意思,让我静静。

1
2
$ git filter-branch --index-filter 'git rm --cached --ignore-unmatch big_file1' HEAD~8..HEAD
$ git filter-branch -f --index-filter 'git rm --cached --ignore-unmatch big_file2' HEAD~8..HEAD

核武器级选项:filter-branch

有另一个历史改写的选项,如果想要通过脚本的方式改写大量提交的话可以使用它——例如,全局修改你的邮箱地址或从每一个提交中移除一个文件。 这个命令是 filter-branch,它可以改写历史中大量的提交,除非你的项目还没有公开并且其他人没有基于要改写的工作的提交做的工作,否则你不应当使用它。 然而,它可以很有用。 你将会学习到几个常用的用途,这样就得到了它适合使用地方的想法。

Caution git filter-branch 有很多陷阱,不再推荐使用它来重写历史。 请考虑使用 git-filter-repo,它是一个 Python 脚本,相比大多数使用 filter-branch 的应用来说,它做得要更好。它的文档和源码可访问 https://github.com/newren/git-filter-repo 获取。

从每一个提交中移除一个文件

这经常发生。 有人粗心地通过 git add . 提交了一个巨大的二进制文件,你想要从所有地方删除。 可能偶然地提交了一个包括一个密码的文件,然而你想要开源项目。 filter-branch 是一个可能会用来擦洗整个提交历史的工具。 为了从整个提交历史中移除一个叫做 passwords.txt 的文件,可以使用 --tree-filter 选项给 filter-branch

1
2
3
$ git filter-branch --tree-filter 'rm -f passwords.txt' HEAD
Rewrite 6b9b3cf04e7c5686a9cb838c3f36a8cb6a0fc2bd (21/21)
Ref 'refs/heads/master' was rewritten

--tree-filter 选项在检出项目的每一个提交后运行指定的命令然后重新提交结果。 在本例中,你从每一个快照中移除了一个叫作 passwords.txt 的文件,无论它是否存在。 如果想要移除所有偶然提交的编辑器备份文件,可以运行类似 git filter-branch --tree-filter 'rm -f *~' HEAD 的命令。

最后将可以看到 Git 重写树与提交然后移动分支指针。 通常一个好的想法是在一个测试分支中做这件事,然后当你决定最终结果是真正想要的,可以硬重置 master 分支。 为了让 filter-branch 在所有分支上运行,可以给命令传递 --all 选项。

使一个子目录做为新的根目录

假设已经从另一个源代码控制系统中导入,并且有几个没意义的子目录(trunktags 等等)。 如果想要让 trunk 子目录作为每一个提交的新的项目根目录,filter-branch 也可以帮助你那么做:

1
2
3
$ git filter-branch --subdirectory-filter trunk HEAD
Rewrite 856f0bf61e41a27326cdae8f09fe708d679f596f (12/12)
Ref 'refs/heads/master' was rewritten

现在新项目根目录是 trunk 子目录了。 Git 会自动移除所有不影响子目录的提交。

全局修改邮箱地址

另一个常见的情形是在你开始工作时忘记运行 git config 来设置你的名字与邮箱地址, 或者你想要开源一个项目并且修改所有你的工作邮箱地址为你的个人邮箱地址。 任何情形下,你也可以通过 filter-branch 来一次性修改多个提交中的邮箱地址。 需要小心的是只修改你自己的邮箱地址,所以你使用 --commit-filter

1
2
3
4
5
6
7
8
9
$ git filter-branch --commit-filter '
if [ "$GIT_AUTHOR_EMAIL" = "schacon@localhost" ];
then
GIT_AUTHOR_NAME="Scott Chacon";
GIT_AUTHOR_EMAIL="schacon@example.com";
git commit-tree "$@";
else
git commit-tree "$@";
fi' HEAD

这会遍历并重写每一个提交来包含你的新邮箱地址。 因为提交包含了它们父提交的 SHA-1 校验和,这个命令会修改你的历史中的每一个提交的 SHA-1 校验和, 而不仅仅只是那些匹配邮箱地址的提交。

How to install Intel IPP on Ubuntu

I wanted to try Intel Integrated Performance Primitives (IPP) with OpenCV. I installed IPP with these steps:

Intel IPP can be downloaded from here. If you are using it for non-commercial purposes, you can get it for free through Intel’s Non-Commercial Software Development webpage. You need to register with an email address. You will be sent an email with the download link and a registration key.

Download the Intel IPP version you want, beware that its a huge download. I downloaded Intel IPP 7.1, which ships as a 777MB .tgz file.

Unzip the downloaded .tgz file. Run the install.sh file. You will be asked to enter your registration key.

The installer walks you through the steps of installing IPP. I was asked to install the gcc-multilib package, before I could proceed. So, I did:

1
$ sudo apt-get install gcc-multilib

By default, the IPP files are installed to /opt/intel/

Going by the steps given on the Intel website, you are supposed to run the ippvars.sh script, which is in the /opt/intel/ipp/bin directory. It sets the following environment variables: IPPROOT, LIBRARY_PATH and LD_LIBRARY_PATH. This script failed to work for me. So, I set those manually in my .bashrc:

1
2
3
4
# My .bashrc
export IPPROOT=/opt/intel/composer_xe_2013.1.117/ipp
export LIBRARY_PATH=$LIBRARY_PATH:/opt/intel/composer_xe_2013.1.117/ipp/lib/intel64:/opt/intel/composer_xe_2013.1.117/compiler/lib/intel64
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/opt/intel/composer_xe_2013.1.117/ipp/lib/intel64:/opt/intel/composer_xe_2013.1.117/compiler/lib/intel64

linux后台执行命令:&和nohup

当我们在终端或控制台工作的时候,如果希望同时进行另外一个工作,那么此时就希望将当前的工作放在后台运行,特别是如果当前的命令执行时间会很长的情况下。

那么下面就说下两种方法。

&

命令后面加上&相信很多人都用过,比较适合一些费时的命令或脚本,不过注意不要讲有交互的命令放在后台执行,因为这样你的机器就会一直等待输入。使用方法为

1
$ command &

如果放在后台的作业会产生大量的输出,那么此时最好可以将输出重定向到某个文件中,使用方法为

1
$ command > out.file 2>&1 &

其中2>&1 表示将标准出错重定向到标准输出

不过使用&有的弊端,就是一旦把当前的控制台关掉或者账号退出时就会停止作业。

此时就需要下面的这个命令了。

nohup

nohup命令可以在退出账户后继续执行相应的命令,意思为不挂起no hang up。

命令的使用方法如下:

1
$ nohup command &

默认情况下作业的所有输出被重定向到一个nohup.out文件中,也可以指定输出文件,如下所示:

1
$ nohup command > mynohup.file 2>&1 &

不过这里还是有需要注意的事项,非正常退出可能会导致命令失效,所以需要使用exit正常退出当前账户。

使用iperf测试网络性能

Iperf是美国伊利诺斯大学(University of Illinois)开发的一种开源的网络性能测试工具。
可以用来测试网络节点间(也包括回环)TCP或UDP连接的性能,包括带宽、抖动以及丢包率
其中抖动和丢包率适应于UDP测试,而带宽测试适应于TCP和UDP

TCP支持:

  • Measure bandwidth
  • Report MSS/MTU size and observed read sizes.
  • Support for TCP window size via socket buffers.
  • Multi-threaded if pthreads or Win32 threads are available. Client and server can have multiple simultaneous connections.

UDP支持:

  • Client can create UDP streams of specified bandwidth.
  • Measure packet loss
  • Measure delay jitter
  • Multicast capable
  • Multi-threaded if pthreads are available. Client and server can have multiple simultaneous connections.

所以,服务端通过-u来区分监听协议,而TCP协议不能计算时延与丢包率,而且不能指定发送带宽。

服务端

1
$ iperf3 -s

客户端

1
2
$ iperf3 -c IP
$ iperf3 --client IP --bandwidth 900M --window 1M

需要使用TCP来测试带宽的时候,需要指定TCP的窗口大小,

窗口的大小即网络通道的容量 capacity = bandwidth x round-trip-time

其中round-trip-time可以通过ping来得到。

参数

1
2
3
4
5
6
7
8
-c --client 标记客户端
-i --interval 设定输出值间隔
-u --udp 使用传输协议为UDP
-t --time 设定传输时间
-F --file 指定传输文件
-P --parallel 指定进程数
-b --bandwidth 指定带宽
-w --window 指定window大小,For TCP, this sets the TCP window size;For UDP it is just the buffer which data

Django 忘记管理员或忘记管理员密码

第一步:运行django shell

1
python3 manage.py shell

第二步:重设密码

1
2
3
4
5
>>> from django.contrib.auth.models import User
>>> user = User.object.get(username='your_account')
>>> user.set_password('your_new_password')
>>> user.save()
>>> quit()

如果你连管理员用户名都忘了。。。

1
2
3
4
5
6
7
>>> from django.contrib.auth.models import User
>>> user = User.object.get(pk=1)
>>> user
<User: you_account>
>>> user.set_password('your_new_password')
>>> user.save()
>>> quit()

git chekcout

请务必记得 git checkout -- <file> 是一个危险的命令。 你对那个文件在本地的任何修改都会消失——Git 会用最近提交的版本覆盖掉它。 除非你确实清楚不想要对那个文件的本地修改了,否则请不要使用这个命令。

分支切换

要切换到一个已存在的分支,你需要使用 git checkout 命令。 我们现在切换到新创建的 testing 分支去:

1
$ git checkout testing

这样 HEAD 就指向 testing 分支了。

检出特定的标签tag

如果你想查看某个标签所指向的文件版本,可以使用 git checkout 命令, 虽然这会使你的仓库处于“分离头指针(detached HEAD)”的状态——这个状态有些不好的副作用:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
$ git checkout 2.0.0
Note: checking out '2.0.0'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

git checkout -b <new-branch>

HEAD is now at 99ada87... Merge pull request #89 from schacon/appendix-final

$ git checkout 2.0-beta-0.1
Previous HEAD position was 99ada87... Merge pull request #89 from schacon/appendix-final
HEAD is now at df3f601... add atlas.json and cover image

在“分离头指针”状态下,如果你做了某些更改然后提交它们,标签不会发生变化, 但你的新提交将不属于任何分支,并且将无法访问,除非通过确切的提交哈希才能访问。 因此,如果你需要进行更改,比如你要修复旧版本中的错误,那么通常需要创建一个新分支:

1
2
$ git checkout -b version2 v2.0.0
Switched to a new branch 'version2'

如果在这之后又进行了一次提交,version2 分支就会因为这个改动向前移动, 此时它就会和 v2.0.0 标签稍微有些不同,这时就要当心了。

建立本地分支并将远程分支拉取至本地

1
$ git checkout -b 本地分支名 origin/远程分支名

Git 中文

git在显示的过程中,中文可能会乱码,解决方法如下

git status 乱码

解决方法:
git config --global core.quotepath false

git commit 乱码

解决方法:
git config --global i18n.commitencoding utf-8

git status 乱码

解决方法:
git config --global i18n.logoutputencoding utf-8

注意:如果是Linux系统,需要设置环境变量 export LESSCHARSET=utf-8

CODE contribution policy

Everybody is encouraged to help improve the quality of the code.
You can help by reporting issues or even better fix issues yourself.

If you have a patch for CODE we use the github pull request system.

Also to keep the CODE code quality high we have written down some guidelines for contributing, see below.

General considerations

If you have problems or questions with/about git or github, first check [1].
If you modify any code, make sure the test suite still runs (make test). If it fails, fix the code or the relevant test.
If you change a function/method signature, update the doxygen documentation accordingly.
If you add a function or method, add a test for it add it to the doxygen documentation.
The tested code coverage line count should increase, not decrease.
Follow the google style code as much as possible [2].

Contribution procedure

Create a github account, setup SSH keys.
Fork the CODE repository on github [3].
Create a branch and commit your changes to this branch.
Push your branch to your github fork (not the original CODE, you probably don’t have permission).
Issue a pull request [4].
When the pull request is reviewed and there are no problems it will be accepted. Merging a pull request should always happen by someone else.
It can happen there are some mistakes here and there, we use the github commenting system to discuss these issues.
If there is a problem with the commit, you need to fix it. You can commit to the same branch, the PR will be updated automatically.

General notes about Pull requests

Please create descriptive commits containing atomic changes. Use short commit messages (try 50 characters, max is 70 characters); longer commit messages are possible in the body of the commit message (separated from the subject by an empty line). See [5].
If you fix an existing github issue, reference it in the commit message body, e.g. ‘Fixes #41’.
If your Pull request does not refer to an existing github issue, it is not necessary to create one (because github will present the PR just like an issue).
You can rewrite the history of the commits in your branch using rebase, but don’t rewrite the history before the first commit of your new branch.
We like to keep the history clean, and prevent a lot of ‘fix typo’ messages.
If you rewrite your history of your branch you can force push those changes to your branch. The PR will be updated. See e.g. [6].

[1] http://help.github.com
[2] https://google-styleguide.googlecode.com/svn/trunk/cppguide.html
[3] https://help.github.com/articles/fork-a-repo/
[4] https://help.github.com/articles/creating-a-pull-request/
[5] https://chris.beams.io/posts/git-commit
[6] https://developer.atlassian.com/blog/2015/04/force-with-lease/

Refer

https://github.com/casacore/casacore/blob/master/CONTRIBUTING.md