Python基础知识之模块详解

转载自:http://www.cnblogs.com/alex3714/articles/5161349.html

1. 定义

模块:用来从逻辑上组织python代码(变量,函数,类,逻辑:实现一个功能),本质就是.py结尾的python文件(文件名:test.py 使用:import test
包:用来从逻辑上组织模块的,本质就是一个目录(必须带有一个__init__.py文件)

2. 导入方法:

import module_name
import module_name1, module_name2

导入多个方法:

from module_name import m1, m2, m3 

导入所有方法:

from module_name import *

但是不建议用,要避免冲突情况的发生

比如说在我们的module_name.py中声明了sayhi()方法,那么在我们导入的时候就会将module_name.py里面的所有代码编译到被导入的文件中去并且执行,这时候如果我们在新文件中也定义了一个sayhi()方法,那么就会执行定义的sayhi()方法而不是module_name.py里面的sayhi()方法。
那么,有什么方法可以解决这种问题呢?

import module_name import sayhi() as module_sayhi

这样我们就可以使用module_sayhi()方法来调用module_name.py里面的sayhi()方法了。

3. import本质(路径搜索和搜索路径)

导入模块的本质就是把python文件解释一遍.
导入包的本质就是执行该包下的__init__.py文件

  • import module_nameimport的本质就是将module_name.py里面的所有代码解释编译之后赋值给了一个叫做module_name的变量当中:module_name = all_code。这个时候如果我们想要调用module_name里面的属性name的话就需要写module_name.name,如果想要调用里面的sayhi()方法的话就需要module_name.sayhi()
  • from module_name import name,sayhiimport的本质就是将module_name.py里面的name属性和sayhi()方法代码解释编译之后赋值给了一个叫做namesayhi的变量中,如果调用sayhi()方法直接调用就可以了。

考虑一下,如果如下图所示:

main.py中,如果我们想要import module_name的话,就会报错,因为module.pymain.py不在同一目录下,那么如何去实现呢?

import sys,os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
print(BASE_DIR)
sys.path.append(BASE_DIR)
from module_test import module_name

这样,就将main.py的目录的目录,即test目录添加到了系统的环境变量中。

有人会想了,那么如果我直接用import module_test.module_name来导入这个包可以吗? 答案是不可以的,我们需要注意导入模块导入包直接的区别,导入包其实只是执行了包下面的__init__.py文件而已。

那么该如何解决这个问题呢,这个时候就需要我们呢在这个包module_test下的__init__.py文件中进行修改:

from . import module_name

这边的.表示的是__init__.py的当前目录

4. 导入优化

from module_name import fuction_name

建议用这种方式调用。

5. 模块的分类

(转载至:http://blog.51cto.com/egon09/1840425)

  • 标准库
  • 开源模块
  • 自定义模块

1. time和datetime

在Python中,表示时间的方式有:

  • 时间戳
  • 格式化的时间字符串
  • 元组(struct_time)

由于Python的time模块实现主要调用C库,所以各个平台可能有所不同.

  1. UTC(Coordinated Universal Time,世界协调时)亦即格林威治天文时间,世界标准时间。在中国为UTC+8。DST(Daylight Saving Time)即夏令时。
  2. 时间戳(timestamp)的方式:通常来说,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量。我们运行“type(time.time())”,返回的是float类型。返回时间戳方式的函数主要有time(),clock()等。
  3. 元组(struct_time)方式:struct_time元组共有9个元素,返回struct_time的函数主要有gmtime(),localtime(),strptime()。

接着介绍time模块中常用的几个函数:

1)time.localtime([secs]):将一个时间戳转换为当前时区的struct_time。secs参数未提供,则以当前时间为准。

>>> time.localtime()
time.struct_time(tm_year=2011, tm_mon=5, tm_mday=5, tm_hour=14, tm_min=14, tm_sec=50, tm_wday=3, tm_yday=125, tm_isdst=0)
>>> time.localtime(1304575584.1361799)
time.struct_time(tm_year=2011, tm_mon=5, tm_mday=5, tm_hour=14, tm_min=6, tm_sec=24, tm_wday=3, tm_yday=125, tm_isdst=0)

2)time.gmtime([secs]):和localtime()方法类似,gmtime()方法是将一个时间戳转换为UTC时区(0时区)的struct_time。

>>>time.gmtime()
time.struct_time(tm_year=2011, tm_mon=5, tm_mday=5, tm_hour=6, tm_min=19, tm_sec=48, tm_wday=3, tm_yday=125, tm_isdst=0)

3)time.time():返回当前时间的时间戳。

>>> time.time() 
1304575584.1361799

4)time.mktime(t):将一个struct_time转化为时间戳。

>>> time.mktime(time.localtime())
1304576839.0

5)time.sleep(secs):线程推迟指定的时间运行。单位为秒。

6)time.clock():这个需要注意,在不同的系统上含义不同。在UNIX系统上,它返回的是“进程时间”,它是用秒表示的浮点数(时间戳)。而在WINDOWS中,第一次调用,返回的是进程运行的实际时间。而第二次之后的调用是自第一次调用以后到现在的运行时间。(实际上是以WIN32上QueryPerformanceCounter()为基础,它比毫秒表示更为精确)

import time  
if __name__ == '__main__':  
    time.sleep(1)  
    print "clock1:%s" % time.clock()  
    time.sleep(1)  
    print "clock2:%s" % time.clock()  
    time.sleep(1)  
    print "clock3:%s" % time.clock()

运行结果:

clock1:3.35238137808e-006 
clock2:1.00004944763 
clock3:2.00012040636

其中第一个clock()输出的是程序运行时间
第二、三个clock()输出的都是与第一个clock的时间间隔

7)time.asctime([t]):把一个表示时间的元组或者struct_time表示为这种形式:'Sun Jun 20 23:21:05 1993'。如果没有参数,将会将time.localtime()作为参数传入。

>>> time.asctime()
'Thu May 5 14:55:43 2011'

8)time.ctime([secs]):把一个时间戳(按秒计算的浮点数)转化为time.asctime()的形式。如果参数未给或者为None的时候,将会默认time.time()为参数。它的作用相当于time.asctime(time.localtime(secs))。

>>> time.ctime()
'Thu May 5 14:58:09 2011'
>>> time.ctime(time.time())
'Thu May 5 14:58:39 2011'
>>> time.ctime(1304579615)
'Thu May 5 15:13:35 2011'

9)time.strftime(format[, t]):把一个代表时间的元组或者struct_time(如由time.localtime()和time.gmtime()返回)转化为格式化的时间字符串。如果t未指定,将传入time.localtime()。如果元组中任何一个元素越界,ValueError的错误将会被抛出。

格式 含义 备注
%a 本地(locale)简化星期名称
%A 本地完整星期名称
%b 本地简化月份名称
%B 本地完整月份名称
%c 本地相应的日期和时间表示
%d 一个月中的第几天(01 - 31)
%H 一天中的第几个小时(24小时制,00 - 23)
%I 第几个小时(12小时制,01 - 12)
%j 一年中的第几天(001 - 366)
%m 月份(01 - 12)
%M 分钟数(00 - 59)
%p 本地am或者pm的相应符
%S 秒(01 - 61)
%U 一年中的星期数。(00 - 53星期天是一个星期的开始。)第一个星期天之前的所有天数都放在第0周。
%w 一个星期中的第几天(0 - 6,0是星期天)
%W 和%U基本相同,不同的是%W以星期一为一个星期的开始。
%x 本地相应日期
%X 本地相应时间
%y 去掉世纪的年份(00 - 99)
%Y 完整的年份
%Z 时区的名字(如果不存在为空字符)
%% ‘%’字符
备注:
  1. “%p”只有与“%I”配合使用才有效果。
  2. 文档中强调确实是0 - 61,而不是59,闰年秒占两秒(汗一个)。
  3. 当使用strptime()函数时,只有当在这年中的周数和天数被确定的时候%U和%W才会被计算。
    举个例子:
>>> time.strftime("%Y-%m-%d %X", time.localtime())
'2011-05-05 16:37:06'

10)time.strptime(string[, format]):把一个格式化时间字符串转化为struct_time。实际上它和strftime()是逆操作。

>>> time.strptime('2011-05-05 16:37:06', '%Y-%m-%d %X')
time.struct_time(tm_year=2011, tm_mon=5, tm_mday=5, tm_hour=16, tm_min=37, tm_sec=6, tm_wday=3, tm_yday=125, tm_isdst=-1)

在这个函数中,format默认为:"%a %b %d %H:%M:%S %Y"

最后,各时间表达方式之间的转换:

dateime模块的使用:

import datetime
 
print(datetime.datetime.now()) #返回 2016-08-19 12:47:03.941925
print(datetime.date.fromtimestamp(time.time()) )  # 时间戳直接转成日期格式 2016-08-19
print(datetime.datetime.now() )
print(datetime.datetime.now() + datetime.timedelta(3)) #当前时间+3天
print(datetime.datetime.now() + datetime.timedelta(-3)) #当前时间-3天
print(datetime.datetime.now() + datetime.timedelta(hours=3)) #当前时间+3小时
 print(datetime.datetime.now() + datetime.timedelta(minutes=30)) #当前时间+30分
 
 
c_time  = datetime.datetime.now()
print(c_time.replace(minute=3,hour=2)) #时间替换,但是c_time本身并没有发生变化

2. random模块

random中的常用方法:

import random
# 1. random.random()
#   用于生成一个0到1的随机浮点数:0<=n<=1.0
print(random.random()) # 0.9250322724099981

# 2. random.randint(a, b)
#   用于生成一个指定范围内的整数
#   其中参数a是下限,参数b是上限,生成的随机数n:a<=n<=b
print(random.randint(1, 8))  # 5

# 3. random.randrange([start], stop[, step])
#   从指定范围内,按指定基数递增的集合中 获取一个随机数。如:random.randrange(10, 100, 2),
#   结果相当于从[10, 12, 14, 16, ... 96, 98]序列中获取一个随机数。
#   注意,不包括100
#   random.randrange(10, 100, 2)在结果上与 random.choice(range(10, 100, 2) 等效。
print(random.randrange(5, 100, 3))  # 74

# 4. random.choice(sequence)
#   从序列中获取一个随机元素。 参数sequence表示一个有序类型。
#   这里要说明一下:sequence在python不是一种特定的类型,而是泛指一系列的类型。
#   list, tuple, 字符串都属于sequence。
print(random.choice('python'))  # o
print(random.choice(['Bear', 'is', 'a', 'good', 'boy'])) # is
print(random.choice(("Tuple", "List", "Dict")))  # Dict

# 5. random.sample(sequence, k),从指定序列中随机获取指定长度的片断。
#   sample函数不会修改原有序列。
print(random.sample([1, 2, 3, 4, 5],3)) # [1, 5, 4] 不按顺序的随机的

实际使用:

import random
import string

# 随机整数:
print(random.randint(0, 99))  # 70

# 随机选取0到100间的偶数:
print(random.randrange(0, 101, 2))  # 4

# 随机浮点数:
print(random.random())  # 0.2746445568079129
# uniform() 方法将随机生成下一个实数,它在 [x, y) 范围内。
print(random.uniform(1, 10))  # 9.887001463194844

# 随机字符:
print(random.choice('abcdefg&#%^*f'))  # f

# 多个字符中选取特定数量的字符:
print(random.sample('abcdefghij', 3))  # ['f', 'h', 'd']

# 随机选取字符串:
print(random.choice(['apple', 'pear', 'peach', 'orange', 'lemon']))  # apple
# 洗牌#
items = [1, 2, 3, 4, 5, 6, 7]
print(items)  # [1, 2, 3, 4, 5, 6, 7]
random.shuffle(items)  #  直接改变了items的顺序
print(items)  # [1, 4, 7, 2, 5, 3, 6]

生成随机验证码

import random

checkcode = ""
for i in range(0, 4):
    current = random.randrange(0, 4)
    if current == i:
        temp = chr(random.randint(65, 90))
    else:
        temp = random.randint(0, 9)
    checkcode += str(temp)
print(checkcode)


3. os模块

提供对操作系统教学调用的接口

import os
os.getcwd() 获取当前工作目录,即当前python脚本工作的目录路径
os.chdir("dirname")  改变当前脚本工作目录;相当于shell下cd
os.curdir  返回当前目录: ('.')
os.pardir  获取当前目录的父目录字符串名:('..')
os.makedirs('dirname1/dirname2')    可生成多层递归目录
os.removedirs('dirname1')    若目录为空,则删除,并递归到上一级目录,如若也为空,则删除,依此类推
os.mkdir('dirname')    生成单级目录;相当于shell中mkdir dirname
os.rmdir('dirname')    删除单级空目录,若目录不为空则无法删除,报错;相当于shell中rmdir dirname
os.listdir('dirname')    列出指定目录下的所有文件和子目录,包括隐藏文件,并以列表方式打印
os.remove()  删除一个文件
os.rename("oldname","newname")  重命名文件/目录
os.stat('path/filename')  获取文件/目录信息
os.sep    输出操作系统特定的路径分隔符,win下为"\\",Linux下为"/"
os.linesep    输出当前平台使用的行终止符,win下为"\t\n",Linux下为"\n"
os.pathsep    输出用于分割文件路径的字符串
os.name    输出字符串指示当前使用平台。win->'nt'; Linux->'posix'
os.system("bash command")  运行shell命令,直接显示
os.environ  获取系统环境变量
os.path.abspath(path)  返回path规范化的绝对路径
os.path.split(path)  将path分割成目录和文件名二元组返回
os.path.dirname(path)  返回path的目录。其实就是os.path.split(path)的第一个元素
os.path.basename(path)  返回path最后的文件名。如何path以/或\结尾,那么就会返回空值。即os.path.split(path)的第二个元素
os.path.exists(path)  如果path存在,返回True;如果path不存在,返回False
os.path.isabs(path)  如果path是绝对路径,返回True
os.path.isfile(path)  如果path是一个存在的文件,返回True。否则返回False
os.path.isdir(path)  如果path是一个存在的目录,则返回True。否则返回False
os.path.join(path1[, path2[, ...]])  将多个路径组合后返回,第一个绝对路径之前的参数将被忽略
os.path.getatime(path)  返回path所指向的文件或者目录的最后存取时间
os.path.getmtime(path)  返回path所指向的文件或者目录的最后修改时间  

4. sys模块

import sys
sys.argv           命令行参数List,第一个元素是程序本身路径
sys.exit(n)        退出程序,正常退出时exit(0)
sys.version        获取Python解释程序的版本信息
sys.maxint         最大的Int值
sys.path           返回模块的搜索路径,初始化时使用PYTHONPATH环境变量的值
sys.platform       返回操作系统平台名称
sys.stdout.write('please:')
val = sys.stdin.readline()[:-1]

5. shutil模块

参考: http://www.cnblogs.com/wupeiqi/articles/4963027.html

6. json&pickle模块

参考:http://www.cnblogs.com/bearkchan/p/8046572.html

7. shelve模块

shelve模块是一个简单的k,v将内存数据通过文件持久化的模块,可以持久化任何pickle可支持的python数据格式。

import shelve
import datetime
# 1.保存shelve
d = shelve.open("shelve_test") # 打开一个文件
d["date"] = datetime.datetime.now()
d["name"] = ["alex", "bear"]
d.close()
# 会多出三个文件分别是:
# shelve_test.bak,
# shelve_test.dat,
# shelve_test.dir
# -----------------------------------------------------------

# 2.读取shelve
d = shelve.open("shelve_test") # 打开一个文件
print(d.get("date")) #2017-12-22 20:05:47.023578

8. PyYAML模块

Python也可以很容易的处理ymal文档格式,只不过需要安装一个模块,参考文档:http://pyyaml.org/wiki/PyYAMLDocumentation

9. xml模块

参考:http://www.cnblogs.com/bearkchan/p/8087976.html

10. configparser模块

用于生成和修改常见配置文档,当前模块的名称在 python 3.x 版本中变更为 configparser。

来看一个好多软件的常见文档格式如下:

[DEFAULT]
ServerAliveInterval = 45
Compression = yes
CompressionLevel = 9
ForwardX11 = yes
 
[bitbucket.org]
User = hg
 
[topsecret.server.com]
Port = 50022
ForwardX11 = no

生成python文件:

import configparser
 
config = configparser.ConfigParser()
config["DEFAULT"] = {'ServerAliveInterval': '45',
                      'Compression': 'yes',
                     'CompressionLevel': '9'}
 
config['bitbucket.org'] = {}
config['bitbucket.org']['User'] = 'hg'
config['topsecret.server.com'] = {}
topsecret = config['topsecret.server.com']
topsecret['Host Port'] = '50022'     # mutates the parser
topsecret['ForwardX11'] = 'no'  # same here
config['DEFAULT']['ForwardX11'] = 'yes'
with open('example.ini', 'w') as configfile:
   config.write(configfile)

读文件:

>>> import configparser
>>> config = configparser.ConfigParser()
>>> config.sections()
[]
>>> config.read('example.ini')
['example.ini']
>>> config.sections()
['bitbucket.org', 'topsecret.server.com']
>>> 'bitbucket.org' in config
True
>>> 'bytebong.com' in config
False
>>> config['bitbucket.org']['User']
'hg'
>>> config['DEFAULT']['Compression']
'yes'
>>> topsecret = config['topsecret.server.com']
>>> topsecret['ForwardX11']
'no'
>>> topsecret['Port']
'50022'
>>> for key in config['bitbucket.org']: print(key)
...
user
compressionlevel
serveraliveinterval
compression
forwardx11
>>> config['bitbucket.org']['ForwardX11']
'yes'

增删改查语法:

[section1]
k1 = v1
k2:v2
  
[section2]
k1 = v1
 
import ConfigParser
  
config = ConfigParser.ConfigParser()
config.read('i.cfg')
  
# ########## 读 ##########
#secs = config.sections()
#print secs
#options = config.options('group2')
#print options
  
#item_list = config.items('group2')
#print item_list
  
#val = config.get('group1','key')
#val = config.getint('group1','key')
  
# ########## 改写 ##########
#sec = config.remove_section('group1')
#config.write(open('i.cfg', "w"))
  
#sec = config.has_section('wupeiqi')
#sec = config.add_section('wupeiqi')
#config.write(open('i.cfg', "w"))
  
  
#config.set('group2','k1',11111)
#config.write(open('i.cfg', "w"))
  
#config.remove_option('group2','age')
#config.write(open('i.cfg', "w"))

11. hashlib模块

用于加密相关的操作,3.x里代替了md5模块和sha模块,主要提供 SHA1, SHA224, SHA256, SHA384, SHA512 ,MD5 算法

import hashlib
 
m = hashlib.md5()
m.update(b"Hello")
m.update(b"It's me")
print(m.digest())
m.update(b"It's been a long time since last time we ...")
 
print(m.digest()) #2进制格式hash
print(len(m.hexdigest())) #16进制格式hash
'''
def digest(self, *args, **kwargs): # real signature unknown
    """ Return the digest value as a string of binary data. """
    pass
 
def hexdigest(self, *args, **kwargs): # real signature unknown
    """ Return the digest value as a string of hexadecimal digits. """
    pass
 
'''
import hashlib
 
# ######## md5 ########
 
hash = hashlib.md5()
hash.update('admin')
print(hash.hexdigest())
 
# ######## sha1 ########
 
hash = hashlib.sha1()
hash.update('admin')
print(hash.hexdigest())
 
# ######## sha256 ########
 
hash = hashlib.sha256()
hash.update('admin')
print(hash.hexdigest())
 
 
# ######## sha384 ########
 
hash = hashlib.sha384()
hash.update('admin')
print(hash.hexdigest())
 
# ######## sha512 ########
 
hash = hashlib.sha512()
hash.update('admin')
print(hash.hexdigest())

还不够吊?python 还有一个 hmac 模块,它内部对我们创建 key 和 内容 再进行处理然后再加密

散列消息鉴别码,简称HMAC,是一种基于消息鉴别码MAC(Message Authentication Code)的鉴别机制。使用HMAC时,消息通讯的双方,通过验证消息中加入的鉴别密钥K来鉴别消息的真伪;

一般用于网络通信中消息加密,前提是双方先要约定好key,就像接头暗号一样,然后消息发送把用key把消息加密,接收方用key + 消息明文再加密,拿加密后的值 跟 发送者的相对比是否相等,这样就能验证消息的真实性,及发送者的合法性了。

import hmac
h = hmac.new(b'you are a baby', '宝塔镇河妖'.encode(encoding="utf-8"))
#  前面必须是bytes类型且ASCII编码的
print (h.hexdigest())

更多关于md5,sha1,sha256等介绍的文章看这里https://www.tbs-certificates.co.uk/FAQ/en/sha256.html

12. re模块

参考:http://www.cnblogs.com/bearkchan/p/8093446.html

posted @ 2017-12-23 15:31  bearkchan  阅读(979)  评论(0编辑  收藏  举报