Python 之路,week05 - 常用模块学习

本节大纲:

  1. 模块定义、导入、优化详解

1. 模块定义、导入、优化详解

1、定义
模块,用一砣代码实现了某个功能的代码集合,本质就是.py结尾的python文件。
包,用来从逻辑上组织模块的,本质就是一个目录(必须带有一个_init_.py文件)

类似于函数式编程和面向过程编程,函数式编程则完成一个功能,其他代码用来调用即可,提供了代码的重用性和代码间的耦合。而对于一个复杂的功能来,可能需要多个函数才能完成(函数又可以在不同的.py文件中),n个 .py 文件组成的代码集合就称为模块。

如:os 是系统相关的模块;file是文件操作相关的模块

模块分为三种:

自定义模块
内置标准模块(又称标准库)
开源模块
自定义模块 和开源模块的使用参考 http://www.cnblogs.com/wupeiqi/articles/4963027.html

2、导入方法

import module_name
import module1_name,module2_name

from module_name import *  
from module_name import m1,m2,m3 #不推荐,直接复制粘贴

from module_alex import logger as lpgger_alex

3、import的本质
导入模块的本质就是把Python文件解释一遍
导入包的本质就是执行该包下的_init_.py文件

4、导入优化

from module_name import m #直接一次导入,如果要多次调用m时可用

5、模块的分类
a:标准库
b:开源模块
c:自定义模块

标准库:
1.time与datetime
在Python中,通常有这几种方式来表示时间:
1)时间戳
2)格式化的时间字符串
3)元组(struct_time) 共九个元素

2.Range模块
random.random() 生成一个0到1的随机浮点数:0<=n<1
random.randint(a,b) 生成一个指定范围内的整数:a<=n<=b
random.randrange(a,b)
random.choice
random.randrange

生成随机验证码:

import random
check_code=''

for i in range(4):
    current=random.randint(0,4)
    if current == i:
        tmp=chr(random.randint(65,90))
    else:
        tmp=random.randint(0,9)
    check_code += str(tmp)

print(check_code)

3.OS模块

4.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模块 ——python高级的文件、文件夹、压缩包处理模块

shutil.copyfileobj(fsrc,fdst[,length])
将文件内容拷贝到另一个文件中,可以部分内容(需要open打开文件,编码utf-8)

shutil.copyfile(src,dst)
拷贝文件(只需输入文件名,不用open打开)

shutil.copymode(src,dst)
仅拷贝权限。内容、组、用户均不变

shutil.copystat(src,dst)
拷贝状态信息,包括:mode bits,atime,mtime, flags

shutil.copy(src,dst)
拷贝文件和权限

shutil.copy2(src,dst)
拷贝文件和状态信息

shutil.copytree(src,dst,symlinks=False,ignore=None)
递归的去拷贝文件(拷贝目录)

shutil.rmtree(path,[,ignore_errors[,onerror]])
递归的去删除文件(删除目录)

shutil.move(src,dst)
递归的去移动文件

shutil.make_archive(base_name,format,...)
创建压缩包并返回文件路径,例如:zip、tar
-base_name:包名,路径
-format:压缩包种类,"zip","tar"
-root_dir:要压缩的那文件夹路径
-owner:用户
-group:组
-logger:用于记录日志,通常是logging.Logger对象

shutil对压缩包的处理是调用zipfile和tarfile两个模块来进行的

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

import shelve

d = shelve.open('shelve.test')  #打开文件

info = {'age':22,'job':'it'}
name = ['alex','rain','e']

d['info'] = info
d['name'] = name
print (d.get('name'))
print (d.get('info'))
d.close

7.xml模块
xml的格式如下,就是通过<>节点来区别数据结构的:

<?xml version="1.0"?>
<data>
    <country name="Liechtenstein">
        <rank updated="yes">2</rank>
        <year>2008</year>
        <gdppc>141100</gdppc>
        <neighbor name="Austria" direction="E"/>
        <neighbor name="Switzerland" direction="W"/>
    </country>
    <country name="Singapore">
        <rank updated="yes">5</rank>
        <year>2011</year>
        <gdppc>59900</gdppc>
        <neighbor name="Malaysia" direction="N"/>
    </country>
    <country name="Panama">
        <rank updated="yes">69</rank>
        <year>2011</year>
        <gdppc>13600</gdppc>
        <neighbor name="Costa Rica" direction="W"/>
        <neighbor name="Colombia" direction="E"/>
    </country>
</data>

xml协议在各个语言里的都 是支持的,在python中可以用以下模块操作xml

import xml.etree.ElementTree as ET

tree = ET.parse("xmltest.xml")
root = tree.getroot()
print(root.tag)

# 遍历xml文档
for child in root:
    print(child.tag, child.attrib)
    for i in child:
        print(i.tag, i.text)

# 只遍历year 节点
for node in root.iter('year'):
    print(node.tag, node.text)

修改和删除xml文档内容

import xml.etree.ElementTree as ET
 
tree = ET.parse("xmltest.xml")
root = tree.getroot()
 
#修改
for node in root.iter('year'):
    new_year = int(node.text) + 1
    node.text = str(new_year)
    node.set("updated","yes")
 
tree.write("xmltest.xml")
 
 
#删除node
for country in root.findall('country'):
   rank = int(country.find('rank').text)
   if rank > 50:
     root.remove(country)
 
tree.write('output.xml')

自己创建xml文档

import xml.etree.ElementTree as ET
 
 
new_xml = ET.Element("namelist")
name = ET.SubElement(new_xml,"name",attrib={"enrolled":"yes"})
age = ET.SubElement(name,"age",attrib={"checked":"no"})
sex = ET.SubElement(name,"sex")
sex.text = '33'
name2 = ET.SubElement(new_xml,"name",attrib={"enrolled":"no"})
age = ET.SubElement(name2,"age")
age.text = '19'
 
et = ET.ElementTree(new_xml) #生成文档对象
et.write("test.xml", encoding="utf-8",xml_declaration=True)
 
ET.dump(new_xml) #打印生成的格式

8.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)

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

import hashlib

# ######## md5 ########
m = hashlib.md5()
m.update(b"Hello")
m.update(b"It's me")
#加密内容是全部消息,即“HelloIt's me”
print(m.digest()) #2进制格式hash
print(len(m.hexdigest())) #16进制格式hash

# ######## sha1 ########
 
hash = hashlib.sha1()
hash.update('admin')
print(hash.hexdigest())
 
# ######## sha256 ########
 
hash = hashlib.sha256()
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'12345', '宝塔镇河妖'.encode(encoding="utf-8"))#key,value
print h.hexdigest()

10.re模块
常用正则表达式符号

'.'     默认匹配除\n之外的任意一个字符,若指定flag DOTALL,则匹配任意字符,包括换行
'^'     匹配字符开头,若指定flags MULTILINE,这种也可以匹配上(r"^a","\nabc\neee",flags=re.MULTILINE)
'$'     匹配字符结尾,或e.search("foo$","bfoo\nsdfsf",flags=re.MULTILINE).group()也可以
'*'     匹配*号前的字符0次或多次,re.findall("ab*","cabb3abcbbac")  结果为['abb', 'ab', 'a']
'+'     匹配前一个字符1次或多次,re.findall("ab+","ab+cd+abb+bba") 结果['ab', 'abb']
'?'     匹配前一个字符1次或0次
'{m}'   匹配前一个字符m次
'{n,m}' 匹配前一个字符n到m次,re.findall("ab{1,3}","abb abc abbcbbb") 结果'abb', 'ab', 'abb']
'|'     匹配|左或|右的字符,re.search("abc|ABC","ABCBabcCD").group() 结果'ABC'
'(...)' 分组匹配,re.search("(abc){2}a(123|456)c", "abcabca456c").group() 结果 abcabca456c
 
 
'\A'    只从字符开头匹配,re.search("\Aabc","alexabc") 是匹配不到的
'\Z'    匹配字符结尾,同$
'\d'    匹配数字0-9
'\D'    匹配非数字
'\w'    匹配[A-Za-z0-9]
'\W'    匹配非[A-Za-z0-9]
's'     匹配空白字符、\t、\n、\r , re.search("\s+","ab\tc1\n3").group() 结果 '\t'
 
'(?P<name>...)' 分组匹配 re.search("(?P<province>[0-9]{4})(?P<city>[0-9]{2})(?P<birthday>[0-9]{4})","371481199306143242").groupdict("city") 

结果:{'province': '3714', 'city': '81', 'birthday': '1993'}

最常用的匹配语法

re.match 从头开始匹配
re.search 匹配包含
re.findall 把所有匹配到的字符放到以列表中的元素返回
re.splitall 以匹配到的字符当做列表分隔符
re.sub      匹配字符并替换

本节作业

开发一个简单的python计算器

1.实现加减乘除及拓号优先级解析
2.用户输入 1 - 2 * ( (60-30 +(-40/5) * (9-25/3 + 7 /399/42998 +10 * 568/14 )) - (-43)/ (16-32) )等类似公式后,必须自己解析里面的(),+,-,,/符号和公式(不能调用eval等类似功能偷懒实现),运算后得出结果,结果必须与真实的计算器所得出的结果一致

posted @ 2017-11-23 23:00  柠檬红茶cc  阅读(217)  评论(0编辑  收藏  举报