摘要:第二章學(xué)習(xí)流程圖函數(shù)轉(zhuǎn)換為模塊函數(shù)轉(zhuǎn)換為模塊后,就可以靈活的使用模塊,方便代碼分類,避免代碼堆積在一個文件上。使用命令打包代碼,生成發(fā)布包打包后會生成目錄和文件發(fā)布后會多了目錄和文件,這個是發(fā)布的生成的包和相關(guān)配置文件。
head first python(第二章)--學(xué)習(xí)流程圖
函數(shù)轉(zhuǎn)換為模塊后,就可以靈活的使用模塊,方便代碼分類,避免代碼堆積在一個文件上。當你寫的python代碼達到一定數(shù)量并且功能開始豐富的時候,就必須要這么做了。而模塊會存在python本地副本目錄里面,所以需要打包,需要安裝模塊到副本目錄,以便python解釋器能夠找到模塊,然后使用。
python搜索模塊的目錄位置:(大致需要知道就行了)
python
Python 2.6.6 (r266:84292, Jan 22 2014, 09:37:14) [GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import sys >>> sys.path ["", "/usr/lib/python26.zip", "/usr/lib/python2.6", "/usr/lib/python2.6/plat-linux2", "/usr/lib/python2.6/lib-tk", "/usr/lib/python2.6/lib-old", "/usr/lib/python2.6/lib-dynload", "/usr/lib/python2.6/site-packages"] >>>1.1 創(chuàng)建目錄和文件
創(chuàng)建目錄nester
mkdir nester
進入nester目錄并創(chuàng)建nester.py 和setup.py
cd netster
vi nester.py
"""This is the "nester.py" module and it provides one function called print_lol() which prints lists that may or may not include nested lists.""" def print_lol(the_list): """This function takes one positional argument called "the_list", which is any Python list (of - possibly - nested lists). Each data item in the provided list is (recursively) printed to the screen on it’s own line.""" for each_item in the_list: if isinstance(each_item, list): print_lol(each_item) else: print(each_item)
nester.py代碼模塊文件,格式要嚴謹,不必加上其他的東西。
vi setup.py
from distutils.core import setup setup( name = "nester", version = "1.0.0", ## TODO: be sure to change these next few lines to match your details! py_modules = ["nester"], author = "hfpython", author_email = "[email protected]", url = "http://www.headfirstlabs.com", description = "A simple printer of nested lists", )
setup.py是模塊發(fā)布的元文件metafile,里面包含模塊的一些發(fā)布信息。按照格式和要求編寫即可。
1.2 使用python setup.py sdist命令打包代碼,生成發(fā)布包python setup.py sdist running sdist 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 nester-1.0.0 making hard links in nester-1.0.0... hard linking nester.py -> nester-1.0.0 hard linking setup.py -> nester-1.0.0 creating dist tar -cf dist/nester-1.0.0.tar nester-1.0.0 gzip -f9 dist/nester-1.0.0.tar tar -cf dist/nester-1.0.0.tar nester-1.0.0 gzip -f9 dist/nester-1.0.0.tar removing "nester-1.0.0" (and everything under it)
打包后會生成目錄dist和文件MANIFEST
├── dist │?? └── nester-1.0.0.tar.gz ├── MANIFEST ├── nester.py └── setup.py
cat MANIFEST
nester.py setup.py
1.3 安裝打包代碼,即使安裝nester模塊發(fā)布后會多了dist目錄和MANIFEST文件,這2個是發(fā)布的生成的包和相關(guān)配置文件。
python setup.py install
running install running build running build_py copying nester.py -> build/lib running install_lib copying build/lib/nester.py -> /usr/lib/python2.6/site-packages byte-compiling /usr/lib/python2.6/site-packages/nester.py to nester.pyc running install_egg_info Removing /usr/lib/python2.6/site-packages/nester-1.0.0-py2.6.egg-info Writing /usr/lib/python2.6/site-packages/nester-1.0.0-py2.6.egg-info
安裝后
. ├── build │?? └── lib │?? └── nester.py ├── dist │?? └── nester-1.0.0.tar.gz ├── MANIFEST ├── nester.py └── setup.py
1.4 模塊使用的時候就是import nester安裝后悔出現(xiàn)build目錄,這個就是用發(fā)布包解壓出來的庫和模塊代碼,會統(tǒng)一放到python的模塊搜索目錄里面去。
不過出現(xiàn)了錯誤
>>> import nester >>> print_lol(a) Traceback (most recent call last): File "", line 1, in NameError: name "print_lol" is not defined >>>
是因為命名空間的關(guān)系,主python程序中(以及idle shell中)的代碼與一個名為main的命名空間關(guān)聯(lián)。所以的話不能直接使用print_lol,而要加上命名空間nester.print_lol()
知識點:
1.命名空間是為了讓python解釋器就會知道去哪里找這個函數(shù),命名空間格式:首先是模塊名字,然后是一個點號,再后面是函數(shù)名。 2.如果使用from nester import print_lol,就會把指定的函數(shù)print_lol增加到當前命名空間中,這樣就不用加上命名空間來調(diào)用函數(shù)了。
這樣可以增加功能而不用增加多余函數(shù)代碼(print_lol2)。
寫新代碼之前先考慮BIF(內(nèi)置函數(shù))
rang() 返回一個迭代器,根據(jù)需要生成一個指定范圍的數(shù)字。
根據(jù)需求寫了這樣的代碼,但運行報錯
增加了一個level參數(shù),默認等于0,遇到嵌套列表就是用rang(level)來進行縮進
def print_lol(a_list, level=0): """Prints each item in a list, recursively descending into nested lists (if necessary).""" for each_item in a_list: if isinstance(each_item, list): print_lol(each_item, level) else: for l in range(level): print(" ", end="") print(each_item)
出現(xiàn)錯誤了:TypeError:print_lol() take exactly 2 positional arguments (1 given)
原因是print_lol需要2個參數(shù),目前只有一個。
要改成
def print_lol(a_list, level=0): """Prints each item in a list, recursively descending into nested lists (if necessary).""" for each_item in a_list: if isinstance(each_item, list): print_lol(each_item, level+1) #因為不加1的話函數(shù)調(diào)用還是會出現(xiàn)level=0,就沒有意義了 else: for l in range(level): print(" ", end="") print(each_item)
然后更新代碼發(fā)布包
from distutils.core import setup setup( name = "nester", version = "1.1.0", #這里改成1.1.0了,更新了新代碼版本 ## TODO: be sure to change these next few lines to match your details! py_modules = ["nester"], author = "hfpython", author_email = "[email protected]", url = "http://www.headfirstlabs.com", description = "A simple printer of nested lists", )
因為增加了level參數(shù),改變了默認的行為(最原始的默認行為不想要縮進),導(dǎo)致打印出來的格式有異常,需要再增加一個可選參數(shù),來控制縮進功能的使用。
增加一個indent參數(shù)來控制縮進是否打開
def print_lol(a_list, indent=False, level=0): """Prints each item in a list, recursively descending into nested lists (if necessary).""" for each_item in a_list: if isinstance(each_item, list): print_lol(each_item, indent, level+1) else: if indent: for l in range(level): print(" ", end="") print(each_item)
需要注意的是所有代碼如果不是idel中運行的話,都需要加上#!/usr/bin/python在頂部。
原文鏈接:http://www.godblessyuan.com/2015/04/13/head_first_python_chapter_2_lea...
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/37517.html
摘要:第一章學(xué)習(xí)流程圖安裝這里是用的,除了之外,和都自帶了,只是版本沒有這么新。是區(qū)分大小寫的。可以根據(jù)多維數(shù)組來理解。知識點補充里列表是打了激素的數(shù)組,意味著列表比數(shù)組更厲害,更好用。的語句的縮進是必須規(guī)范的。 head first python(第一章)--學(xué)習(xí)流程圖 showImg(http://source1.godblessyuan.com/blog_headfirstpytho...
摘要:原來的順序會丟失。原數(shù)據(jù)的順序依然保留。方法串聯(lián)第一個方法應(yīng)用到數(shù)據(jù)中,然后再將處理好的數(shù)據(jù)應(yīng)用到第二個方法中。例子函數(shù)串聯(lián)每個函數(shù)會取得數(shù)據(jù),對他完成某個操作,然后把轉(zhuǎn)換后的數(shù)據(jù)繼續(xù)向下傳遞到下一個函數(shù)。 showImg(http://source1.godblessyuan.com/blog_head_first_python_chapter_5_20150427.jpg); 1...
showImg(http://source1.godblessyuan.com/blog_head_first_python_chapter_4_20150426.jpg); 其實持久存儲不僅僅包含文件,還包括數(shù)據(jù)庫等,本章先介紹一部分,先熟悉一下。 熟悉python數(shù)據(jù) #!/usr/bin/python # -*- coding: utf-8 -*- man = [] other =...
摘要:代碼改為根據(jù)數(shù)據(jù)結(jié)構(gòu),第一個數(shù)據(jù)是名字,第二個是生日,第二個之后是成績,所以分別將相關(guān)數(shù)據(jù)賦值到字典里面。是否知道何時使用列表而何時使用字典,這正式從好的程序員中區(qū)分出優(yōu)秀程序員的一個標準。特定函數(shù)應(yīng)用特定數(shù)據(jù)。更加正規(guī)的做法是建立類。 showImg(http://source1.godblessyuan.com/blog_head_first_python_chapter_6_20...
摘要:增加邏輯來處理首先通過觀察方法對于不同的數(shù)據(jù)返回的值是不同的。所以需要加一些標記,標識數(shù)據(jù)不符合期望的格式時會出現(xiàn)數(shù)據(jù)無法正常訪問時會出現(xiàn)。 1.介紹基礎(chǔ)文件,輸入,輸出 open() 打開文件,一次傳入一行數(shù)據(jù),可以結(jié)合for循環(huán)和readline()來使用 close() 用來關(guān)閉open打開的文件 the_file = open(sketch.txt) the_file....
閱讀 2809·2021-11-17 09:33
閱讀 4490·2021-09-22 15:57
閱讀 2883·2019-08-30 14:16
閱讀 3147·2019-08-29 14:07
閱讀 2428·2019-08-26 11:55
閱讀 3440·2019-08-23 17:07
閱讀 1738·2019-08-23 16:50
閱讀 2553·2019-08-23 16:08