s12-20160130-day05

*:first-child {
margin-top: 0 !important;
}

body>*:last-child {
margin-bottom: 0 !important;
}

/* BLOCKS
=============================================================================*/

p, blockquote, ul, ol, dl, table, pre {
margin: 15px 0;
}

/* HEADERS
=============================================================================*/

h1, h2, h3, h4, h5, h6 {
margin: 20px 0 10px;
padding: 0;
font-weight: bold;
-webkit-font-smoothing: antialiased;
}

h1 tt, h1 code, h2 tt, h2 code, h3 tt, h3 code, h4 tt, h4 code, h5 tt, h5 code, h6 tt, h6 code {
font-size: inherit;
}

h1 {
font-size: 28px;
color: #000;
}

h2 {
font-size: 24px;
border-bottom: 1px solid #ccc;
color: #000;
}

h3 {
font-size: 18px;
}

h4 {
font-size: 16px;
}

h5 {
font-size: 14px;
}

h6 {
color: #777;
font-size: 14px;
}

body>h2:first-child, body>h1:first-child, body>h1:first-child+h2, body>h3:first-child, body>h4:first-child, body>h5:first-child, body>h6:first-child {
margin-top: 0;
padding-top: 0;
}

a:first-child h1, a:first-child h2, a:first-child h3, a:first-child h4, a:first-child h5, a:first-child h6 {
margin-top: 0;
padding-top: 0;
}

h1+p, h2+p, h3+p, h4+p, h5+p, h6+p {
margin-top: 10px;
}

/* LINKS
=============================================================================*/

a {
color: #4183C4;
text-decoration: none;
}

a:hover {
text-decoration: underline;
}

/* LISTS
=============================================================================*/

ul, ol {
padding-left: 30px;
}

ul li > :first-child,
ol li > :first-child,
ul li ul:first-of-type,
ol li ol:first-of-type,
ul li ol:first-of-type,
ol li ul:first-of-type {
margin-top: 0px;
}

ul ul, ul ol, ol ol, ol ul {
margin-bottom: 0;
}

dl {
padding: 0;
}

dl dt {
font-size: 14px;
font-weight: bold;
font-style: italic;
padding: 0;
margin: 15px 0 5px;
}

dl dt:first-child {
padding: 0;
}

dl dt>:first-child {
margin-top: 0px;
}

dl dt>:last-child {
margin-bottom: 0px;
}

dl dd {
margin: 0 0 15px;
padding: 0 15px;
}

dl dd>:first-child {
margin-top: 0px;
}

dl dd>:last-child {
margin-bottom: 0px;
}

/* CODE
=============================================================================*/

pre, code, tt {
font-size: 12px;
font-family: Consolas, "Liberation Mono", Courier, monospace;
}

code, tt {
margin: 0 0px;
padding: 0px 0px;
white-space: nowrap;
border: 1px solid #eaeaea;
background-color: #f8f8f8;
border-radius: 3px;
}

pre>code {
margin: 0;
padding: 0;
white-space: pre;
border: none;
background: transparent;
}

pre {
background-color: #f8f8f8;
border: 1px solid #ccc;
font-size: 13px;
line-height: 19px;
overflow: auto;
padding: 6px 10px;
border-radius: 3px;
}

pre code, pre tt {
background-color: transparent;
border: none;
}

kbd {
-moz-border-bottom-colors: none;
-moz-border-left-colors: none;
-moz-border-right-colors: none;
-moz-border-top-colors: none;
background-color: #DDDDDD;
background-image: linear-gradient(#F1F1F1, #DDDDDD);
background-repeat: repeat-x;
border-color: #DDDDDD #CCCCCC #CCCCCC #DDDDDD;
border-image: none;
border-radius: 2px 2px 2px 2px;
border-style: solid;
border-width: 1px;
font-family: "Helvetica Neue",Helvetica,Arial,sans-serif;
line-height: 10px;
padding: 1px 4px;
}

/* QUOTES
=============================================================================*/

blockquote {
border-left: 4px solid #DDD;
padding: 0 15px;
color: #777;
}

blockquote>:first-child {
margin-top: 0px;
}

blockquote>:last-child {
margin-bottom: 0px;
}

/* HORIZONTAL RULES
=============================================================================*/

hr {
clear: both;
margin: 15px 0;
height: 0px;
overflow: hidden;
border: none;
background: transparent;
border-bottom: 4px solid #ddd;
padding: 0;
}

/* TABLES
=============================================================================*/

table th {
font-weight: bold;
}

table th, table td {
border: 1px solid #ccc;
padding: 6px 13px;
}

table tr {
border-top: 1px solid #ccc;
background-color: #fff;
}

table tr:nth-child(2n) {
background-color: #f8f8f8;
}

/* IMAGES
=============================================================================*/

img {
max-width: 100%
}
-->

pytho自动化开发 day05

Date:2016.01.30

    @南非波波

课程大纲:

day04

http://www.cnblogs.com/alex3714/articles/5143440.html

day05

http://www.cnblogs.com/alex3714/articles/5161349.html

一、正则深入

re

匹配数字开头

import re
#匹配以数字开头
p = re.compile("^[0-9]") #使用compile进行编译正则表达式,在大量匹配处理的时候非常有效
m = p.match("14534Abc")
print(m.group()) #输出结果:1
'''
m = re.match("^[0-9]","14534Abc")
print(m.group()) #输出结果:1
'''

匹配数字、字母等开头

import re
#匹配以数字和字母开头
p = re.compile("^[0-9A-Za-z]")
m = p.match("AK14534Abc")
print(m.group()) #输出结果:A

匹配IP地址:

#匹配IP地址
string = '180.115.183.45 - - [03/Feb/2016:10:35:50 +0800] "CONNECT imppl.tradedoubler.com:443 HTTP/1.0" 400 172 "-" "-"'
p = re.compile("([0-9]{1,3}\.){3}\d{1,3}")
ip = p.match(string).group()
# ip = re.match("([0-9]{1,3}\.){3}\d{1,3}",string).group()
print(ip)

正则表达式常用5种操作

re.match(pattern,string) #从头匹配

re.search(pattern,string) #匹配整个字符串,直到找到一个匹配

re.split() #将匹配到的格式当做分隔点对字符串分成列表类型

示例:
import re
string = "2016 02 02 00:26:35 Statistic_Svncommit.py[line:50] INFO ['A6909850956019', 'A6997448878578', 'A6909712867884', 'A6996408725555', 'A6908651568946', 'A6996150172637', 'A6909747538711', 'A6994084740002', 'A6908427730549', 'A6982187752993', 'A6909861345295', 'A6909860252981', 'A6905214359647', 'A6995115894134']"
strlist = re.search("\[\'.*?\'\]",string).group().lstrip("[").rstrip("]")
list1 = re.sub("[\'' ']","",strlist).split(",")
print(list1) 返回结果:['A6909850956019', 'A6997448878578', 'A6909712867884', 'A6996408725555', 'A6908651568946', 'A6996150172637', 'A6909747538711', 'A6994084740002', 'A6908427730549', 'A6982187752993', 'A6909861345295', 'A6909860252981', 'A6905214359647', 'A6995115894134']
列表类型 m = re.split("[0-9]", "swht1swht2jack3helenrachel8")
print(m) #['swht', 'swht', 'jack', 'helenrachel', '']  

re.findall() # 找到所有要匹配的字符并返回列表格式

示例:
import re
string = "sdsdss(welcome)dff(china)"
m = re.findall('\(.*?\)',string) #匹配所有以(开头和以)结尾的字符串,返回类型为list
print(m) #['(welcome)', '(china)']
print(type(m)) #<class 'list'>

re.sub(pattern, repl, string, count,flag) # 替换匹配到的字符

示例:
import re
string = "sdsdss(welcome)dff(china)"
m = re.sub("\(|\)","\"",string) #匹配"("或者")",然后将其替换成双引号
print(m) #sdsdss"welcome"dff"china"
print(type(m)) #<class 'str'>

模式

re.I #大小写不敏感

import rz
string = "swht"
m = re.search("[A-Z]",string,flags = re.I)
print(m.group()) #s

匹配手机号:

import re
string = "sdssaawa15865921165sdsdscf"
m = re.search("(1)([358]\d{9})",string)
print(m.group())

匹配IP地址:

import re
ip_addr = "inet 192.168.60.223 netmask 0xffffff00 broadcast 192.168.60.255"
IP = re.search("(([1-9]|[1-9][0-9]|[1][0-9][0-9]|[2][0-5][0-5])\.){3}[0-9]{1,3}",ip_addr)
print(IP.group()) #192.168.60.223

匹配邮箱地址:

import re
emailstr = "qingbo.song@gmail.com www.baidu.com"
email = re.search("^[a-z]([0-9a-z]|\.){4,20}@[0-9a-z]{0,10}\.[0-9a-z]{0,8}",emailstr)
print(email.group()) #qingbo.song@gmail.com #只匹配gmail邮箱
import
emailstr = "qingbo.song@gmail.com www.baidu.com"
email = re.search("^[a-z]([0-9a-z]|\.){4,20}@gmail\.com",emailstr)
if email:
print(email.group()) #qingbo.song@gmail.com
else:
print("系统只接受gamil邮箱注册,感谢你的支持!")

二、冒泡排序算法

示例代码:
#!/usr/local/env python3
'''
Author:@南非波波
Blog:http://www.cnblogs.com/songqingbo/
E-mail:qingbo.song@gmail.com
''' li = [11,78,45,12,90,34,56] # for m in range(1,len(li)):
# for i in range(len(li) - 1):
# if li[i] > li[i+1]:
# temp = li[i]
# li[i] = li[i+1]
# li[i+1] = temp
# print(li) for m in range(len(li) - 1):
for n in range(m+1,len(li)):
if li[m] > li[n]:
temp = li[m]
li[m] = li[n]
li[n] = temp
print(li) #[11, 12, 34, 45, 56, 78, 90] #!/usr/local/env python3
'''
Author:@南非波波
Blog:http://www.cnblogs.com/songqingbo/
E-mail:qingbo.song@gmail.com
''' data = [11,78,45,12,90,34,56]
for i in range(1,len(data)):
for j in range(len(data) - i):
if data[j] > data[j+1]:
tmp = data[j]
data[j+1] = data[j]
data[j] = tmp
print(data)

三、模块

模块介绍

模块:用一坨代码实现了某个功能的代码集合。
类似于函数式编程和面向过程编程,函数式编程则完成一个功能,其他代码用来处理和代码间的耦合。可能需要多个.py的文件组成。 模块类型:
1. 自定义模块
2. 内置标准模块(标准库)
3. 开源模块
1. 下载:
1. yum
2. apt-get
3. pip
4. easy_install
5. 源码编译安装 python stup.py install

导入模块

import module
from module.xx.xx import xx
from module.xx.xx import xx as rename
#不推荐下面的导入方式,如果被导入的模块中含有与当前文件中相同名称的函数,容易产生调用混乱。
from module.xx.xx import *

四、自定义模块

模块框架

1. backend
1. logic
handle.py
#!/usr/local/env python3
'''
Author:@南非波波
Blog:http://www.cnblogs.com/songqingbo/
E-mail:qingbo.song@gmail.com
'''
from backend.database.sql_select import select
def home():
print("welcome to home page!")
q_data = select("user","test")
print("query res:%s" % q_data) def tv():
print("welcome to tv page!") def moive():
print("welcome to moive page!")
2. database
1. sql_select.py
#!/usr/local/env python3
'''
Author:@南非波波
Blog:http://www.cnblogs.com/songqingbo/
E-mail:qingbo.song@gmail.com
'''
'''
增加模块的路径。下面的语句增加的是dj路径
该功能解决的是在子模块中的文件单独调试的时候无法获取父模块路径,导致在导入其他模块的时候报错
'''
import sys,os
#获取dj的绝对路径
base_dir = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
#将获取的路径添加到系统环境变量中
sys.path.append(base_dir) from config import settings
from backend.database.user_auth import db_auth def select(table,column):
if db_auth(settings):
if table == "user":
user_info = {
"001":["swht",24,"yunwei"],
"002":["shen",26,"dba"],
"003":["test",28,"student"],
}
return user_info
2. user_auth.py
#!/usr/local/env python3
'''
Author:@南非波波
Blog:http://www.cnblogs.com/songqingbo/
E-mail:qingbo.song@gmail.com
'''
def db_auth(configs):
if configs.DATABASES["user"] == "root" and configs.DATABASES["password"] == 123:
print("验证通过!")
return True
else:
print("验证错误!") 2. frontend
3. config
settings
#!/usr/local/env python3
'''
Author:@南非波波
Blog:http://www.cnblogs.com/songqingbo/
E-mail:qingbo.song@gmail.com
''' DATABASES = {
"engine":"mysql",
"host":"localhost",
"port":3306,
"user":"root",
"password":123,
}
4. user_main.py
#!/usr/local/env python3
'''
Author:@南非波波
Blog:http://www.cnblogs.com/songqingbo/
E-mail:qingbo.song@gmail.com
''' from backend.logic import handle handle.home()

五、标准模块

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

time & datetime模块

import time
import datetime #作用:计算一个程序从执行到结束用的时间
print(time.clock()) #返回处理器时间,3.3开始已废弃
print(time.process_time()) #返回处理器时间,3.3开始已废弃 #获取系统的当前时间,从1970年1月1日0:00到当前时间的秒数
print(time.time()) #返回当前系统时间戳 #格式化输出的时间
print(time.ctime()) #输出Tue Jan 26 18:23:48 2016 ,当前系统时间
print(time.ctime(time.time()-86640)) #将时间戳转为字符串格式,输出昨天的时间Tue Jan 25 18:23:48 2016 #time.struct_time(tm_year=2016, tm_mon=2,c=48, tm_wday=5, tm_yday=44, tm_isdst=0)
#gmtime是从零时区算起的格林时间
print(time.gmtime(time.time()-86640)) #将时间戳转换成struct_time格式
#当前时间,是按照系统当前时区算起的
print(time.localtime(time.time()-86640)) #将时间戳转换成struct_time格式,但返回 的本地时间
print(time.mktime(time.localtime())) #与time.localtime()功能相反,将struct_time格式转回成时间戳格式
#time.sleep(4) #sleep #格式化输出时间
print(time.strftime("%Y-%m-%d %H:%M:%S",time.gmtime()) ) #将struct_time格式转成指定的字符串格式
#time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())
#'2016-02-14 12:45:49' print(time.strptime("2016-01-28","%Y-%m-%d") ) #将字符串格式转换成struct_time格式 #datetime module #将时间戳转换成日期格式
print(datetime.date.today()) #输出格式python2 2016-01-26 python3:datetime.date(2016, 2, 14)
print(datetime.date.fromtimestamp(time.time()-864400) ) #2016-01-16 将时间戳转成日期格式
current_time = datetime.datetime.now() #
print(current_time) #输出2016-01-26 19:04:30.335935
print(current_time.timetuple()) #返回struct_time格式 #datetime.replace([year[, month[, day[, hour[, minute[, second[, microsecond[, tzinfo]]]]]]]])
#将当前的时间替换成输入的时间
print(current_time.replace(2014,9,12)) #输出2014-09-12 19:06:24.074900,返回当前时间,但指定的值将被替换 str_to_date = datetime.datetime.strptime("21/11/06 16:30", "%d/%m/%y %H:%M") #将字符串转换成日期格式
new_date = datetime.datetime.now() + datetime.timedelta(days=10) #比现在加10天
new_date = datetime.datetime.now() + datetime.timedelta(days=-10) #比现在减10天
new_date = datetime.datetime.now() + datetime.timedelta(hours=-10) #比现在减10小时
new_date = datetime.datetime.now() + datetime.timedelta(seconds=120) #比现在+120s
print(new_date)

random模块

产生随机数

#!/usr/local/env python3
'''
Author:@南非波波
Blog:http://www.cnblogs.com/songqingbo/
E-mail:qingbo.song@gmail.com
'''
import random #产生随机小数
print(random.random())
'''
0.8426794741026359
0.8703558703687821
''' #产生随机整数
print(random.randint(1,10))
print(random.randrange(1,10))
'''
1
9
'''

生成随机验证码

#!/usr/local/env python3
'''
Author:@南非波波
Blog:http://www.cnblogs.com/songqingbo/
E-mail:qingbo.song@gmail.com
'''
'''
生成n位随机数,包含大写字母和数字
'''
import random
def checkcode(n):
checkcode = ''
for i in range(n):
current = random.randrange(0,4)
if current != i:
tmp = chr(random.randint(65,90))
else:
tmp = random.randint(0,9)
checkcode += str(tmp)
return checkcode print(checkcode(6))

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") 重命名文件/目录,或者移动,相当于shell命令mv
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命令,直接显示,只是单个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所指向的文件或者目录的最后修改时间

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] #输入一行内容,减去后面的最后一个\n

使用sys和time模块生成进度条

#!/usr/local/env python3
'''
Author:@南非波波
Blog:http://www.cnblogs.com/songqingbo/
E-mail:qingbo.song@gmail.com
'''
'''
生成进度条。调用函数输入的参数为进度条长度
'''
import sys,time
def processbar(rangenum):
for i in range(rangenum):
if i == 0:
sys.stdout.write("0%[#")
elif i == rangenum - 1:
sys.stdout.write("#]100%")
else:
sys.stdout.write("#")
#刷新缓存,使其实时显示出来
sys.stdout.flush()
time.sleep(0.5) processbar(16)

json 和 pickle

json和pickle是用于序列化的两个模块:

json:用于处理字符串和python数据类型间的转换
pickle:用于处理python特有类型和python数据类型间的转换

json

json模块提供了四个功能:dumps、dump、loads、load
json在所有的语言中都通用,存取的直接字符

pickle

pickle模块提供了四个功能:dumps、dump、loads、load
在python中独有一个模块,存取二进制字符
不仅仅可以序列化简单的字符、列表、字典,还能序列化函数、类以至于整个程序

关于dump和dumps的区别

dump直接将序列化后的字符写到文件中,dumps是将序列化后的字符先赋给一个变量,然后再有write方法将其写入到文件中

关于load和loads的区别

load直接从文件中读取内容,loads是从内存中获取文件的内容

shutil模块

高级的 文件、文件夹、压缩包 处理模块

shutil.copyfileobj(fsrc, fdst[, length])

功能:将文件内容拷贝到另一个文件中,可以部分内容

shutil.copyfile(src, dst)

功能:仅拷贝文件

shutil.copymode(src, dst)

功能:仅拷贝权限,内容、组、用户均不变

shutil.copystat(src, dst)

功能:拷贝状态的信息,包括:mode bits, atime, mtime, flags

shutil.copy(src, dst)

功能:拷贝文件和权限

shutil.copy2(src, dst)

功能:拷贝文件和状态信息

作业需求:

模拟实现一个ATM + 购物商城程序

额度 15000或自定义
实现购物商城,买东西加入 购物车,调用信用卡接口结账
可以提现,手续费5%
每月22号出账单,每月10号为还款日,过期未还,按欠款总额 万分之5 每日计息
支持多账户登录
支持账户间转账
记录每月日常消费流水
提供还款接口
ATM记录操作日志
提供管理接口,包括添加账户、用户额度,冻结账户等。

最新文章

  1. 在公有云AZURE上部署私有云AZUREPACK以及WEBSITE CLOUD(二)
  2. Java并发1——线程创建、启动、生命周期与线程控制
  3. jfreeChart柱状图各属性详细设置
  4. Angular JS 学习之过滤器
  5. 【BZOJ3439】Kpm的MC密码 trie树+主席树
  6. 限制input输入类型(多种方法实现)
  7. java中的字符串简介,字符串的优化以及如何高效率的使用字符串
  8. Myeclipse10 + JBPM4.4 环境搭建图文教程
  9. Android中数据存储之SharedPreferences
  10. ftp服务器的搭建
  11. Effective C++(13) 用对象管理资源
  12. java数组、泛型、集合在多态中的使用及对比
  13. Excel公式-求最低价网站名字
  14. 欠了好久的CRM帖子,双11来读。
  15. 201521123101 《Java程序设计》第14周学习总结
  16. 一个小的tab切换插件
  17. OJ题:输入一个多位的数字,求各数位相加。
  18. JAVA类的继承之多态特性
  19. MarkDown里面的Emoji表情
  20. Web前端-关于jQuerry

热门文章

  1. java8系列
  2. Linux系统之路——用CentOS 7打造合适的科研环境
  3. tp查询中2个表格中字段,比较大小
  4. python基础--文件操作实现全文或单行替换
  5. C语言 结构体传值与传址分析
  6. [DeeplearningAI笔记]序列模型2.1-2.2词嵌入word embedding
  7. WdatePicker日历控件动态设置属性参数值
  8. ZOJ 3782 G - Ternary Calculation 水
  9. Mysql通过show processlist排查数据库执行慢
  10. MongoDB-3.4集群搭建:分片