您的位置:首页 > 编程语言 > Python开发

笔记:HeadFirstPython(2)共享你的代码

2016-03-20 00:00 751 查看
摘要: HeadFirstPython(中文版) 学习笔记:第二章 共享你的代码

本章目标:Python代码重用。将可重复使用的函数放到一个模块中,并将模块发布出来,并放到Python软件共享网站。

一、Python 代码注释方法:

1、多行注释,使用三重引号(单引号和双引号都可以)。

2、单行注释使用#符号。

二、创建和使用模块,将函数写入一个后缀名为.py的文件中并保存,就做成了一个模块,将模块放入Python搜索路径中就可以通过import引入了,Python的搜索路径是指哪里呢?可以在Python Shell 中输入import sys;sys.path看到。通常类似以下内容:

['', 'D:\\Python35\\Lib\\idlelib', 'D:\\Python35\\python35.zip', 'D:\\Python35\\DLLs', 'D:\\Python35\\lib', 'D:\\Python35', 'D:\\Python35\\lib\\site-packages']

使用模块时,先用import引用,然后再使用模块中的函数,通常有以下两种方法:

1、只简单的使用import引用。

import 模块名    #引用模块
模块名.函数名()   #调用模块中的函数

在这里,我们可以看出,只是简单使用import引用时,调用模块中的函数,需要在函数前写上函数所在的模块名。

2、使用from 模块名 import 函数名 方式引用。

from 模块名 import 函数名         #引用模块并声明引用函数
函数名()                         #调用模块中的函数,不再需要模块名。

这种引用方式虽然可以在调用函数时不再需要写模块名,但是如果当前命名空间中已经有一个和这函数同名的函数,就会被引用的函数覆盖掉。

三、发布(distribution)模块

1、为模块创建一个文件夹,并将模块文件复制到文件夹中

2、在文件夹中创建文件setup.py,在文件中包含有关发布的元数据。

#例子
from distutils.core import setup
setup(
name         =    'lumNester',
version      =    '1.0.0',
py_modules   =    ['lumNester'],
author       =    'luming',
author_email =    'luming@gmail.com',
url          =    'http://ww.test.com',
description  =    'A Test',
)

3、构建一个发布文件:在终端窗口中,使用命令python setup.py sdist 。

结果大概如下:

running sdist
running check
warning: sdist: manifest template 'MANIFEST.in' does not exist (using default file list)

warning: sdist: standard file not found: should have one of README, README.txt

writing manifest file 'MANIFEST'
creating lumNester-1.0.0
making hard links in lumNester-1.0.0...
hard linking lumNester.py -> lumNester-1.0.0
hard linking setup.py -> lumNester-1.0.0
creating dist
creating 'dist\lumNester-1.0.0.zip' and adding 'lumNester-1.0.0' to it
adding 'lumNester-1.0.0\lumNester.py'
adding 'lumNester-1.0.0\PKG-INFO'
adding 'lumNester-1.0.0\setup.py'
removing 'lumNester-1.0.0' (and everything under it)

4、将发布安装到你的Python中。

在终端窗口中,使用命令 python setup.py install (如果是在linux中可能会需要sudo)。

结果大概类似如下内容:

running install
running build
running build_py
creating build
creating build\lib
copying lumNester.py -> build\lib
running install_lib
copying build\lib\lumNester.py -> d:\Python35\Lib\site-packages
byte-compiling d:\Python35\Lib\site-packages\lumNester.py to lumNester.cpython-35.pyc
running install_egg_info
Writing d:\Python35\Lib\site-packages\lumNester-1.0.0-py3.5.egg-info

5、将模块上传到PyPI。

(1)如果第一次使用PyPI,先到pypi.python.org网站注册一个用户。注册好之后,在终端窗口中,使用命令python setup.py register。

running register
running check
We need to know who you are, so please choose either:
1. use your existing login,
2. register as a new user,
3. have the server generate a new password for you (and email it to you), or
4. quit
Your selection [default 1]:
1
Username:  #这里输入在网站注册的用户名密码
Password:
Registering lumNester to https://pypi.python.org/pypi Server response (200): OK
I can store your PyPI login so future submissions will be faster.
(the login will be stored in D:\emacs\emacshome\.pypirc)
Save your login (y/N)?y  #把用户密码信息保存,以后就不用再来这一步了。

(2)上传模块,使用命令python setup.py sdist upload。以后如果模块做了修改,那么要修改setup.py中version(即版本号),然后重新运行这个命令。

running sdist
running check
warning: sdist: manifest template 'MANIFEST.in' does not exist (using default file list)
warning: sdist: standard file not found: should have one of README, README.txt
writing manifest file 'MANIFEST'
creating lumNester-1.0.0
making hard links in lumNester-1.0.0...
hard linking lumNester.py -> lumNester-1.0.0
hard linking setup.py -> lumNester-1.0.0
creating 'dist\lumNester-1.0.0.zip' and adding 'lumNester-1.0.0' to it
adding 'lumNester-1.0.0\lumNester.py'
adding 'lumNester-1.0.0\PKG-INFO'
adding 'lumNester-1.0.0\setup.py'
removing 'lumNester-1.0.0' (and everything under it)
running upload
Submitting dist\lumNester-1.0.0.zip to https://pypi.python.org/pypi Server response (200): OK

6、使用pip安装自己的模块(前提是你的模块已经上传到PyPI了),在终端窗口中,使用命令 pip install 模块名。

7、如果网站上的模块更新以后,可以用命令pip install -U 模块名 来升级模块。-U也可以用--upgrade代替。

四、已发布模块修改原则

1、已经发布的模块中函数如果需要增加参数时,应该使用带默认值的参数,避免用户更新模块后无法使用。

例如:原函数def print_lol(the_list) 需要增加一个参数level,可以这样修改

def print_lol(the_list,level=0)

这里level=0就是给参数一个默认值,那么已经使用这个函数的程序不需要修改就可以使用新函数。

2、函数修改后,在使用默认参数时,得到的结果应该和原函数保持一致,避免已经使用该函数的程序产生问题。

五、新内置函数及使用

print的新用法:print(varname,end='') 使用参数end='',可以输出不带回车换行的字符。

制表符可以用'\t'表示。

range(n),生成迭代器,数字从0到n-1。

>>>for num in range(4):
print(num)

0
1
2
3
>>>

六、代码格式错误处理

在IDLE中按F5后有提示Inconsistent indentation detected等,表示制表符和空格混合缩进的错误时,可用在IDLE中菜单栏选择Format-->点击Tabify Region。可用自动将缩进全部改为制表符。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python 笔记