python学习10 -模块

模块:

  在pyhont 中任何程序都可以作为模块存在。如下面的 代码,且将他存起  hello.py,并存放在 C:\python 目录中

#hello.py
print "hello. world"

  在 shell 中调用:

>>> import sys   # 告诉解释器,除了 在默认的路径中寻找模块,还要在指定的目录中寻找模块
>>> sys.path.append('c"/Python27')
>>> import hello
hello. world
>>> import hello  # 再次调用,并不打印"hello world" ,因为导入模块并不意味着在导入时执行某些操作(如打印)。他们主要用于定义,比如变量,函数等,因此,因为只需要定义这些东西一次,导入多次模块和导入一次的效果是一样的
>>>

  上面的例子中,我们改变了sys.path,他包含了一个目录列表,解释器在该列表中查找模块,0如果你不想这么做,那么有2种方法

  1.将模块放在正确的位置

    列出 python解释器从哪里查找模块 ,--site-packages 目录是最佳选择!!!

>>> import sys,pprint
>>> pprint.pprint(sys.path)
['',
 'C:\\Python27\\Lib\\idlelib',
 'C:\\Python27\\lib\\site-packages\\setuptools-21.0.0-py2.7.egg',
 'C:\\Python27\\lib\\site-packages\\pip-8.1.1-py2.7.egg',
 'C:\\Windows\\system32\\python27.zip',
 'C:\\Python27\\DLLs',
 'C:\\Python27\\lib',
 'C:\\Python27\\lib\\plat-win',
 'C:\\Python27\\lib\\lib-tk',
 'C:\\Python27',
 'C:\\Python27\\lib\\site-packages',
 'c"/Python27']

  ps:如果数据结构过大,不能在一行打印完,可以商用pprint 中的pprint 函数代替 print语句。

  2.告诉编译器去哪寻找

    --在PYTHONPATH 环境变量中包含模块所在目录。

  包

    为了更好的组织模块,将他们分组为包(package)

    包就是模块所在目录,为了让python将其作为包处理,它必须包含一个 __init__.py的文件(模块)

 

探究模块

  1.dir dir函数可以查看模块内所包含的内容

  2.__all__变量,在编写模块时,可以能会有一大堆其他程序不需要或不想要的变量,函数和类,__all__会将他们过滤了出去,如果没有设__all__,用import * 语句会将导入模块中所有不以下划线开头的全局名称

  3.help 与直接检查文档字符串相比,help会得到更多信息,比如函数签名等

  4.文档 __doc__

  5.查看源码  __file__

>>> [n for n in dir(copy) if not n.startswith('__')]
['Error', 'PyStringMap', '_EmptyClass', '_copy_dispatch', '_copy_immutable', '_copy_inst', '_copy_with_constructor', '_copy_with_copy_method', '_deepcopy_atomic', '_deepcopy_dict', '_deepcopy_dispatch', '_deepcopy_inst', '_deepcopy_list', '_deepcopy_method', '_deepcopy_tuple', '_keep_alive', '_reconstruct', '_test', 'copy', 'deepcopy', 'dispatch_table', 'error', 'name', 't', 'weakref']
>>> copy.__all__
'Error', 'copy', 'deepcopy']
>>> help(copy.copy)
Help on function copy in module copy:
copy(x)
Shallow copy operation on arbitrary Python objects.

See the module's __doc__ string for more info.
>>> print copy.copy.__doc__
Shallow copy operation on arbitrary Python objects.

See the module's __doc__ string for more info.

 

一些标准库

  1.sys 这个模块可让你能够访问与Pyhont解释器联系紧密的变量和函数。

    argv   --- 命令行参数,包括脚本名称

    exit([arg])  ---退出当前的程序,可选参数为给定的返回值或错误信息

    modules  ---映射模块名字到载入的字典

    path  ---查找模块所在的目录的目录名称列表

    platform  ---类似 win32 的平台标识符

    stdin  ---标准输入流

    stdou   ---标准输出流

    stderr  --标准错误流

  2.os 该模块提供了访问多给操作系统服务的功能。

    environ  ---对环境变量进行映射

    system(command)  ---在子shell中执行操作系统命令

    sep  ---路径中的分隔符

    pathsep   ---分割路径中的分隔符

    linesep   ---行分隔符('\n','\r','\r\n',)

  3.fileinput  该模块能够让你轻松的遍历文本文件的所有行

    input()  ---遍历所输入流中的行  ,他会返回一个能够用于for 循环遍历的对象,也可以给函数提供一个或多个文件名,还能将 inplace参数设为真,进行原地处理

    filename()   --- 返回当前文件名

    lineno()   ---返回当前(累计)的行数

    filelineno()   ---返回当前文件的的行数

    isfirstline()   ---检查当前行是否是文件的第一行

    isstdin()   ---检查最后一行是否来自sys.stdin

    nextfile()   ---关闭当前文件,移动到下一个文件

    close()   ---关闭序列

  集合,堆,双端队列

    集合 set   集合是由序列(或其他可迭代的对象)构建的,主要用于检查成员资格。且里面元素是随意的。

1 >>> set((1,2,3))
2 set([1, 2, 3])

    堆   是优先队列的一种,能够任务的顺序增加对象,并能在任何时间找到最小的原色,

    python 中没有独立的堆类型,只有一个包含一些堆操作的模块, heapq

函数 描述
heappush(heap,x) 将X 入堆
heappop(heap) 将堆中最小元素弹出
heapify(heap) 将heap属性强制应用到任意一个列表
heapreplace(heap,x) 将堆中最小元素弹出,同时将X 入堆
nlargest(n,iter) 返回iter 中第n大的元素
nsmallest(n,iter) 返回iter 中第n小的元素

 

 

 

 

 

 

ps:前4个函数直接和堆操作有关,你必须将列表作为 堆对象本身,heappush 函数用于增加堆得想,他不能用于任何之前讲述的列表中,只能用于通过堆函数建立的列表中,

 1 >>> from heapq import *
 2 >>> from random import shuffle
 3 >>> data  = range[10]
 4 >>> print data
 5 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
 6 >>> shuffle(data)
 7 >>> data
 8 [8, 6, 5, 3, 9, 0, 1, 4, 2, 7]
 9 >>> heap = []
10 >>> for  n in data:
11     heappush(heap,n)
12 
13     
14 >>> heap
15 [0, 2, 1, 4, 7, 6, 3, 8, 5, 9]
16 >>> heappush(heap,0.5)
17 >>> heap
18 [0, 0.5, 1, 4, 2, 6, 3, 8, 5, 9, 7]
19 >>> 

     heappop 函数弹出最小元素,一般来说都是在 索引为0 的元素,并且会确保神域元素中最小的那个占据这个位置

1 >>> heappop(heap)
2 0
3 >>> heappop(heap)
4 1
5 >>> heappop(heap)
6 2
7 >>> heap
8 [3, 4, 7, 6, 5, 9, 8]

   heapify 函数使用了任意列表作为参数,将其转化成合法的堆

1 >>> heap = [2,3,6,1,5]
2 >>> heapify(heap)
3 >>> heap
4 [1, 2, 6, 3, 5]

       heapreplace 函数弹出最小元素,并将新元素推入

1 >>> heap
2 [1, 2, 6, 3, 5]
3 >>> heapreplace(heap,8)
4 1
5 >>> heap
6 [2, 3, 6, 8, 5]

 

  双端队列  (double-ended queue 或称 deque)

 1 >>> q.append(5)
 2 >>> q
 3 deque([0, 1, 2, 3, 4, 5])
 4 >>> q.appendleft(6)
 5 >>> q
 6 deque([6, 0, 1, 2, 3, 4, 5])
 7 >>> q.pop()
 8 5
 9 >>> q.popleft()
10 6
11 >>> q.rotate(-1)
12 >>> q

 

  time 获得当前时间,操作时间和日期,从字符串读取时间以及格式化时间为字符串。

asctime([tuple]) 将时间元祖转化成字符串
localtime([secs]) 将秒数转化成日期元祖,以本地时间为准
mktime([tuple]) 将时间元祖转化成本地时间
sleep(secs) 休眠 secs 秒
strptime(string[,format]) 将字符串转化成时间元祖
time() 当前时间

 

 

 

 

 

 

 

 

 

 

>>> import time
>>> time.asctime()
'Tue Mar 28 08:08:25 2017'   #获取时间字符串

>>> time.localtime()
time.struct_time(tm_year=2017, tm_mon=3, tm_mday=28, tm_hour=8, tm_min=12, tm_sec=22, tm_wday=1, tm_yday=87, tm_isdst=0)
>>> a  = time.localtime()
>>> time.asctime(a)
'Tue Mar 28 08:12:54 2017'

>>> time.mktime(a)
1490659974.0
>>> b = time.asctime(a)
>>> time.strptime(b)
time.struct_time(tm_year=2017, tm_mon=3, tm_mday=28, tm_hour=8, tm_min=12, tm_sec=54, tm_wday=1, tm_yday=87, tm_isdst=-1)
>>> time.time()
1490660044.994
>>> 

 

  random  该模块报货返回随机数的函数,可以用于模拟或者用于任何产生随机输出的程序

random() 返回 0<n<=1之间的随机实数
getranbits(n) 以长整型形式返回n个随机为
uniform(a,b) 返回随机实数n,其中  a<=n < b
randrange([start],stop,[step]) 返回range(start,stop,setp) 中的随机数
choice(seq) 从序列seq 中返回随意隐患苏
shuffle(seq[,random]) 原地指定序列seq
sample(seq,n) 从序列seq 中选择N个随机且独立的元素

 

 

 

 

 

 

 

 

>>> import random as r
>>> r.sample([1,2,3,4,5,6],3)   # 从给定的徐磊中选择给定数目的元素,且确保元素互补相同
[3, 4, 2]
>>> r.choice([1,2,3]) #从给定的薛烈中返回任意元素
1
>>> r.choice([1,2,3])
2
>>> r.choice([1,2,3])
3

 

posted @ 2017-03-28 08:40  大愚者  阅读(217)  评论(0编辑  收藏  举报