您的位置:首页 > 编程语言 > Python开发

python第五课

2015-12-02 23:48 585 查看
翻译三层装饰器
 def Before(request,kargs):
print 'before' def After(request,kargs): print 'after' def Filter(before_func,after_func): def outer(main_func): def wrapper(request,kargs): before_result = before_func(request,kargs) if(before_result != None): return before_result;           main_result = main_func(request,kargs) if(main_result != None): return main_result; after_result = after_func(request,kargs) if(after_result != None): return after_result; return wrapper return outer @Filter(Before, After)def Index(request,kargs): print 'index'
1. def Before(request,kargs)
2. def After(request,kargs)
3. def Filter(before_func,after_func)
4. @Filter(Before, After)a. fileter(befor,after) return outerb. @outer(main_func) return wrapperc. index(request,kargs) -à wrapper(request,kargs)

递归
函数自已调用自己
def fun(i):
i = i+1
fun(i)
fun(0)
如果需要返回值,那么,需要 return fun(i)如果不加return,那么返回值为none
def fun(i):
i +=1
if i < 100:return fun(i)
else: return i

n = fun(0)
print n

模块
实现某种功能的所有代码的集合
内置模块 开源模块  自定义模块
导入 import module from module.xx import xx from module.xx import xx as newname from module.xx import *导入一个包,解释器解释该包下的__init__.py文件  (文件夹)查找模块的路径 sys.path,返回值为列表,使用sys.path.append(‘路径’)可以添加路径,但是是临时的内置模块

os.getcmd() 获取当前工作目录os.chdir(‘’) 改变当前工作目录os.curdir返回当前目录 (‘.’)os.pardir返回当前目录的父目录字符串名 (‘..’)os.makedirs(‘dirname1/dirname2’) 可生成多层递归目录os.removedirs(‘dirname1’) 若目录为空,则删除并返回上层目录,如若也为空,则删除,依此类推os.mkdir(‘dirname’) 生成单级空目录os.rmdir(‘dirname’)   删除单级空目录os.listdir(‘dirname’) 列出指定目录下的所有文件和子目录,包括隐藏文件,并以列表方式打印os.remove() 删除一个文件os.rename(‘oldname’,’newname’) 重命名文件/目录 os.stat(‘path/filename’)  获取文件目录信处os.sep 输出操作系统特定的路径分隔符 win ‘\\’ linux ‘/’os.lineseq 输出当前平台使用的行终止符 win ‘\r\n’ linux ‘\n’os.pathseq 输出用于分割文件路径的字符串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.pah.basename(path) 返回path最后的文件名,是os.path.split(path)的第二个元素os.path.exists(path) 判断path 是否存在os.path.istabs(path) 判断path是否为绝对路径os.path.isfile(path) 判断path是否为一个存在的文件os.path.isdir(path) 判断path是否为一个存在的目录os.path.join(path1[,path2..]) 将多个路径组合后返回os.path.getatime(path) 返回path所指向的文件或目录的最后存取时间os.path.getmtime(path) 返回path所指向的文件或目录的最后修改时间文件的 Access time,atime 是在读取文件或者执行文件时更改的;
文件的 Modified time,mtime 是在写入文件时随文件内容的更改而更改的;
文件的 Create time,ctime 是在写入文件、更改所有者、权限或链接设置时随 Inode 的内容更改而更改的。 ls -lc filename 列出文件的 ctime
ls -lu filename 列出文件的 atime
ls -l filename 列出文件的 mtime hashlib

import hashlibhash = hashlib.md5() #hash.sha1() hash.sha256() hash.sha384()hash.updat(‘admin’)print hash.hexdigest()等效hashlib.md5(‘admin’).hexdigest()对加密算法中添加自定义key再来做加密hash=hashlib.md5(‘abc’)hash.update(‘admin’)print hash.hexdigest()等郊hashlib.md5(‘abc’+’admin’).hexdigest()configparse

[section1]k1 = v1k2 = v2[section2] k1 = v1#!/usr/bin/env python#-*- coding:utf-8 -*-import ConfigParserconfig = ConfigParser.ConfigParser()config.read('i.cfg')secs = config.sections()print secsoptions = config.options('section2')print optionsitem_list = config.items('section1')print item_listval = config.get('section1','k1')print val#sec = config.add_section('han')#config.write(open('i.cfg','w'))options = config.options('han')print options#config.remove_section('han')#config.write(open('i.cfg','w'))config.set('han','age',38)config.set('han','name','hanyanling')config.set('han','gentle','male') config.write(open('i.cfg','w')) config.remove_option(‘han’,’age’) config.write(open(‘i.cfg’,’w’))random
random.random() 返回0到1之间的一个随机数random.randint(a,b) 返回a和b之间的一个数,包括a和b random.randrange ([start], stop[, step]),从指定范围内,按指定基数递增的集合中获取一个随机数random.shuffle(x[,random]) 将一个列表中的元素打散p = ["Python","is", "powerful","simple", "and so on..."]
random.shuffle(p)
print prandom.sample(sequence,k) 从指定列表中随机获取指定长度的片断,不修改原列表list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
slice = random.sample(list, 5) #从list中随机获取5个元素,作为一个片断返回
print slice print list #原有序列并没有改变

随机验证码
import random
checkcode = ''
for i in range(4):
current = random.randrange(0,4)
if current != i:
temp = chr(random.randint(65,90)) #取A-Z(65-90)中随机一个字母
else:
temp = random.randint(0,9)  #取0-9中随机一个数字
checkcode += str(temp)  
print checkcode
1.在write的时候报错,UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordin
al not in range(128)import sys
print sys.getdefaultencoding()
# 'ascii'
基本上是ascii编码方式,由此Python自然调用ascii编码解码程序去处理字符流,当字符流不属于ascii范围内,就会抛出异常(ordinal not in range(128))解决方法 import sys reload(sys) sys.setdefaultencoding(‘utf-8’)2. raw_input 中文在win终端不能正常显示,需要解码再编码 raw_input("请输入密码:".decode('utf-8').encode('gbk'))
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python 模块