标签:
Python之路_Day5_课堂笔记一、python基础二、基本数据类型
intstrlisttupledictset
三、函数式编程四、装饰器
1、将func当作参数传递给装饰器,并执行2、将装饰器函数的返回值重新赋值给func
一、作业重点二、双层装饰器三、模块
内置模块自定义模块第三方模块
四、字符串格式化五、生成器和迭代器六、其他
递归
#!/usr/bin/env python
# -.- coding: utf-8 -.-
# By sandler
USER_INFO = {}
def check_login(func):
def inner(*args,**kwargs):
if USER_INFO.get(‘is_login‘, None):
ret = func(*args,**kwargs)
return ret
else:
print(‘请登录‘)
return inner
def check_admin(func):
def inner(*args,**kwargs):
if USER_INFO.get(‘user_type‘,None) == 2:
ret = func(*args,**kwargs)
return ret
else:
print(‘权限不足‘)
return inner
@check_login
@check_admin
def index():
print(‘index‘)
index()
#!/usr/bin/env python
# -.- coding: utf-8 -.-
# By sandler
# lib/commons.py
def f1():
print("F1")
#!/usr/bin/env python
# -.- coding: utf-8 -.-
# By sandler
- # mk_dy.py
def login():
print("login")
def logout():
print("logout")
#!/usr/bin/env python
# -.- coding: utf-8 -.-
# By sandler
# mokuai.py
import mk_dy
import lib.commons
mk_dy.login()
lib.commons.f1()
#!/usr/bin/env python
# -.- coding: utf-8 -.-
# By sandler
# requests 测试
import requests
import json
response = requests.get(‘http://wthrcdn.etouch.cn/weather_mini?city=北京‘)
response.encoding = ‘utf-8‘
# print(response.text)
dic = json.loads(response.text)
print(dic)
#!/usr/bin/env python
# -.- coding: utf-8 -.-
# By sandler
import json
r = json.dumps([11,22,33])
print(r,type(r))
li = ‘["sandler","yuli"]‘
ret = json.loads(li)
print(ret,type(ret))
- + 右对齐;正数前加正好,负数前加负号;
- - 左对齐;正数前无符号,负数前加负号;
- 空格 右对齐;正数前加空格,负数前加负号;
- 0 右对齐;正数前无符号,负数前加负号;用0填充空白处
- s,获取传入对象的__str__方法的返回值,并将其格式化到指定位置
- r,获取传入对象的__repr__方法的返回值,并将其格式化到指定位置
- c,整数:将数字转换成其unicode对应的值,10进制范围为 0 <= i <= 1114111(py27则只支持0-255);字符:将字符添加到指定位置
- o,将整数转换成 八 进制表示,并将其格式化到指定位置
- x,将整数转换成十六进制表示,并将其格式化到指定位置
- d,将整数、浮点数转换成 十 进制表示,并将其格式化到指定位置
- e,将整数、浮点数转换成科学计数法,并将其格式化到指定位置(小写e)
- E,将整数、浮点数转换成科学计数法,并将其格式化到指定位置(大写E)
- f, 将整数、浮点数转换成浮点数表示,并将其格式化到指定位置(默认保留小数点后6位)
- F,同上
- g,自动调整将整数、浮点数转换成 浮点型或科学计数法表示(超过6位数用科学计数法),并将其格式化到指定位置(如果是科学计数则是e;)
- G,自动调整将整数、浮点数转换成 浮点型或科学计数法表示(超过6位数用科学计数法),并将其格式化到指定位置(如果是科学计数则是E;)
- %,当字符串中存在格式化标志时,需要用 %%表示一个百分号
tpl = "i am %s" % "alex"
tpl = "i am %s age %d" % ("alex", 18)
tpl = "i am %(name)s age %(age)d" % {"name": "alex", "age": 18}
tpl = "percent %.2f" % 99.97623
tpl = "i am %(pp).2f" % {"pp": 123.425556, }
tpl = "i am %.2f %%" % {123.425556, }
- <,内容左对齐
- >,内容右对齐(默认)
- =,内容右对齐,将符号放置在填充字符的左侧,且只对数字类型有效。 即使:符号+填充物+数字
- ^,内容居中
- +,正号加正,负号加负;
- -,正号不变,负号加负;
- 空格 ,正号空格,负号加负;
传入” 字符串类型 “的参数
- s,格式化字符串类型数据
- 空白,未指定类型,则默认是None,同s
传入“ 整数类型 ”的参数
- b,将10进制整数自动转换成2进制表示然后格式化
- c,将10进制整数自动转换为其对应的unicode字符
- d,十进制整数
- o,将10进制整数自动转换成8进制表示然后格式化;
- x,将10进制整数自动转换成16进制表示然后格式化(小写x)
- X,将10进制整数自动转换成16进制表示然后格式化(大写X)
传入“ 浮点型或小数类型 ”的参数
- e, 转换为科学计数法(小写e)表示,然后格式化;
- E, 转换为科学计数法(大写E)表示,然后格式化;
- f , 转换为浮点型(默认小数点后保留6位)表示,然后格式化;
- F, 转换为浮点型(默认小数点后保留6位)表示,然后格式化;
- g, 自动在e和f中切换
- G, 自动在E和F中切换
- %,显示百分比(默认显示小数点后6位)
tpl = "i am {}, age {}, {}".format("seven", 18, ‘alex‘)
tpl = "i am {}, age {}, {}".format(*["seven", 18, ‘alex‘])
tpl = "i am {0}, age {1}, really {0}".format("seven", 18)
tpl = "i am {0}, age {1}, really {0}".format(*["seven", 18])
tpl = "i am {name}, age {age}, really {name}".format(name="seven", age=18)
tpl = "i am {name}, age {age}, really {name}".format(**{"name": "seven", "age": 18})
tpl = "i am {0[0]}, age {0[1]}, really {0[2]}".format([1, 2, 3], [11, 22, 33])
tpl = "i am {:s}, age {:d}, money {:f}".format("seven", 18, 88888.1)
tpl = "i am {:s}, age {:d}".format(*["seven", 18])
tpl = "i am {name:s}, age {age:d}".format(name="seven", age=18)
tpl = "i am {name:s}, age {age:d}".format(**{"name": "seven", "age": 18})
tpl = "numbers: {:b},{:o},{:d},{:x},{:X}, {:%}".format(15, 15, 15, 15, 15, 15.87623, 2)
tpl = "numbers: {:b},{:o},{:d},{:x},{:X}, {:%}".format(15, 15, 15, 15, 15, 15.87623, 2)
tpl = "numbers: {0:b},{0:o},{0:d},{0:x},{0:X}, {0:%}".format(15)
tpl = "numbers: {num:b},{num:o},{num:d},{num:x},{num:X}, {num:%}".format(num=15)
>>> s1 = "--{:^20s}--{:+d}--{:x}".format(‘sandler‘,20,15)
>>> print(s1)
-- sandler --+20--f
>>> s2 = "--{:*^20s}--{:+d}--{:#x}".format(‘sandler‘,20,15)
>>> print(s2)
--******sandler*******--+20--0xf
>>> s3 = "numbers: {:b},{:o},{:d},{:x},{:X},{:%},{:d}".format(15, 15, 15, 15, 15, 15.87623, 2)
>>> print(s3)
numbers: 1111,17,15,f,F,1587.623000%,2
>>> s4 = "\tI am \033[31m{:s}\033[0m \n\tAge \033[31m{:d}\033[0m".format(‘sandler‘,20)
>>> print(s4)
I am sandler
Age 20
#!/usr/bin/env python
# -.- coding: utf-8 -.-
# By sandler
def func():
print(111)
yield 1
print(222)
yield 2
print(333)
yield 3
ret = func()
r1 = ret.__next__()
print(r1)
r2 = ret.__next__()
print(r2)
r3 = ret.__next__()
print(r3)
#!/usr/bin/env python
# -.- coding: utf-8 -.-
# By sandler
def myrange(arg):
start = 0
while True:
if start > arg:
return
yield start
start += 1
ret = myrange(3)
r1 = ret.__next__()
print(r1)
r2 = ret.__next__()
print(r2)
r3 = ret.__next__()
print(r3)
r4 = ret.__next__()
print(r4)
#!/usr/bin/env python
# -.- coding: utf-8 -.-
# By sandler
def func(n):
n += 1
if n >= 4:
return ‘end‘
return func(n)
ret = func(1)
print(ret)
标签:
原文地址:http://www.cnblogs.com/sandler613/p/5561959.html