# ********************day21-time与random等常用模块与包 *******************
# =====>>>>>>内容概览
# =====>>>>>>内容概览
# =====>>>>>>内容概览# ------------------------------------------------------------
# # 1、import 模块文件
# # # 导入整个模块文件
# ------------------------------------------------------------# ------------------------------------------------------------
# # 2、from 文件 import 函数名
# # # 导文模块文件中所需要用到的函数
# ------------------------------------------------------------# ------------------------------------------------------------
# # 3、from 文件 import *
# # # 导文模块文件中 所有 的函数
# # # 这种方式是不退荐的,因为会把一个模块文件中的所有函数全部都导入进来,原因:
# # # 1、导入多
# # # 2、导入的函数名,可能与当前所用的文件中所定义的文件,出现名称重复,导致功能不正常
# ------------------------------------------------------------# ------------------------------------------------------------
# # 4、sys.path
# # # 执行文件所在的路径
# # 模块 import:
# 1、执行 对应文件
# 2、引入变量名
# ------------------------------------------------------------# ------------------------------------------------------------
# # 5、# from 子目录 import 文件
# # # from my_module import cal
# # # 从该运行文件中的子集目录中,引入文件
# # # 需要注意的是,引入的文件 没有包含其他的引入文件
# ------------------------------------------------------------# ------------------------------------------------------------
# # 6、from 子目录 import 文件1
# # # from my_module import main
# # # 需要注意的是,引入的文件1中,包含其他的文件2,文件2与文件1 同目录
# # # ===>>以下的实例是会报错的!
# # # 出错的原因是找不到cal文件
# ------------------------------------------------------------# ------------------------------------------------------------
# # 7、from 子目录 import 文件1
# # # from my_module import main
# # # 需要注意的是,引入的文件1中,包含其他的文件2,文件2与运行文件 同目录
# # # ===>>以下的实例是 不会 报错的!注意与上面区别
# ------------------------------------------------------------# ------------------------------------------------------------
# # 8、包的概念
# # # 包的概念:包是用来组织模块的!在pycharm建立的python包中,下面会自动生成一个__init()__文件
# # # 调用包就是执行包下的__init__.py文件
# # # 引入了包以后,只要顶层的包名不与别人冲突,那所有模块都不会与别人冲突。现在,view.py模块的
# # # 名字就变成了hello_django.app01.views,类似的,manage.py的模块名则是hello_django.manage。
# ------------------------------------------------------------# ------------------------------------------------------------
# # 9、from 包1.包2.包3.被调用文件 import 函数名
# # # from web.web1.web3.cal import add
# # # 从包中引入文件
# ------------------------------------------------------------# ------------------------------------------------------------
# 10、from 包1.包2 import 包3
# # # from web.web1 import web3
# # # 不修改包3下的文件__init()__,从包中引入文件
# # # ===>>>注意:报错,原因是这种方式在不修改包3下的文件__init()__<<<===
# ------------------------------------------------------------# ------------------------------------------------------------
# # 11、from 包1.包2 import 包3
# # # from web.web1 import web3
# # # ''修改''包3下的文件__init()__,从包中引入文件,添加内容如下:
# # # from . import cal
# ------------------------------------------------------------# ------------------------------------------------------------
# # 12、总结,关于包下的内容调用
# # # 推荐使用方式一与方式二
# # # 方式三不推荐
# # # 方式一:from 包1.包2.包3 import 被调用文件
# # # from web.web1.web3 import cal
# # #
# # # 方式二:from 包1.包2.包3.被调用文件 import 函数名
# # # from web.web1.web3.cal import add
# # #
# # # 方式三:from 包1.包2 import 包3
# # # from web.web1 import web3
# # # ''修改''包3下的文件__init()__
# ------------------------------------------------------------# ------------------------------------------------------------
# # 13、__name__
# # # 注意区别执行文件下的 __name__ 是 __main__,而被调用文件下的__name__是一个路径
# ------------------------------------------------------------# ------------------------------------------------------------
# # 14、if __name__ == "__main__":
# # # 只是执行该文件下才会使用
# # # 功能一:如果我们是直接执行某个.py文件的时候,该文件中那么”__name__ == '__main__'“是True,
# # # 但是我们如果从另外一个.py文件通过import导入该文件的时候,这时__name__的值就是我们这个py文件的名字
# # # 而不是__main__。
# # #
# # # 功能二:调试代码的时候,在”if __name__ == '__main__'“中
# # # 加入一些我们的调试代码,我们可以让外部模块调用的时候不执行我们的调试代码,但是如果我们想排查问题
# # # 的时候,直接执行该模块文件,调试代码能够正常运行!
# ------------------------------------------------------------# ------------------------------------------------------------
# # 15、sleep
# # # Python time sleep() 函数推迟调用线程的运行,可通过参数secs指秒数,表示进程挂起的时间。
# ------------------------------------------------------------# ------------------------------------------------------------
# # 16、time.time()
# # # 时间戳(timestamp) :
# # # 打印的时间是从1970.1.1 00:00凌晨开始算,多少秒
# # # 时间戳(timestamp) : 通常来说,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量。
# # # 我们运行“type(time.time())”,返回的是float类型。
# ------------------------------------------------------------# ------------------------------------------------------------
# # 17、time.localtime()
# # # 当前的时间,是以我所在的时区进行计算的,其默认值为time.time()
# # # 元组(struct_time) : struct_time元组共有9个元素共九个元素:
# # # (年,月,日,时,分,秒,一年中第几周,一年中第几天,夏令时)
# # #
# # # int tm_sec; /* 秒 – 取值区间为[0,59] */
# # # int tm_min; /* 分 - 取值区间为[0,59] */
# # # int tm_hour; /* 时 - 取值区间为[0,23] */
# # # int tm_mday; /* 一个月中的日期 - 取值区间为[1,31] */
# # # int tm_mon; /* 月份(从一月开始,0代表一月) - 取值区间为[0,11] */
# # # int tm_year; /* 年份,其值等于实际年份减去1900 */
# # # int tm_wday; /* 星期 – 取值区间为[0,6],其中0代表星期天,1代表星期一,以此类推 */
# # # int tm_yday; /* 从每年的1月1日开始的天数 – 取值区间为[0,365],其中0代表1月1日,1代表1月2日,以此类推 */
# # # int tm_isdst; /* 夏令时标识符,实行夏令时的时候,tm_isdst为正。不实行夏令时的时候,tm_isdst为0;不了解情况时,tm_isdst()为负。
# ------------------------------------------------------------# ------------------------------------------------------------
# # 18、time.gmtime()
# # # gmtime()方法是将一个时间戳转换为UTC时区。其默认值为time.time()
# # # 用法与localtime差不多,只是基准的时区不一样
# # # 元组(struct_time) : struct_time元组共有9个元素共九个元素:
# # # (年,月,日,时,分,秒,一年中第几周,一年中第几天,夏令时)
# ------------------------------------------------------------# ------------------------------------------------------------
# # 19、time.mktime()
# # # 将一个struct_time转化为时间戳。
# ------------------------------------------------------------# ------------------------------------------------------------
# # 20、time.strftime()
# # # 将一个struct_time转化为时间戳。
# # # 把一个代表时间的元组或者struct_time(如由time.localtime()和
# # # time.gmtime()返回)转化为格式化的时间字符串。如果t未指定,将传入time.localtime()。
# # # 如果元组中任何一个元素越界,ValueError的错误将会被抛出。
# ------------------------------------------------------------# ------------------------------------------------------------
# # 21、time.strptime
# # # 把一个格式化时间字符串转化为struct_time。实际上它和strftime()是逆操作。
# ------------------------------------------------------------# ------------------------------------------------------------
# # 22、time.asctime()
# # # asctime([t]) : 把一个表示时间的元组或者struct_time表示为这种形式:
# # # 'Sun Jun 20 23:21:05 1993'。
# # # 如果没有参数,将会将time.localtime()作为参数传入。
# ------------------------------------------------------------# ------------------------------------------------------------
# # 23、time.asctime()
# # # 把一个时间戳(按秒计算的浮点数)转化为time.asctime()的形式。如果参数未给或者为
# # # None的时候,将会默认time.time()为参数。它的作用相当于time.asctime(time.localtime(secs))。
# ------------------------------------------------------------# ------------------------------------------------------------
# # 24 、datetime
# # # 获取当前的时间
# ------------------------------------------------------------
# ------------------------------------------------------------
# # 25、关于文件命名
# # # 自己定义的文件,不可与python自己已经自带的库重命,这个原则是与函数的命名是类似的
# # # 一旦自己的文件与python库中的重名,那么就会可能出现python库中的文件模块无法被调用
# ------------------------------------------------------------# ------------------------------------------------------------
# # 26、random() 方法
# # # 返回随机生成的一个实数,它在[0,1)范围内。
# ------------------------------------------------------------
# ------------------------------------------------------------
# # 27、randint() 方法
# # # 语法为: random.randint(a,b)
# # # 函数返回数字 N ,N 为 a 到 b 之间的数字(a <= N <= b),包含 a 和 b。
# ------------------------------------------------------------
# ------------------------------------------------------------
# # 28、randrange() 方法
# # # 语法为: random.randrange ([start,] stop [,step])
# # # 参数
# # # start -- 指定范围内的开始值,包含在范围内。
# # # stop -- 指定范围内的结束值, 不包含 在范围内。
# # # step -- 指定递增基数。
# ------------------------------------------------------------
# ------------------------------------------------------------
# # 29、choice() 方法
# # # choice() 方法,对给定的括号内的内容进行选择,默认抽取一项,
# # # 返回一个列表,元组或字符串的随机项。
# ------------------------------------------------------------
# ------------------------------------------------------------
# # 30、sample() 方法
# # # sample() 方法,对给定的括号内的内容进行选择,根据给定决定抽取的不重复项数
# ------------------------------------------------------------
# ------------------------------------------------------------
# # 31、uniform() 方法
# # # uniform() 方法将随机生成下一个实数,它在 [x, y) 范围内。
# # # 注意:uniform()是不能直接访问的,需要导入 random 模块,然后通过 random 静态对象调用该方法。
# ------------------------------------------------------------
# ------------------------------------------------------------
# # 32、shuffle() 方法
# # # shuffle() 方法将序列的所有元素随机排序。
# # # 注意:uniform()是不能直接访问的,需要导入 random 模块,然后通过 random 静态对象调用该方法。
# ------------------------------------------------------------# ------------------------------------------------------------
# # 33、实例应用:生成验证码
# ------------------------------------------------------------
11
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 # ********************day21-time与random等常用模块与包 ******************* 4 # ********************day21-time与random等常用模块与包 ******************* 5 # ********************day21-time与random等常用模块与包 ******************* 6 ''' 7 # ------------------------------------------------------------ 8 # # 1、import 模块文件 9 # # # 导入整个模块文件 10 # ------------------------------------------------------------ 11 同目录下,文件cal.py,内容: 12 #!/usr/bin/env python 13 # -*- coding:utf-8 -*- 14 15 print("ok1") 16 def add(x,y): 17 return x+y 18 19 print("ok2") 20 21 def sub(x,y): 22 return x-y 23 24 print("ok3") 25 26 ''' 27 28 29 # 30 # import cal 31 # print("add: ",cal.add(3,4)) 32 # print("sub: ",cal.sub(3,4)) 33 # 34 # # D:\Anaconda3\python.exe D:/C_cache/py/day21_time_random_ChangYongMoKuaiYuBao/day21_lesson_package/test.py 35 # # ok1 36 # # ok2 37 # # ok3 38 # # add: 7 39 # # sub: -1 40 # # 41 # # Process finished with exit code 0 42 43 44 45 46 47 ''' 48 # ------------------------------------------------------------ 49 # # 2、from 文件 import 函数名 50 # # # 导文模块文件中所需要用到的函数 51 # ------------------------------------------------------------ 52 # 53 54 55 同目录下,文件cal.py,内容: 56 #!/usr/bin/env python 57 # -*- coding:utf-8 -*- 58 print("ok----->>1") 59 def add(x,y): 60 return x+y 61 print("ok----->>2") 62 def sub(x,y): 63 return x-y 64 print("ok----->>3") 65 66 ''' 67 # from cal import add 68 # from cal import sub 69 # 70 # print("add: ",add(3,4)) 71 # print("sub: ",sub(3,4)) 72 # 73 # # D:\Anaconda3\python.exe D:/C_cache/py/day21_time_random_ChangYongMoKuaiYuBao/day21_lesson_package/test.py 74 # # ok----->>1 75 # # ok----->>2 76 # # ok----->>3 77 # # add: 7 78 # # sub: -1 79 # # 80 # # Process finished with exit code 0 81 82 83 ''' 84 85 # ------------------------------------------------------------ 86 # # 3、from 文件 import * 87 # # # 导文模块文件中 所有 的函数 88 # # # 这种方式是不退荐的,因为会把一个模块文件中的所有函数全部都导入进来,原因: 89 # # # 1、导入多 90 # # # 2、导入的函数名,可能与当前所用的文件中所定义的文件,出现名称重复,导致功能不正常 91 # ------------------------------------------------------------ 92 # 93 # 94 95 同目录下,文件cal.py,内容: 96 #!/usr/bin/env python 97 # -*- coding:utf-8 -*- 98 print("ok----->>1") 99 def add(x,y): 100 return x+y 101 print("ok----->>2") 102 def sub(x,y): 103 return x-y 104 print("ok----->>3") 105 106 ''' 107 108 # from cal import * 109 # 110 # ''' 111 # # 打开这段函数后,将会出现cal中的add,被替代,结果会是17 112 # def add(x,y): 113 # return x+y+10 114 # ''' 115 # print("add: ",add(3,4)) 116 # print("sub: ",sub(3,4)) 117 118 # # D:\Anaconda3\python.exe D:/C_cache/py/day21_time_random_ChangYongMoKuaiYuBao/day21_lesson_package/test.py 119 # # ok----->>1 120 # # ok----->>2 121 # # ok----->>3 122 # # add: 7 123 # # sub: -1 124 # # 125 # # Process finished with exit code 0 126 127 128 ''' 129 130 131 # ------------------------------------------------------------ 132 # # 4、sys.path 133 # # # 执行文件所在的路径 134 # # 模块 import: 135 # 1、执行 对应文件 136 # 2、引入变量名 137 # ------------------------------------------------------------ 138 # 139 ''' 140 # 141 # import sys 142 # print("sys.path:\n",sys.path) 143 # 144 # D:\Anaconda3\python.exe D:/C_cache/py/day21_time_random_ChangYongMoKuaiYuBao/day21_lesson_package/test.py 145 # sys.path: 146 # ['D:\\C_cache\\py\\day21_time_random_ChangYongMoKuaiYuBao\\day21_lesson_package', 147 # 'D:\\C_cache\\py\\day21_time_random_ChangYongMoKuaiYuBao', 148 # 'D:\\Anaconda3\\python36.zip', 149 # 'D:\\Anaconda3\\DLLs', 'D:\\Anaconda3\\lib', 150 # 'D:\\Anaconda3', 'D:\\Anaconda3\\lib\\site-packages', 151 # 'D:\\Anaconda3\\lib\\site-packages\\win32', 152 # 'D:\\Anaconda3\\lib\\site-packages\\win32\\lib', 153 # 'D:\\Anaconda3\\lib\\site-packages\\Pythonwin', 154 # 'D:\\Program Files (x86)\\PyCharm 2018.1.3\\helpers\\pycharm_matplotlib_backend'] 155 # 156 # Process finished with exit code 0 157 158 159 160 ''' 161 162 # ------------------------------------------------------------ 163 # # 5、# from 子目录 import 文件 164 # # # from my_module import cal 165 # # # 从该运行文件中的子集目录中,引入文件 166 # # # 需要注意的是,引入的文件 没有包含其他的引入文件 167 # ------------------------------------------------------------ 168 169 文件目录结结 170 day21_lesson_package\test.py( 该运行文件 ) 171 day21_lesson_package\my_module\cal.py 172 173 ===》》main.py内容cal.py 内容 174 def add(x,y): 175 return x+y 176 177 def sub(x,y): 178 return x-y 179 ''' 180 181 182 # from my_module import cal 183 # 184 # print("add: ",cal.add(3,4)) 185 # print("sub: ",cal.sub(3,4)) 186 # 187 # # D:\Anaconda3\python.exe D:/C_cache/py/day21_time_random_ChangYongMoKuaiYuBao/day21_lesson_package/test.py 188 # # add: 7 189 # # sub: -1 190 # # 191 # # Process finished with exit code 0 192 193 194 195 196 197 ''' 198 199 # ------------------------------------------------------------ 200 # # 6、from 子目录 import 文件1 201 # # # from my_module import main 202 # # # 需要注意的是,引入的文件1中,包含其他的文件2,文件2与文件1 同目录 203 # # # ===>>以下的实例是会报错的! 204 # # # 出错的原因是找不到cal文件 205 # ------------------------------------------------------------ 206 207 208 文件目录结结 209 day21_lesson_package\test.py( 该运行文件 ) 210 day21_lesson_package\my_module\main.py 211 day21_lesson_package\my_module\cal.py 212 213 ===>>>main.py内容 214 import cal 215 def run(): 216 print("here is run function:\n",cal.add(3,5)) 217 def only_main(): 218 print("here is only_main function, not other directory\n") 219 220 ===>>>cal.py 内容 221 def add(x,y): 222 return x+y 223 def sub(x,y): 224 return x-y 225 226 227 ''' 228 229 # from my_module import main 230 # main.only_main() 231 # main.run() 232 233 # # D:\Anaconda3\python.exe D:/C_cache/py/day21_time_random_ChangYongMoKuaiYuBao/day21_lesson_package/test.py 234 # # Traceback (most recent call last): 235 # # File "D:/C_cache/py/day21_time_random_ChangYongMoKuaiYuBao/day21_lesson_package/test.py", line 206, in <module> 236 # # from my_module import main 237 # # File "D:\C_cache\py\day21_time_random_ChangYongMoKuaiYuBao\day21_lesson_package\my_module\main.py", line 3, in <module> 238 # # import cal 239 # # ModuleNotFoundError: No module named 'cal' 240 # # 241 # # Process finished with exit code 1 242 243 244 245 246 ''' 247 # ------------------------------------------------------------ 248 # # 7、from 子目录 import 文件1 249 # # # from my_module import main 250 # # # 需要注意的是,引入的文件1中,包含其他的文件2,文件2与运行文件 同目录 251 # # # ===>>以下的实例是 不会 报错的!注意与上面区别 252 # ------------------------------------------------------------ 253 # # # 254 # 255 256 文件目录结结 257 day21_lesson_package\test.py( 该运行文件 ) 258 day21_lesson_package\my_module\main.py 259 day21_lesson_package\cal.py 260 261 ===>>>main.py内容 262 import cal 263 def run(): 264 print("here is run function:\n",cal.add(3,5)) 265 def only_main(): 266 print("here is only_main function, not other directory\n") 267 268 ===>>>cal.py 内容 269 def add(x,y): 270 return x+y 271 def sub(x,y): 272 return x-y 273 274 275 ''' 276 # 277 # from my_module import main 278 # main.only_main() 279 # main.run() 280 # 281 # # D:\Anaconda3\python.exe D:/C_cache/py/day21_time_random_ChangYongMoKuaiYuBao/day21_lesson_package/test.py 282 # # here is only_main function, not other directory 283 # # 284 # # here is run function: 285 # # 8 286 # # 287 # # Process finished with exit code 0 288 289 290 291 292 ''' 293 # 06模块的执行以及__name__ 294 # 06模块的执行以及__name__ 295 # 06模块的执行以及__name__ 296 # ------------------------------------------------------------ 297 # # 8、包的概念 298 # # # 包的概念:包是用来组织模块的!在pycharm建立的python包中,下面会自动生成一个__init()__文件 299 # # # 调用包就是执行包下的__init__.py文件 300 # # # 引入了包以后,只要顶层的包名不与别人冲突,那所有模块都不会与别人冲突。现在,view.py模块的 301 # # # 名字就变成了hello_django.app01.views,类似的,manage.py的模块名则是hello_django.manage。 302 # ------------------------------------------------------------ 303 ''' 304 305 306 307 308 309 # # 、from 包1.包2.包3 import 被调用文件 310 # # # from web.web1.web3 import cal 311 # # # 从包中引入文件 312 # # 313 # 314 ''' 315 目录结构: 316 当前目录/test.py(执行文件) 317 当前目录/web(包)/web1(包)/web3(包)/cal.py 318 319 ===>>>cal.py内容 320 def add(x,y): 321 return x+y 322 def sub(x,y): 323 return x-y 324 325 ''' 326 # from web.web1.web3 import cal 327 # print(cal.add(2,3)) 328 # 329 # # 330 # # D:\Anaconda3\python.exe D:/C_cache/py/day21_time_random_ChangYongMoKuaiYuBao/day21_lesson_package/test.py 331 # # 5 332 # # 333 # # Process finished with exit code 0 334 335 336 337 338 339 340 341 342 ''' 343 344 # ------------------------------------------------------------ 345 # # 9、from 包1.包2.包3.被调用文件 import 函数名 346 # # # from web.web1.web3.cal import add 347 # # # 从包中引入文件 348 # ------------------------------------------------------------ 349 # # 350 # 351 352 目录结构: 353 当前目录/test.py(执行文件) 354 当前目录/web(包)/web1(包)/web3(包)/cal.py 355 356 ===>>>cal.py内容 357 def add(x,y): 358 return x+y 359 def sub(x,y): 360 return x-y 361 362 ''' 363 # from web.web1.web3.cal import add 364 # print(add(2,3)) 365 # 366 # # 367 # # D:\Anaconda3\python.exe D:/C_cache/py/day21_time_random_ChangYongMoKuaiYuBao/day21_lesson_package/test.py 368 # # 5 369 # # 370 # # Process finished with exit code 0 371 372 373 374 375 376 377 ''' 378 # ------------------------------------------------------------ 379 # # 10、from 包1.包2 import 包3 380 # # # from web.web1 import web3 381 # # # 不修改包3下的文件__init()__,从包中引入文件 382 # # # ===>>>注意:报错,原因是这种方式在不修改包3下的文件__init()__<<<=== 383 # ------------------------------------------------------------ 384 # # 385 # 386 387 目录结构: 388 当前目录/test.py(执行文件) 389 当前目录/web(包)/web1(包)/web3(包)/cal.py 390 391 ===>>>cal.py内容 392 def add(x,y): 393 return x+y 394 def sub(x,y): 395 return x-y 396 397 ''' 398 399 # from web.web1 import web3 400 # print(web3.cal.add(2,3)) 401 # 402 # 403 # # D:\Anaconda3\python.exe D:/C_cache/py/day21_time_random_ChangYongMoKuaiYuBao/day21_lesson_package/test.py 404 # # Traceback (most recent call last): 405 # # File "D:/C_cache/py/day21_time_random_ChangYongMoKuaiYuBao/day21_lesson_package/test.py", line 367, in <module> 406 # # print(web3.cal.add(2,3)) 407 # # AttributeError: module 'web.web1.web3' has no attribute 'cal' 408 # # 409 # # Process finished with exit code 1 410 411 412 413 414 415 416 417 ''' 418 # ------------------------------------------------------------ 419 # # 11、from 包1.包2 import 包3 420 # # # from web.web1 import web3 421 # # # ''修改''包3下的文件__init()__,从包中引入文件,添加内容如下: 422 # # # from . import cal 423 # ------------------------------------------------------------ 424 # # 425 # 426 427 目录结构: 428 当前目录/test.py(执行文件) 429 当前目录/web(包)/web1(包)/web3(包)/cal.py 430 431 432 ===>>>cal.py内容 433 def add(x,y): 434 return x+y 435 def sub(x,y): 436 return x-y 437 438 当前目录/web(包)/web1(包)/web3(包)/__init()__.py 439 ===》__init()__内容: 440 from . import cal 441 442 ''' 443 # 444 # 445 # from web.web1 import web3 446 # print(web3.cal.add(2,3)) 447 # 448 # # D:\Anaconda3\python.exe D:/C_cache/py/day21_time_random_ChangYongMoKuaiYuBao/day21_lesson_package/test.py 449 # # 5 450 # # 451 # # Process finished with exit code 0 452 453 454 455 ''' 456 # ------------------------------------------------------------ 457 # # 12、总结,关于包下的内容调用 458 # # # 推荐使用方式一与方式二 459 # # # 方式三不推荐 460 # # # 方式一:from 包1.包2.包3 import 被调用文件 461 # # # from web.web1.web3 import cal 462 # # # 463 # # # 方式二:from 包1.包2.包3.被调用文件 import 函数名 464 # # # from web.web1.web3.cal import add 465 # # # 466 # # # 方式三:from 包1.包2 import 包3 467 # # # from web.web1 import web3 468 # # # ''修改''包3下的文件__init()__ 469 # ------------------------------------------------------------ 470 ''' 471 472 473 474 475 476 ''' 477 # ------------------------------------------------------------ 478 # # 13、__name__ 479 # # # 注意区别执行文件下的 __name__ 是 __main__,而被调用文件下的__name__是一个路径 480 # ------------------------------------------------------------ 481 482 目录结构: 483 当前目录/test.py(执行文件) 484 当前目录/web(包)/web1(包)/web3(包)/cal.py 485 486 ===>>>cal.py内容 487 print("这个是web3目录下的__name__: ",__name__) # 被调用时,打印是相对于执行文件下的路径 488 def add(x,y): 489 return x+y 490 def sub(x,y): 491 return x-y 492 493 494 ''' 495 # from web.web1.web3 import cal 496 # print('test运行函数下: ',__name__) 497 # print('web.web1.web3 下: ',cal.__name__) 498 # print('web.web1.web3 下add(3,4): ',cal.add(3,4)) 499 # 500 # # D:\Anaconda3\python.exe D:/C_cache/py/day21_time_random_ChangYongMoKuaiYuBao/day21_lesson_package/test.py 501 # # 这个是web3目录下的__name__: web.web1.web3.cal 502 # # test运行函数下: __main__ 503 # # web.web1.web3 下: web.web1.web3.cal 504 # # web.web1.web3 下add(3,4): 7 505 # # 506 # # Process finished with exit code 0 507 508 509 ''' 510 511 # ------------------------------------------------------------ 512 # # 14、if __name__ == "__main__": 513 # # # 只是执行该文件下才会使用 514 # # # 功能一:如果我们是直接执行某个.py文件的时候,该文件中那么”__name__ == '__main__'“是True, 515 # # # 但是我们如果从另外一个.py文件通过import导入该文件的时候,这时__name__的值就是我们这个py文件的名字 516 # # # 而不是__main__。 517 # # # 518 # # # 功能二:调试代码的时候,在”if __name__ == '__main__'“中 519 # # # 加入一些我们的调试代码,我们可以让外部模块调用的时候不执行我们的调试代码,但是如果我们想排查问题 520 # # # 的时候,直接执行该模块文件,调试代码能够正常运行! 521 # ------------------------------------------------------------ 522 523 524 目录结构: 525 当前目录/test.py(执行文件) 526 当前目录/web(包)/web1(包)/web3(包)/cal.py 527 528 ===>>>cal.py内容 529 print("这个是web3目录下的__name__: ",__name__) # 被调用时,打印是相对于执行文件下的路径 530 def add(x,y): 531 return x+y 532 def sub(x,y): 533 return x-y 534 if __name__ == "__main__": # 此处该cal被调用,这里是False,不执行 535 print("这个是web3\\cal文件!") 536 print("sub(5,1): ",sub(5,1)) 537 ''' 538 # if __name__ == "__main__": 539 # from web.web1.web3 import cal 540 # print('web.web1.web3 下add(3,4): ',cal.add(3,4)) 541 # 542 # # D:\Anaconda3\python.exe D:/C_cache/py/day21_time_random_ChangYongMoKuaiYuBao/day21_lesson_package/test.py 543 # # web.web1.web3 下add(3,4): 7 544 # # 545 # # Process finished with exit code 0 546 547 548 549 550 551 552 553 554 # 08 time时间模块 555 # 08 time时间模块 556 # 08 time时间模块 557 ''' 558 # ------------------------------------------------------------ 559 # # 15、sleep 560 # # # Python time sleep() 函数推迟调用线程的运行,可通过参数secs指秒数,表示进程挂起的时间。 561 # ------------------------------------------------------------ 562 563 ''' 564 # 565 # import time 566 # print("start:") 567 # time.sleep(3) 568 # print("end!") 569 # 570 # # D:\Anaconda3\python.exe D:/C_cache/py/day21_time_random_ChangYongMoKuaiYuBao/day21_lesson_package/test.py 571 # # start: 572 # # end: 573 # # 574 # # Process finished with exit code 0 575 576 577 ''' 578 # ------------------------------------------------------------ 579 # # 16、time.time() 580 # # # 时间戳(timestamp) : 581 # # # 打印的时间是从1970.1.1 00:00凌晨开始算,多少秒 582 # # # 时间戳(timestamp) : 通常来说,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量。 583 # # # 我们运行“type(time.time())”,返回的是float类型。 584 # ------------------------------------------------------------ 585 ''' 586 # 587 # import time 588 # print("时间秒:",( (2018-1970)*365*24*60*60) ) 589 # print(time.time() ) # 1533436467.959069秒 590 # 591 # # D:\Anaconda3\python.exe D:/C_cache/py/day21_time_random_ChangYongMoKuaiYuBao/day21_lesson_package/test.py 592 # # 时间秒: 1513728000 593 # # 1533436567.1447423 594 # # 595 # # Process finished with exit code 0 596 597 598 599 600 601 ''' 602 # ------------------------------------------------------------ 603 # # 17、time.localtime() 604 # # # 当前的时间,是以我所在的时区进行计算的,其默认值为time.time() 605 # # # 元组(struct_time) : struct_time元组共有9个元素共九个元素: 606 # # # (年,月,日,时,分,秒,一年中第几周,一年中第几天,夏令时) 607 # # # 608 # # # int tm_sec; /* 秒 – 取值区间为[0,59] */ 609 # # # int tm_min; /* 分 - 取值区间为[0,59] */ 610 # # # int tm_hour; /* 时 - 取值区间为[0,23] */ 611 # # # int tm_mday; /* 一个月中的日期 - 取值区间为[1,31] */ 612 # # # int tm_mon; /* 月份(从一月开始,0代表一月) - 取值区间为[0,11] */ 613 # # # int tm_year; /* 年份,其值等于实际年份减去1900 */ 614 # # # int tm_wday; /* 星期 – 取值区间为[0,6],其中0代表星期天,1代表星期一,以此类推 */ 615 # # # int tm_yday; /* 从每年的1月1日开始的天数 – 取值区间为[0,365],其中0代表1月1日,1代表1月2日,以此类推 */ 616 # # # int tm_isdst; /* 夏令时标识符,实行夏令时的时候,tm_isdst为正。不实行夏令时的时候,tm_isdst为0;不了解情况时,tm_isdst()为负。 617 # ------------------------------------------------------------ 618 ''' 619 # 620 # import time 621 # print(time.localtime()) 622 # print(time.localtime(time.time())) 623 # t = time.localtime() 624 # print(t.tm_year,t.tm_mon,t.tm_mday) 625 # 626 # # D:\Anaconda3\python.exe D:/C_cache/py/day21_time_random_ChangYongMoKuaiYuBao/day21_lesson_package/test.py 627 # # time.struct_time(tm_year=2018, tm_mon=8, tm_mday=5, tm_hour=10, tm_min=53, tm_sec=19, tm_wday=6, tm_yday=217, tm_isdst=0) 628 # # time.struct_time(tm_year=2018, tm_mon=8, tm_mday=5, tm_hour=10, tm_min=53, tm_sec=19, tm_wday=6, tm_yday=217, tm_isdst=0) 629 # # 2018 8 5 630 # # 631 # # Process finished with exit code 0 632 633 634 635 636 637 638 639 640 641 642 ''' 643 644 # ------------------------------------------------------------ 645 # # 18、time.gmtime() 646 # # # gmtime()方法是将一个时间戳转换为UTC时区。其默认值为time.time() 647 # # # 用法与localtime差不多,只是基准的时区不一样 648 # # # 元组(struct_time) : struct_time元组共有9个元素共九个元素: 649 # # # (年,月,日,时,分,秒,一年中第几周,一年中第几天,夏令时) 650 # ------------------------------------------------------------ 651 ''' 652 # 653 # import time 654 # print("time.localtime():\n",time.localtime()) 655 # print("time.gmtime():\n ",time.gmtime()) 656 # print("time.gmtime(time.time()):\n ",time.gmtime(time.time()) ) 657 # 658 # # D:\Anaconda3\python.exe D:/C_cache/py/day21_time_random_ChangYongMoKuaiYuBao/day21_lesson_package/test.py 659 # # time.localtime(): 660 # # time.struct_time(tm_year=2018, tm_mon=8, tm_mday=5, tm_hour=10, tm_min=54, tm_sec=26, tm_wday=6, tm_yday=217, tm_isdst=0) 661 # # time.gmtime(): 662 # # time.struct_time(tm_year=2018, tm_mon=8, tm_mday=5, tm_hour=2, tm_min=54, tm_sec=26, tm_wday=6, tm_yday=217, tm_isdst=0) 663 # # time.gmtime(time.time()): 664 # # time.struct_time(tm_year=2018, tm_mon=8, tm_mday=5, tm_hour=2, tm_min=54, tm_sec=26, tm_wday=6, tm_yday=217, tm_isdst=0) 665 # # 666 # # Process finished with exit code 0 667 668 669 670 671 672 673 ''' 674 # ------------------------------------------------------------ 675 # # 19、time.mktime() 676 # # # 将一个struct_time转化为时间戳。 677 # ------------------------------------------------------------ 678 ''' 679 # 680 # import time 681 # print("time.mktime(time.localtime()):\n",time.mktime(time.localtime())) 682 # 683 # # D:\Anaconda3\python.exe D:/C_cache/py/day21_time_random_ChangYongMoKuaiYuBao/day21_lesson_package/test.py 684 # # time.mktime(time.localtime()): 685 # # 1533437792.0 686 # # 687 # # Process finished with exit code 0 688 689 690 691 692 693 ''' 694 # ------------------------------------------------------------ 695 # # 20、time.strftime() 696 # # # 将一个struct_time转化为时间戳。 697 # # # 把一个代表时间的元组或者struct_time(如由time.localtime()和 698 # # # time.gmtime()返回)转化为格式化的时间字符串。如果t未指定,将传入time.localtime()。 699 # # # 如果元组中任何一个元素越界,ValueError的错误将会被抛出。 700 # ------------------------------------------------------------ 701 ''' 702 # 703 # import time 704 # print("%Y-%m-%d %X ",time.strftime("%Y-%m-%d %X",time.localtime())) 705 # print("%Y-%m-%d %X ",time.strftime("%Y-%m-%d %X")) 706 # print("%Y: %m: %d %X ",time.strftime("%Y: %m: %d %X",time.localtime())) 707 # 708 # # D:\Anaconda3\python.exe D:/C_cache/py/day21_time_random_ChangYongMoKuaiYuBao/day21_lesson_package/test.py 709 # # %Y-%m-%d %X 2018-08-05 11:04:25 710 # # %Y-%m-%d %X 2018-08-05 11:04:25 711 # # %Y: %m: %d %X 2018: 08: 05 11:04:25 712 # # 713 # # Process finished with exit code 0 714 715 716 717 718 ''' 719 # ------------------------------------------------------------ 720 # # 21、time.strptime 721 # # # 把一个格式化时间字符串转化为struct_time。实际上它和strftime()是逆操作。 722 # ------------------------------------------------------------ 723 724 ''' 725 # 726 # import time 727 # print( time.strptime("2018-08-05 11:04:25","%Y-%m-%d %X" ) ) 728 # print( time.strptime("2018-08-05 11:04:25","%Y-%m-%d %X" ) ) 729 # print( time.strptime("2018: 08: 05 11:04:25","%Y: %m: %d %X") ) 730 # 731 # # D:\Anaconda3\python.exe D:/C_cache/py/day21_time_random_ChangYongMoKuaiYuBao/day21_lesson_package/test.py 732 # # time.struct_time(tm_year=2018, tm_mon=8, tm_mday=5, tm_hour=11, tm_min=4, tm_sec=25, tm_wday=6, tm_yday=217, tm_isdst=-1) 733 # # time.struct_time(tm_year=2018, tm_mon=8, tm_mday=5, tm_hour=11, tm_min=4, tm_sec=25, tm_wday=6, tm_yday=217, tm_isdst=-1) 734 # # time.struct_time(tm_year=2018, tm_mon=8, tm_mday=5, tm_hour=11, tm_min=4, tm_sec=25, tm_wday=6, tm_yday=217, tm_isdst=-1) 735 # # 736 # # Process finished with exit code 0 737 # # 738 739 740 741 ''' 742 743 # ------------------------------------------------------------ 744 # # 22、time.asctime() 745 # # # asctime([t]) : 把一个表示时间的元组或者struct_time表示为这种形式: 746 # # # 'Sun Jun 20 23:21:05 1993'。 747 # # # 如果没有参数,将会将time.localtime()作为参数传入。 748 # ------------------------------------------------------------ 749 750 ''' 751 # import time 752 # print(time.asctime()) 753 # print(time.asctime(time.localtime())) 754 # 755 # # D:\Anaconda3\python.exe D:/C_cache/py/day21_time_random_ChangYongMoKuaiYuBao/day21_lesson_package/test.py 756 # # Sun Aug 5 11:14:31 2018 757 # # Sun Aug 5 11:14:31 2018 758 # # 759 # # Process finished with exit code 0 760 761 762 763 764 765 766 767 ''' 768 # ------------------------------------------------------------ 769 # # 23、time.asctime() 770 # # # 把一个时间戳(按秒计算的浮点数)转化为time.asctime()的形式。如果参数未给或者为 771 # # # None的时候,将会默认time.time()为参数。它的作用相当于time.asctime(time.localtime(secs))。 772 # ------------------------------------------------------------ 773 ''' 774 # import time 775 # print(time.ctime()) 776 # print(time.ctime(time.time())) 777 # 778 # D:\Anaconda3\python.exe D:/C_cache/py/day21_time_random_ChangYongMoKuaiYuBao/day21_lesson_package/test.py 779 # Sun Aug 5 11:18:40 2018 780 # Sun Aug 5 11:18:40 2018 781 # Sun Aug 5 11:18:40 2018 782 # 783 # Process finished with exit code 0 784 785 786 787 788 789 790 ''' 791 # ------------------------------------------------------------ 792 # # 24 、datetime 793 # # # 获取当前的时间 794 # ------------------------------------------------------------ 795 ''' 796 # import datetime 797 # print(datetime.datetime.now()) 798 799 # D:\Anaconda3\python.exe D:/C_cache/py/day21_time_random_ChangYongMoKuaiYuBao/day21_lesson_package/test.py 800 # 2018-08-05 11:21:08.873272 801 # 802 # Process finished with exit code 0 803 804 805 806 807 808 # 09 random模块 809 # 09 random模块 810 # 09 random模块 811 ''' 812 813 # ------------------------------------------------------------ 814 # # 25、关于文件命名 815 # # # 自己定义的文件,不可与python自己已经自带的库重命,这个原则是与函数的命名是类似的 816 # # # 一旦自己的文件与python库中的重名,那么就会可能出现python库中的文件模块无法被调用 817 # ------------------------------------------------------------ 818 819 ''' 820 821 822 823 824 ''' 825 # ------------------------------------------------------------ 826 # # 26、random() 方法 827 # # # 返回随机生成的一个实数,它在[0,1)范围内。 828 # ------------------------------------------------------------ 829 ''' 830 # 831 # 832 # import random 833 # ret = random.random() 834 # print(ret) 835 # 836 # # D:\Anaconda3\python.exe D:/C_cache/py/day21_time_random_ChangYongMoKuaiYuBao/day21_lesson_package/test.py 837 # # 0.5055980648694552 838 # # 839 # # Process finished with exit code 0 840 841 842 843 844 845 ''' 846 # ------------------------------------------------------------ 847 # # 27、randint() 方法 848 # # # 语法为: random.randint(a,b) 849 # # # 函数返回数字 N ,N 为 a 到 b 之间的数字(a <= N <= b),包含 a 和 b。 850 # ------------------------------------------------------------ 851 ''' 852 # import random 853 # ret1 = random.randint(1,5) 854 # print(random.randint(1,5),random.randint(1,5),\ 855 # random.randint(1,5),random.randint(1,5), 856 # random.randint(1,5),random.randint(1,5)) 857 # 858 # # D:\Anaconda3\python.exe D:/C_cache/py/day21_time_random_ChangYongMoKuaiYuBao/day21_lesson_package/test.py 859 # # 2 1 3 3 5 1 860 # # 861 # # Process finished with exit code 0 862 863 864 865 866 ''' 867 # ------------------------------------------------------------ 868 # # 28、randrange() 方法 869 # # # 语法为: random.randrange ([start,] stop [,step]) 870 # # # 参数 871 # # # start -- 指定范围内的开始值,包含在范围内。 872 # # # stop -- 指定范围内的结束值, 不包含 在范围内。 873 # # # step -- 指定递增基数。 874 # ------------------------------------------------------------ 875 876 ''' 877 # import random 878 # ret1 = random.randrange(1,5,) 879 # print(random.randrange(1,5),random.randrange(1,5),\ 880 # random.randrange(1,5),random.randrange(1,5), 881 # random.randrange(1,5),random.randrange(1,5)) 882 # 883 # # D:\Anaconda3\python.exe D:/C_cache/py/day21_time_random_ChangYongMoKuaiYuBao/day21_lesson_package/test.py 884 # # 3 4 4 1 4 2 885 # # 886 # # Process finished with exit code 0 887 888 889 890 891 892 893 ''' 894 895 # ------------------------------------------------------------ 896 # # 29、choice() 方法 897 # # # choice() 方法,对给定的括号内的内容进行选择,默认抽取一项, 898 # # # 返回一个列表,元组或字符串的随机项。 899 # ------------------------------------------------------------ 900 901 ''' 902 # import random 903 # print(random.choice([1,'23',[4,5]]))#23 904 # print(random.choice([1,'23',[4,5]]))#23 905 # 906 # # D:\Anaconda3\python.exe D:/C_cache/py/day21_time_random_ChangYongMoKuaiYuBao/day21_lesson_package/test.py 907 # # 23 908 # # [4, 5] 909 # # 910 # # Process finished with exit code 0 911 912 913 914 915 916 ''' 917 # ------------------------------------------------------------ 918 # # 30、sample() 方法 919 # # # sample() 方法,对给定的括号内的内容进行选择,根据给定决定抽取的不重复项数 920 # ------------------------------------------------------------ 921 922 ''' 923 924 # import random 925 # print(random.sample([1,'23',[4,5]],2)) # 抽取2项 926 # print(random.sample([1,'23',[4,5]],1)) # 抽取1项 927 # print(random.sample([1,'23',[4,5]],3)) # 抽取3项 928 # 929 # D:\Anaconda3\python.exe D:/C_cache/py/day21_time_random_ChangYongMoKuaiYuBao/day21_lesson_package/test.py 930 # ['23', [4, 5]] 931 # [1] 932 # ['23', [4, 5], 1] 933 # 934 # Process finished with exit code 0 935 936 937 938 939 940 941 942 943 ''' 944 # ------------------------------------------------------------ 945 # # 31、uniform() 方法 946 # # # uniform() 方法将随机生成下一个实数,它在 [x, y) 范围内。 947 # # # 注意:uniform()是不能直接访问的,需要导入 random 模块,然后通过 random 静态对象调用该方法。 948 # ------------------------------------------------------------ 949 ''' 950 # import random 951 # print(random.uniform(1,3)) 952 # print(random.uniform(1,3)) 953 # print(random.uniform(1,3)) 954 # 955 # # D:\Anaconda3\python.exe D:/C_cache/py/day21_time_random_ChangYongMoKuaiYuBao/day21_lesson_package/test.py 956 # # 2.883114382491064 957 # # 2.873907630119383 958 # # 2.136903515729406 959 # # 960 # # Process finished with exit code 0 961 962 963 964 965 ''' 966 # ------------------------------------------------------------ 967 # # 32、shuffle() 方法 968 # # # shuffle() 方法将序列的所有元素随机排序。 969 # # # 注意:uniform()是不能直接访问的,需要导入 random 模块,然后通过 random 静态对象调用该方法。 970 # ------------------------------------------------------------ 971 972 ''' 973 # import random 974 # item = [1,3,5,4,6,7,2] 975 # random.shuffle(item) 976 # print(item) 977 # 978 # random.shuffle(item) 979 # print(item) 980 # 981 # # D:\Anaconda3\python.exe D:/C_cache/py/day21_time_random_ChangYongMoKuaiYuBao/day21_lesson_package/test.py 982 # # [4, 3, 5, 2, 1, 7, 6] 983 # # [7, 3, 1, 2, 4, 6, 5] 984 # # 985 # # Process finished with exit code 0 986 987 988 989 990 ''' 991 992 # ------------------------------------------------------------ 993 # # 33、实例应用:生成验证码 994 # ------------------------------------------------------------ 995 996 ''' 997 import random 998 def identifying_code(): 999 code ='' 1000 for i in range(1,5): 1001 num = random.randint(0,9) 1002 alf_small = chr( random.randint(65,90)) 1003 alf_large = chr(random.randint(97, 122)) 1004 s = str( random.choice( [num,alf_small,alf_large] ) ) 1005 code += s 1006 return code 1007 print(identifying_code())