雨默婷婷

第三天笔记整理

一、文件处理

# f=open(r'a.txt','w',encoding='utf-8')
# # print(f.writable())
# f.write('1111\n')
# f.write('2222\n')
# f.writelines(['3333\n','444\n'])
# f.close()


#a模式:文件不存在则创建,文件存在那么在打开文件后立刻将光标移动到文件末尾,进行追加写
# f=open(r'b.txt','a',encoding='utf-8')
# # print(f.writable())
# f.write('4444\n')
# f.write('5555\n')
# f.writelines(['66666\n','7777\n'])
# f.close()#关闭文件


#r模式:只读模式,并且只能读文本文件
# f=open(r'b.txt','r',encoding='utf-8')
# # print(f.writable())
# # print(f.read())
# # print(f.readlines())
# print(f.readline(),end='')
# print(f.readline(),end='')
# f.close()


# with open('b.txt','r',encoding='utf-8') as f:
# # while True:
# # line=f.readline()
# # if len(line) == 0:break
# # print(line)
#
# # print(f.readline())
# # print(f.readline())
# # print(f.readline())
# # print(f.readline())
# # print(f.readline())
# # print(f.readline())
# # print(f.readline())
# # print('第八次',f.readline())
#
# for line in f:
# print(line)


#b:bytes,rb模式,可以读任何格式的文件,不需要指定字符编码
# with open('111.png','rb') as f:
# print(f.read())

# with open('b.txt','rb',) as f:
# print(f.read().decode('utf-8'))

# with open('b.txt','rt',encoding='utf-8') as f:
# print(f.read())

#wb:只写模式,写入的是二进制的形式,如果要显示成字符串,需要用decode反解码
# with open('b.txt','wb') as f:
# res='中问'.encode('utf-8')
# print(res,type(res))
# f.write(res)


# with open('b.txt','ab') as f:
# res='哈哈哈'.encode('utf-8')
# print(res,type(res))
# f.write(res)

#encoding:utf-8这个指定的是打开这个文件的格式要是utf-8
with open('b.txt','rt',encoding='utf-8') as f: #encoding='utf-8' 指的是需要操作系统用utf-8的形式把b.txt文件读到内存中
print(f.read())

二、文件修改

# import os#导入一个模块
# with open('info.txt','r',encoding='utf-8') as read_f,open('.info.txt.swap','w',encoding='utf-8') as write_f:
# data=read_f.read()#
读出文件所以内容
# write_f.write(data.replace('alex','SB'))#将文件中所有的‘’alex‘’
替换为‘’SB‘’
# os.remove('info.txt') 删除源文件
# os.rename('.info.txt.swap','info.txt')
将修改后的文件名称改为源文件的名称

#
# import os
#
# with open('info.txt', 'r', encoding='utf-8') as read_f, open('.info.txt.swap', 'w', encoding='utf-8') as write_f:
# for line in read_f:
#每从read_f中读入一行内容后,就会判断该行内容中有没有可以替换的内容,替换后再赋值给一个新的变量line,
# if 'SB' in line:
# line=line.replace('SB','alex')
# write_f.write(line)#
将修改后的内容写入write_f文件中
#
# os.remove('info.txt')
# os.rename('.info.txt.swap', 'info.txt')

三、函数对象及嵌套

# import os#导入一个模块
# with open('info.txt','r',encoding='utf-8') as read_f,open('.info.txt.swap','w',encoding='utf-8') as write_f:
# data=read_f.read()#
读出文件所以内容
# write_f.write(data.replace('alex','SB'))#将文件中所有的‘’alex‘’
替换为‘’SB‘’
# os.remove('info.txt') 删除源文件
# os.rename('.info.txt.swap','info.txt')
将修改后的文件名称改为源文件的名称

#
# import os
#
# with open('info.txt', 'r', encoding='utf-8') as read_f, open('.info.txt.swap', 'w', encoding='utf-8') as write_f:
# for line in read_f:
#每从read_f中读入一行内容后,就会判断该行内容中有没有可以替换的内容,替换后再赋值给一个新的变量line,
# if 'SB' in line:
# line=line.replace('SB','alex')
# write_f.write(line)#
将修改后的内容写入write_f文件中
#
# os.remove('info.txt')
# os.rename('.info.txt.swap', 'info.txt')

 

 

 

#1、函数的嵌套调用
# def my_max(x,y):
# if x >= y:
# return x
# else:
# return y
#
# def my_max4(a,b,c,d):#嵌套调用 就是在一个函数内部 调用调用另一个函数
# res1=my_max(a,b)
# res2=my_max(res1,c)
# res3=my_max(res2,d)
# return res3


#2、函数的嵌套定义
def f1():
def f2():
print(
'from f2')
def
f3():
print(
'from f3')
f3()
# print(f2)
f2()


f1()
# f2

五、名称空间

#名称空间指的是:存放名字与值绑定关系的地方,

#内置名称空间(python解释器启动就有):python解释器内置的名字,max,len,print
#全局名称空间(执行python文件时生效):文件级别定义的名字
# x=1
# def func():pass
# import time
# if x == 1:
# y=2

#局部名称空间(函数调用时生效,调用结束失效):函数内部定义的名字
# func()

#加载顺序:内置---》全局----》局部名称空间
#访问名字的顺序:局部名称空间===》全局----》内置

# x=1
# print(x)

# print(max)

# max=2
# def func():
# # max=1
# print(max)
#
# func()


# x='gobal'
# def f1():
# # x=1
# def f2():
# # x=2
# def f3():
# # x=3
# print(x)
# f3()
# f2()
#
# f1()



#全局作用域(全局范围):内置名称空间与全局名称空间的名字,全局存活,全局有效,globals()
#局部作用域(局部范围):局部名称空间的名字,临时存活,局部有效,locals()

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx=111111111111111111111
# print(globals())
# print(dir(globals()['__builtins__']))

# print(locals() is globals())

# def func():
# yyyyyyyyyyyyyyyyyyyyyyyy=22222222
# print(globals())
# print(locals())
#
# func()


# x=100
# def func():
# global x
# x=1
#
# func()
# print(x)



# x='global'
# def f1():
# # x=1
# def f2():
# nonlocal x
# x=0
# f2()
# print('===f1 innter--->',x)
#
# f1()
# print(x)





#强调两点:
#1、打破函数层级限制来调用函数
# def outter():
# def inner():
# print('inner')
# return inner
#
# f=outter()
# # print(f)
#
# def bar():
# f()
# bar()


#2、函数的作用域关系是在函数定义阶段就已经固定了,与调用位置无关
x=1
def outter():
# x=2
def inner():
print(
'inner',x)
return
inner

f=outter()
# print(f)
# x=1111111111111111111111111111111111111111111111111111111111111111111111111111111111
def bar():
x=3
f()
# x=1111111111111111111111111111111111111111111111111111111111111111111111111111111111
bar()
x=
1111111111111111111111111111111111111111111111111111111111111111111111111111111111
六、闭包函数

#闭包函数:
#1 定义在函数内部的函数
#2 该函数的函数体代码包含对外部作用域(而不是全局作用域)名字的引用
#3 通常将闭包函数用return返回,然后可以在任意使用
# z=1
# def outer():
# x=1
# y=2
# def inner():
# print(x,y)
# # print(z)
# return inner
#
# f=outer()
# print(f.__closure__[0].cell_contents)
# print(f.__closure__[1].cell_contents)
# print(f.__closure__)


# def bar():
# x=111121
# y=2222
# f()
#
# bar()




# def foo(x,y):
# print(x+y)
#
# foo(1,2)

# def outter():
# x=1
# y=2
# def foo():
# print(x+y)
# return foo
#
#
# f=outter()
#
# f()



#爬页面:
闭包函数为我们提供了一种新的为函数传参的方式
import requests #pip3 install requests

# def get(url):
# response=requests.get(url)
# if response.status_code == 200:
# print(len(response.text))
#
# get('https://www.baidu.com')
# get('https://www.baidu.com')
# get('https://www.baidu.com')

def outter(url):
# url = 'https://www.baidu.com'
def get():
response=requests.get(url)
if response.status_code == 200:
print(len(response.text))
return get

baidu=outter(
'https://www.baidu.com')
python=outter(
'https://www.python.org')
# baidu()
# baidu()
# baidu()



七、装饰器及装饰器修订

#1、开放封闭原则:对扩展开放,对修改是封闭

#2、装饰器:装饰它人的,器指的是任意可调用对象,现在的场景装饰器-》函数,被装饰的对象也是-》函数
#原则:1、不修改被装饰对象的源代码 2、不修改被装饰对象的调用方式
#装饰器的目的:在遵循1,2的前提下为被装饰对象添加上新功能


#错误的示范
# import time
#
# def index():
# time.sleep(3)
# print('welecome to index')
#
# def timmer(func):
# start=time.time()
# func()
# stop=time.time()
# print('run time is %s' %(stop-start))
#
# timmer(index)


#
# import time
# def index():
# time.sleep(3)
# print('welecome to index')
#
# def timmer(func):
# # func=index #最原始的index
# def inner():
# start=time.time()
# func() #最原始的index
# stop=time.time()
# print('run time is %s' %(stop-start))
# return inner
#
# index=timmer(index) #index=inner
# # print(f)
# index() #inner()

 

 

#装饰器语法:在被装饰对象正上方单独一行写上,@装饰器名

# #改进一:
# import time
# def timmer(func):
# def inner():
# start=time.time()
# res=func()
# stop=time.time()
# print('run time is %s' %(stop-start))
# return res
# return inner
#
# @timmer #index=timmer(index)
# def index():
# time.sleep(1)
# print('welecome to index')
# return 1111
#
# res=index() #res=inner()
# print(res)


#改进二:
import time
def timmer(func):
def inner(*args,**kwargs):
start=time.time()
res=func(*args,**kwargs)
stop=time.time()
print(
'run time is %s' %(stop-start))
return
res
return inner

# @timmer #index=timmer(index)
# def index(name):
# time.sleep(1)
# print('welecome %s to index' %name)
# return 1111
# res=index('egon') #res=inner('egon')
# print(res)

@timmer #home=timmer(home)
def home(name):
print(
'welcome %s to home page' %name)

home(
'egon') #inner('egon')

八、有参数装饰器

# import time
# def auth(func): # func=index
# def inner(*args,**kwargs):
# name=input('name>>: ').strip()
# password=input('password>>: ').strip()
# if name == 'egon' and password == '123':
# print('login successful')
# return func(*args,**kwargs)
# else:
# print('login err')
# return inner
#
# @auth
# def index(name):
# time.sleep(1)
# print('welecome %s to index' %name)
# return 1111
#
# res=index('egon')
# print(res)

#有参装饰器
# import time
#
# def auth2(engine='file'):
# def auth(func): # func=index
# def inner(*args,**kwargs):
# if engine == 'file':
# name=input('name>>: ').strip()
# password=input('password>>: ').strip()
# if name == 'egon' and password == '123':
# print('login successful')
# return func(*args,**kwargs)
# else:
# print('login err')
# elif engine == 'mysql':
# print('mysql auth')
# elif engine == 'ldap':
# print('ldap auth')
# else:
# print('engin not exists')
# return inner
# return auth
#
# @auth2(engine='mysql') #@auth #index=auth(index) #index=inner
# def index(name):
# time.sleep(1)
# print('welecome %s to index' %name)
# return 1111
#
# res=index('egon') #res=inner('egon')
九、并列多个装饰器
import time
def timmer(func):
def inner(*args,**kwargs):
start=time.time()
res=func(*args,**kwargs)
stop=time.time()
print('run time is %s' %(stop-start))
return
res
return inner

def auth2(engine='file'):
def
auth(func): # func=index
def inner(*args,**kwargs):
if engine == 'file':
name=input(
'name>>: ').strip()
password=input(
'password>>: ').strip()
if
name == 'egon' and password == '123':
print(
'login successful')
return
func(*args,**kwargs)
else:
print(
'login err')
elif
engine == 'mysql':
print(
'mysql auth')
elif
engine == 'ldap':
print(
'ldap auth')
else
:
print(
'engin not exists')
return
inner
return auth


@
auth2(engine='file')
@
timmer
def index(name):
time.sleep(
1)
print(
'welecome %s to index' %name)
return
1111

res=index('egon')
print(res)

 

posted on 2018-01-02 17:53  雨默婷婷  阅读(123)  评论(0编辑  收藏  举报

导航