关于if __name__ == "__main__":
若执行文件为bin,调用文件为cal;
若在执行文件bin中执行print(__name__) 输出:__main__ 当print(__name__)在调用文件中被调用执行则 输出当前调用文件的路径:lesson.web.web1.web2.cal
也就是说只有在执行文件中__name__才等于__main__
if __name__ == "__main__": 作用
- 用于执行文件时(#不想让其他人来调用)
- 用于调用文件时(#用于调用文件的测试)
time模块
import time #----时间戳---- print(time.time()) #时间戳:1542779690.9086285,用于计算 #----当地时间---- print(time.localtime(9999999999)) #结构化时间struct_time,默认参数是当前时间time.time() print(time.localtime().tm_year) #只打印年 #----国际标准时间----UTC print(time.gmtime()) #按英国的算差8个小时 #-------------------------------------------------------- #----将时间戳转换为结构化时间---- #Timestamp--->>>struct_time print(time.localtime(9999999999)) #----将结构化时间转换为时间戳---- #struct_time--->>>Timestamp print(time.mktime(time.localtime())) #----将结构化时间转换成字符串时间---- #struct_time--->>>Formait_string print(time.strftime("%Y-%m-%d-%X",time.localtime())) #第一个参数是字符串时间的排布,第二个参数是结构化时间 #----将字符串时间转换成结构化时间---- #Format_string--->>>struct_time print(time.strptime("2018-11-21-14:16:08","%Y-%m-%d-%X"))#第一个参数是字符串时间,第二个参数是时间排布,必须和第一个参数一一对应 #直接看一个时间,不需要排布(固定) print(time.asctime()) #Wed Nov 21 14:24:48 2018,把一个结构化时间转换成固定的字符串表达形式 print(time.ctime()) #Wed Nov 21 14:25:51 2018,把一个时间戳转换成固定格式的字符串
时间转换:
random模块
import random print(random.random()) #随机取[0,1)的浮点数 print(random.randint(1,5)) #随机取[1,5]的整数 print(random.randrange(1,5)) #随机取[1,5)的整数 print(random.choice([55,8,6,[11,22,33]])) #随机取出列表中的一个元素 print(random.sample([55,8,6],2))#随机取出列表中的2个元素 print(random.uniform(0,9))#随机取出任意范围的浮点型[0,9) #打乱顺序 item = [1,3,5,7,9] random.shuffle(item) print(item)
def inner(): res = "" for i in range(4): #几位验证码就做几次循环 num = random.randint(0,9) #随机生成0,9的数字 alf = chr(random.randint(65,122)) #将随机生成的数字,用chr()转换成字母 s = str(random.choice([num,alf])) #随机取出数字或字母 res += s #拼接 return res print(inner())