funny features
'''
python 3.8 引入不少好玩的新特性:比如判断条件里可以赋值的海象运算:=,限定位置/,限定关键字*
f-string说明符,
'''
# 1 position *,/ 海象运算
def fun(a, b, /, c, d):
return a + b
print(fun(2, 3, c=5, d=6))
for i in [6, 4]:
if (x := 3) > 2:
print('x = ', x)
x = i
print('x = ?', x)
x = 0
for i in [100, 4]:
if (x := x + 3) > 2:
print('x = ', x)
x = i
print('x = ?', x)
# x := sentence 使得判断,内联中也可以进行赋值操作;
# 2 函数形参中的/,表示限定符;表示前面仅仅限位置形参(a,b,/),(a=a,b=b,/)报错;
# 3 函数形参中的*,表示关键字形参,后面的变量只能用a = x,b=y形式传入参数,否则报错;
def funcStar(a,b,/,c,*,d,e, **kwargs):
print(kwargs)
#return a + b + c + d + e
ls = kwargs.values()
return a + b + c + d + e + sum(ls)
print(funcStar(2,3,4,d=5,e=6))
print(funcStar(2,3,c=4, d=5,e = 6))
#print(funcStar(2,3,4,d=5,6) )
print(funcStar(2,3,4,d=5,e=6,f=7,g=8)) # ** kwargs是传入一个字典{name:value}形式;
def funcSum(*args):
print(type(args))
print("sum is :", sum(args))
funcSum(2,3,4) # * 则是传入一个列表;
# 4 f-string特性:说明符 '=' (说明符=会在f-string中输出'变量名=')
data = ['wakaka', '娃哈哈', 3]
counter = len(data)
dig = 2567891011
print(f'{data},{counter}')
print(f'{data=},{counter=},{dig=:,d}') #=: ,d 格式化输出xxx,xxx形式
更详细可以参考:
https://docs.python.org/zh-cn/3.8/whatsnew/3.8.html
logging editing
'''
会使用pythonlogging函数,了解事件级别debug,info,warrning,error,critical;
事件的基本配置basicconfig
'''
import logging
import time
fmt = '%d-%d-%d-%d-%d-%d'%(time.localtime()[0:6]) #格式化
# 对 basicConfig() 的调用应该在 debug() ,
# info() 等的前面。因为它被设计为一次性的配置,只有第一次调用会进行操作,随后的调用不会产生有效操作。
logging.basicConfig(
filename = 'math_func_logging' + fmt + '.logging',
format='[%(asctime)s] [%(levelname)s]: %(message)s',
level=logging.DEBUG)
logging.info('%d now start!', 1)
logging.warning('%s warn an event %s','Root', 'Divide 0')
logging.error('%s raise an error %s', 'Function a', 'no input')
logging.critical('Program %s corruppted', 'immeditly')
logging.debug('This is happy end!')