zoukankan      html  css  js  c++  java
  • day21-time与random等常用模块与包

     2018-08-05

    # ********************day21-time与random等常用模块与包 *******************

    # 参考资料
    # python模块(转自Yuan先生)
    # https://www.cnblogs.com/wupeiqi/articles/4938499.html


    # =====>>>>>>内容概览
    # =====>>>>>>内容概览
    # =====>>>>>>内容概览

    # ------------------------------------------------------------
    # # 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、实例应用:生成验证码
    # ------------------------------------------------------------

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    # ********************day21-time与random等常用模块与包 *******************
    # ********************day21-time与random等常用模块与包 *******************
    # ********************day21-time与random等常用模块与包 *******************
    '''
    # ------------------------------------------------------------
    # # 1、import 模块文件
    # # # 导入整个模块文件
    # ------------------------------------------------------------
    同目录下,文件cal.py,内容:
    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    
    print("ok1")
    def add(x,y):
        return x+y
    
    print("ok2")
    
    def sub(x,y):
        return x-y
    
    print("ok3")
    
    '''
    
    
    #
    # import cal
    # print("add: ",cal.add(3,4))
    # print("sub: ",cal.sub(3,4))
    #
    # # D:Anaconda3python.exe D:/C_cache/py/day21_time_random_ChangYongMoKuaiYuBao/day21_lesson_package/test.py
    # # ok1
    # # ok2
    # # ok3
    # # add:  7
    # # sub:  -1
    # #
    # # Process finished with exit code 0
    
    
    
    
    
    '''
    # ------------------------------------------------------------
    # # 2、from 文件 import 函数名
    # # # 导文模块文件中所需要用到的函数
    # ------------------------------------------------------------
    
    同目录下,文件cal.py,内容:
    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    print("ok----->>1")
    def add(x,y):
        return x+y
    print("ok----->>2")
    def sub(x,y):
        return x-y
    print("ok----->>3")
    
    '''
    # from cal import add
    # from cal import sub
    #
    # print("add: ",add(3,4))
    # print("sub: ",sub(3,4))
    #
    # # D:Anaconda3python.exe D:/C_cache/py/day21_time_random_ChangYongMoKuaiYuBao/day21_lesson_package/test.py
    # # ok----->>1
    # # ok----->>2
    # # ok----->>3
    # # add:  7
    # # sub:  -1
    # #
    # # Process finished with exit code 0
    
    
    '''
    
    # ------------------------------------------------------------
    # # 3、from 文件 import *
    # # # 导文模块文件中  所有  的函数
    # # # 这种方式是不退荐的,因为会把一个模块文件中的所有函数全部都导入进来,原因:
    # # # 1、导入多
    # # # 2、导入的函数名,可能与当前所用的文件中所定义的文件,出现名称重复,导致功能不正常
    # ------------------------------------------------------------
    #
    #
    
    同目录下,文件cal.py,内容:
    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    print("ok----->>1")
    def add(x,y):
        return x+y
    print("ok----->>2")
    def sub(x,y):
        return x-y
    print("ok----->>3")
    
    '''
    
    # from cal import *
    #
    # '''
    # # 打开这段函数后,将会出现cal中的add,被替代,结果会是17
    # def add(x,y):
    #     return x+y+10
    # '''
    # print("add: ",add(3,4))
    # print("sub: ",sub(3,4))
    
    # # D:Anaconda3python.exe D:/C_cache/py/day21_time_random_ChangYongMoKuaiYuBao/day21_lesson_package/test.py
    # # ok----->>1
    # # ok----->>2
    # # ok----->>3
    # # add:  7
    # # sub:  -1
    # #
    # # Process finished with exit code 0
    
    
    '''
    
    
    # ------------------------------------------------------------
    # # 4、sys.path
    # # # 执行文件所在的路径
    # # 模块 import:
    #             1、执行 对应文件
    #             2、引入变量名
    # ------------------------------------------------------------
    #
    '''
    #
    # import sys
    # print("sys.path:
    ",sys.path)
    #
    # D:Anaconda3python.exe D:/C_cache/py/day21_time_random_ChangYongMoKuaiYuBao/day21_lesson_package/test.py
    # sys.path:
    #  ['D:\C_cache\py\day21_time_random_ChangYongMoKuaiYuBao\day21_lesson_package',
    # 'D:\C_cache\py\day21_time_random_ChangYongMoKuaiYuBao',
    #  'D:\Anaconda3\python36.zip',
    #  'D:\Anaconda3\DLLs', 'D:\Anaconda3\lib',
    #  'D:\Anaconda3', 'D:\Anaconda3\lib\site-packages',
    # 'D:\Anaconda3\lib\site-packages\win32',
    # 'D:\Anaconda3\lib\site-packages\win32\lib',
    #  'D:\Anaconda3\lib\site-packages\Pythonwin',
    # 'D:\Program Files (x86)\PyCharm 2018.1.3\helpers\pycharm_matplotlib_backend']
    #
    # Process finished with exit code 0
    
    
    
    '''
    
    # ------------------------------------------------------------
    # # 5、# from 子目录 import 文件
    # # # from my_module import cal
    # # # 从该运行文件中的子集目录中,引入文件
    # # # 需要注意的是,引入的文件     没有包含其他的引入文件
    # ------------------------------------------------------------
    
    文件目录结结
    day21_lesson_package\test.py( 该运行文件 )
    day21_lesson_package\my_modulecal.py
    
    ===》》main.py内容cal.py   内容
    def add(x,y):
        return x+y
        
    def sub(x,y):
        return x-y
    '''
    
    
    # from my_module import cal
    #
    # print("add: ",cal.add(3,4))
    # print("sub: ",cal.sub(3,4))
    #
    # # D:Anaconda3python.exe D:/C_cache/py/day21_time_random_ChangYongMoKuaiYuBao/day21_lesson_package/test.py
    # # add:  7
    # # sub:  -1
    # #
    # # Process finished with exit code 0
    
    
    
    
    
    '''
    
    # ------------------------------------------------------------
    # # 6、from 子目录 import 文件1
    # # # from my_module import main
    # # # 需要注意的是,引入的文件1中,包含其他的文件2,文件2与文件1  同目录
    # # # ===>>以下的实例是会报错的!
    # # # 出错的原因是找不到cal文件
    # ------------------------------------------------------------
    
    
    文件目录结结
    day21_lesson_package\test.py( 该运行文件 )
    day21_lesson_package\my_modulemain.py
    day21_lesson_package\my_modulecal.py
    
    ===>>>main.py内容
    import cal
    def run():
        print("here is run function:
    ",cal.add(3,5))
    def only_main():
        print("here is only_main function, not other directory
    ")
        
    ===>>>cal.py   内容
    def add(x,y):
        return x+y
    def sub(x,y):
        return x-y
        
    
    '''
    
    # from my_module import main
    # main.only_main()
    # main.run()
    
    # # D:Anaconda3python.exe D:/C_cache/py/day21_time_random_ChangYongMoKuaiYuBao/day21_lesson_package/test.py
    # # Traceback (most recent call last):
    # #   File "D:/C_cache/py/day21_time_random_ChangYongMoKuaiYuBao/day21_lesson_package/test.py", line 206, in <module>
    # #     from my_module import main
    # #   File "D:C_cachepyday21_time_random_ChangYongMoKuaiYuBaoday21_lesson_packagemy_modulemain.py", line 3, in <module>
    # #     import cal
    # # ModuleNotFoundError: No module named 'cal'
    # #
    # # Process finished with exit code 1
    
    
    
    
    '''
    # ------------------------------------------------------------
    # # 7、from 子目录 import 文件1
    # # # from my_module import main
    # # # 需要注意的是,引入的文件1中,包含其他的文件2,文件2与运行文件  同目录
    # # # ===>>以下的实例是  不会  报错的!注意与上面区别
    # ------------------------------------------------------------
    # # #
    #
    
    文件目录结结
    day21_lesson_package\test.py( 该运行文件 )
    day21_lesson_package\my_modulemain.py
    day21_lesson_package\cal.py
    
    ===>>>main.py内容
    import cal
    def run():
        print("here is run function:
    ",cal.add(3,5))
    def only_main():
        print("here is only_main function, not other directory
    ")
    
    ===>>>cal.py   内容
    def add(x,y):
        return x+y
    def sub(x,y):
        return x-y
    
    
    '''
    #
    # from my_module import main
    # main.only_main()
    # main.run()
    #
    # # D:Anaconda3python.exe D:/C_cache/py/day21_time_random_ChangYongMoKuaiYuBao/day21_lesson_package/test.py
    # # here is only_main function, not other directory
    # #
    # # here is run function:
    # #  8
    # #
    # # Process finished with exit code 0
    
    
    
    
    '''
    # 06模块的执行以及__name__
    # 06模块的执行以及__name__
    # 06模块的执行以及__name__
    # ------------------------------------------------------------
    # # 8、包的概念
    # # # 包的概念:包是用来组织模块的!在pycharm建立的python包中,下面会自动生成一个__init()__文件
    # # # 调用包就是执行包下的__init__.py文件
    # # # 引入了包以后,只要顶层的包名不与别人冲突,那所有模块都不会与别人冲突。现在,view.py模块的
    # # # 名字就变成了hello_django.app01.views,类似的,manage.py的模块名则是hello_django.manage。
    # ------------------------------------------------------------
    '''
    
    
    
    
    '''
    # ------------------------------------------------------------
    # # 8.1、from 包1.包2.包3 import 被调用文件
    # # # from web.web1.web3 import cal
    # # # 从包中引入文件
    # ------------------------------------------------------------
    # #
    #
    
    目录结构:
    当前目录/test.py(执行文件)
    当前目录/web(包)/web1(包)/web3(包)/cal.py
    
    ===>>>cal.py内容
    def add(x,y):
        return x+y
    def sub(x,y):
        return x-y
    
    '''
    # from web.web1.web3 import cal
    # print(cal.add(2,3))
    #
    # #
    # # D:Anaconda3python.exe D:/C_cache/py/day21_time_random_ChangYongMoKuaiYuBao/day21_lesson_package/test.py
    # # 5
    # #
    # # Process finished with exit code 0
    
    
    
    
    
    
    
    
    '''
    
    # ------------------------------------------------------------
    # # 9、from 包1.包2.包3.被调用文件 import 函数名
    # # # from web.web1.web3.cal import add
    # # # 从包中引入文件
    # ------------------------------------------------------------
    # #
    #
    
    目录结构:
    当前目录/test.py(执行文件)
    当前目录/web(包)/web1(包)/web3(包)/cal.py
    
    ===>>>cal.py内容
    def add(x,y):
        return x+y
    def sub(x,y):
        return x-y
    
    '''
    # from web.web1.web3.cal import add
    # print(add(2,3))
    #
    # #
    # # D:Anaconda3python.exe D:/C_cache/py/day21_time_random_ChangYongMoKuaiYuBao/day21_lesson_package/test.py
    # # 5
    # #
    # # Process finished with exit code 0
    
    
    
    
    
    
    '''
    # ------------------------------------------------------------
    # # 10、from 包1.包2 import 包3
    # # # from web.web1 import web3
    # # # 不修改包3下的文件__init()__,从包中引入文件
    # # # ===>>>注意:报错,原因是这种方式在不修改包3下的文件__init()__<<<===
    # ------------------------------------------------------------
    # #
    #
    
    目录结构:
    当前目录/test.py(执行文件)
    当前目录/web(包)/web1(包)/web3(包)/cal.py
    
    ===>>>cal.py内容
    def add(x,y):
        return x+y
    def sub(x,y):
        return x-y
    
    '''
    
    # from web.web1 import web3
    # print(web3.cal.add(2,3))
    #
    #
    # # D:Anaconda3python.exe D:/C_cache/py/day21_time_random_ChangYongMoKuaiYuBao/day21_lesson_package/test.py
    # # Traceback (most recent call last):
    # #   File "D:/C_cache/py/day21_time_random_ChangYongMoKuaiYuBao/day21_lesson_package/test.py", line 367, in <module>
    # #     print(web3.cal.add(2,3))
    # # AttributeError: module 'web.web1.web3' has no attribute 'cal'
    # #
    # # Process finished with exit code 1
    
    
    
    
    
    
    
    '''
    # ------------------------------------------------------------
    # # 11、from 包1.包2 import 包3
    # # # from web.web1 import web3
    # # # ''修改''包3下的文件__init()__,从包中引入文件,添加内容如下:
    # # # from . import cal
    # ------------------------------------------------------------
    # #
    #
    
    目录结构:
    当前目录/test.py(执行文件)
    当前目录/web(包)/web1(包)/web3(包)/cal.py
    
    
    ===>>>cal.py内容
    def add(x,y):
        return x+y
    def sub(x,y):
        return x-y
    
    当前目录/web(包)/web1(包)/web3(包)/__init()__.py
    ===》__init()__内容:
    from . import cal
    
    '''
    #
    #
    # from web.web1 import web3
    # print(web3.cal.add(2,3))
    #
    # # D:Anaconda3python.exe D:/C_cache/py/day21_time_random_ChangYongMoKuaiYuBao/day21_lesson_package/test.py
    # # 5
    # #
    # # Process finished with exit code 0
    
    
    
    '''
    # ------------------------------------------------------------
    # # 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__是一个路径
    # ------------------------------------------------------------
    
    目录结构:
    当前目录/test.py(执行文件)
    当前目录/web(包)/web1(包)/web3(包)/cal.py
    
    ===>>>cal.py内容
    print("这个是web3目录下的__name__: ",__name__)  # 被调用时,打印是相对于执行文件下的路径
    def add(x,y):
        return x+y
    def sub(x,y):
        return x-y
    
    
    '''
    # from web.web1.web3 import cal
    # print('test运行函数下:   ',__name__)
    # print('web.web1.web3 下: ',cal.__name__)
    # print('web.web1.web3 下add(3,4): ',cal.add(3,4))
    #
    # # D:Anaconda3python.exe D:/C_cache/py/day21_time_random_ChangYongMoKuaiYuBao/day21_lesson_package/test.py
    # # 这个是web3目录下的__name__:  web.web1.web3.cal
    # # test运行函数下:    __main__
    # # web.web1.web3 下:  web.web1.web3.cal
    # # web.web1.web3 下add(3,4):  7
    # #
    # # Process finished with exit code 0
    
    
    '''
    
    # ------------------------------------------------------------
    # # 14、if __name__ == "__main__":
    # # # 只是执行该文件下才会使用
    # # # 功能一:如果我们是直接执行某个.py文件的时候,该文件中那么”__name__ == '__main__'“是True,
    # # # 但是我们如果从另外一个.py文件通过import导入该文件的时候,这时__name__的值就是我们这个py文件的名字
    # # # 而不是__main__。
    # # # 
    # # # 功能二:调试代码的时候,在”if __name__ == '__main__'“中
    # # # 加入一些我们的调试代码,我们可以让外部模块调用的时候不执行我们的调试代码,但是如果我们想排查问题
    # # # 的时候,直接执行该模块文件,调试代码能够正常运行!
    # ------------------------------------------------------------
    
    
    目录结构:
    当前目录/test.py(执行文件)
    当前目录/web(包)/web1(包)/web3(包)/cal.py
    
    ===>>>cal.py内容
    print("这个是web3目录下的__name__: ",__name__)  # 被调用时,打印是相对于执行文件下的路径
    def add(x,y):
        return x+y
    def sub(x,y):
        return x-y
    if __name__ == "__main__":              # 此处该cal被调用,这里是False,不执行
        print("这个是web3\cal文件!")
        print("sub(5,1): ",sub(5,1))
    '''
    # if __name__ == "__main__":
    #     from web.web1.web3 import cal
    #     print('web.web1.web3 下add(3,4): ',cal.add(3,4))
    #
    # # D:Anaconda3python.exe D:/C_cache/py/day21_time_random_ChangYongMoKuaiYuBao/day21_lesson_package/test.py
    # # web.web1.web3 下add(3,4):  7
    # #
    # # Process finished with exit code 0
    
    
    
    
    
    
    
    
    # 08 time时间模块
    # 08 time时间模块
    # 08 time时间模块
    '''
    # ------------------------------------------------------------
    # # 15、sleep
    # # # Python time sleep() 函数推迟调用线程的运行,可通过参数secs指秒数,表示进程挂起的时间。
    # ------------------------------------------------------------
    
    '''
    #
    # import time
    # print("start:")
    # time.sleep(3)
    # print("end!")
    #
    # # D:Anaconda3python.exe D:/C_cache/py/day21_time_random_ChangYongMoKuaiYuBao/day21_lesson_package/test.py
    # # start:
    # # end:
    # #
    # # Process finished with exit code 0
    
    
    '''
    # ------------------------------------------------------------
    # # 16、time.time()
    # # # 时间戳(timestamp) :
    # # # 打印的时间是从1970.1.1     00:00凌晨开始算,多少秒
    # # # 时间戳(timestamp) :         通常来说,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量。
    # # # 我们运行“type(time.time())”,返回的是float类型。
    # ------------------------------------------------------------
    '''
    #
    # import time
    # print("时间秒:",( (2018-1970)*365*24*60*60)  )
    # print(time.time() )     # 1533436467.959069秒
    #
    # # D:Anaconda3python.exe D:/C_cache/py/day21_time_random_ChangYongMoKuaiYuBao/day21_lesson_package/test.py
    # # 时间秒: 1513728000
    # # 1533436567.1447423
    # #
    # # Process finished with exit code 0
    
    
    
    
    
    '''
    # ------------------------------------------------------------
    # # 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()为负。
    # ------------------------------------------------------------
    '''
    #
    # import time
    # print(time.localtime())
    # print(time.localtime(time.time()))
    # t = time.localtime()
    # print(t.tm_year,t.tm_mon,t.tm_mday)
    #
    # # D:Anaconda3python.exe D:/C_cache/py/day21_time_random_ChangYongMoKuaiYuBao/day21_lesson_package/test.py
    # # 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)
    # # 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)
    # # 2018 8 5
    # #
    # # Process finished with exit code 0
    
    
    
    
    
    
    
    
    
    
    '''
    
    # ------------------------------------------------------------
    # # 18、time.gmtime()
    # # # gmtime()方法是将一个时间戳转换为UTC时区。其默认值为time.time()
    # # # 用法与localtime差不多,只是基准的时区不一样
    # # # 元组(struct_time)   :         struct_time元组共有9个元素共九个元素:
    # # # (年,月,日,时,分,秒,一年中第几周,一年中第几天,夏令时)
    # ------------------------------------------------------------
    '''
    #
    # import time
    # print("time.localtime():
    ",time.localtime())
    # print("time.gmtime():
     ",time.gmtime())
    # print("time.gmtime(time.time()):
     ",time.gmtime(time.time()) )
    #
    # # D:Anaconda3python.exe D:/C_cache/py/day21_time_random_ChangYongMoKuaiYuBao/day21_lesson_package/test.py
    # # time.localtime():
    # #  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)
    # # time.gmtime():
    # #   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)
    # # time.gmtime(time.time()):
    # #   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)
    # #
    # # Process finished with exit code 0
    
    
    
    
    
    
    '''
    # ------------------------------------------------------------
    # # 19、time.mktime()
    # # # 将一个struct_time转化为时间戳。
    # ------------------------------------------------------------
    '''
    #
    # import time
    # print("time.mktime(time.localtime()):
    ",time.mktime(time.localtime()))
    #
    # # D:Anaconda3python.exe D:/C_cache/py/day21_time_random_ChangYongMoKuaiYuBao/day21_lesson_package/test.py
    # # time.mktime(time.localtime()):
    # #  1533437792.0
    # #
    # # Process finished with exit code 0
    
    
    
    
    
    '''
    # ------------------------------------------------------------
    # # 20、time.strftime()
    # # # 把一个代表时间的元组或者struct_time(如由time.localtime()和
    # # # time.gmtime()返回)转化为格式化的时间字符串。如果t未指定,将传入time.localtime()。
    # # # 如果元组中任何一个元素越界,ValueError的错误将会被抛出。
    # ------------------------------------------------------------
    '''
    #
    # import time
    # print("%Y-%m-%d %X        ",time.strftime("%Y-%m-%d %X",time.localtime()))
    # print("%Y-%m-%d %X        ",time.strftime("%Y-%m-%d %X"))
    # print("%Y:  %m:  %d   %X  ",time.strftime("%Y:  %m:  %d   %X",time.localtime()))
    #
    # # D:Anaconda3python.exe D:/C_cache/py/day21_time_random_ChangYongMoKuaiYuBao/day21_lesson_package/test.py
    # # %Y-%m-%d %X          2018-08-05 11:04:25
    # # %Y-%m-%d %X          2018-08-05 11:04:25
    # # %Y:  %m:  %d   %X    2018:  08:  05   11:04:25
    # #
    # # Process finished with exit code 0
    
    
    
    
    '''
    # ------------------------------------------------------------
    # # 21、time.strptime
    # # # 把一个格式化时间字符串转化为struct_time。实际上它和strftime()是逆操作。
    # ------------------------------------------------------------
    
    '''
    #
    # import time
    # print( time.strptime("2018-08-05 11:04:25","%Y-%m-%d %X" ) )
    # print( time.strptime("2018-08-05 11:04:25","%Y-%m-%d %X" ) )
    # print( time.strptime("2018:  08:  05   11:04:25","%Y:  %m:  %d   %X") )
    #
    # # D:Anaconda3python.exe D:/C_cache/py/day21_time_random_ChangYongMoKuaiYuBao/day21_lesson_package/test.py
    # # 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)
    # # 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)
    # # 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)
    # #
    # # Process finished with exit code 0
    # #
    
    
    
    '''
    
    # ------------------------------------------------------------
    # # 22、time.asctime()
    # # # asctime([t]) : 把一个表示时间的元组或者struct_time表示为这种形式:
    # # # 'Sun Jun 20 23:21:05 1993'。
    # # # 如果没有参数,将会将time.localtime()作为参数传入。
    # ------------------------------------------------------------
    
    '''
    # import time
    # print(time.asctime())
    # print(time.asctime(time.localtime()))
    #
    # # D:Anaconda3python.exe D:/C_cache/py/day21_time_random_ChangYongMoKuaiYuBao/day21_lesson_package/test.py
    # # Sun Aug  5 11:14:31 2018
    # # Sun Aug  5 11:14:31 2018
    # #
    # # Process finished with exit code 0
    
    
    
    
    
    
    
    '''
    # ------------------------------------------------------------
    # # 23、time.ctime()
    # # # 把一个时间戳(按秒计算的浮点数)转化为time.asctime()的形式。如果参数未给或者为
    # # # None的时候,将会默认time.time()为参数。它的作用相当于time.asctime(time.localtime(secs))。
    # ------------------------------------------------------------
    '''
    # import time
    # print(time.ctime())
    # print(time.ctime(time.time()))
    #
    # D:Anaconda3python.exe D:/C_cache/py/day21_time_random_ChangYongMoKuaiYuBao/day21_lesson_package/test.py
    # Sun Aug  5 11:18:40 2018
    # Sun Aug  5 11:18:40 2018
    # Sun Aug  5 11:18:40 2018
    #
    # Process finished with exit code 0
    
    
    
    
    
    
    '''
    # ------------------------------------------------------------
    # # 24 、datetime
    # # # 获取当前的时间
    # ------------------------------------------------------------
    '''
    # import datetime
    # print(datetime.datetime.now())
    
    # D:Anaconda3python.exe D:/C_cache/py/day21_time_random_ChangYongMoKuaiYuBao/day21_lesson_package/test.py
    # 2018-08-05 11:21:08.873272
    #
    # Process finished with exit code 0
    
    
    
    
    
    # 09 random模块
    # 09 random模块
    # 09 random模块
    '''
    
    # ------------------------------------------------------------
    # # 25、关于文件命名
    # # # 自己定义的文件,不可与python自己已经自带的库重命,这个原则是与函数的命名是类似的
    # # # 一旦自己的文件与python库中的重名,那么就会可能出现python库中的文件模块无法被调用
    # ------------------------------------------------------------
    
    '''
    
    
    
    
    '''
    # ------------------------------------------------------------
    # # 26、random() 方法
    # # # 返回随机生成的一个实数,它在[0,1)范围内。
    # ------------------------------------------------------------
    '''
    #
    #
    # import random
    # ret = random.random()
    # print(ret)
    #
    # # D:Anaconda3python.exe D:/C_cache/py/day21_time_random_ChangYongMoKuaiYuBao/day21_lesson_package/test.py
    # # 0.5055980648694552
    # #
    # # Process finished with exit code 0
    
    
    
    
    
    '''
    # ------------------------------------------------------------
    # # 27、randint() 方法
    # # # 语法为:      random.randint(a,b)
    # # # 函数返回数字 N ,N 为 a 到 b 之间的数字(a <= N <= b),包含 a 和 b。
    # ------------------------------------------------------------
    '''
    # import random
    # ret1 = random.randint(1,5)
    # print(random.randint(1,5),random.randint(1,5),
    #       random.randint(1,5),random.randint(1,5),
    #       random.randint(1,5),random.randint(1,5))
    #
    # # D:Anaconda3python.exe D:/C_cache/py/day21_time_random_ChangYongMoKuaiYuBao/day21_lesson_package/test.py
    # # 2 1 3 3 5 1
    # #
    # # Process finished with exit code 0
    
    
    
    
    '''
    # ------------------------------------------------------------
    # # 28、randrange() 方法
    # # # 语法为:      random.randrange ([start,] stop [,step])
    # # # 参数
    # # # start -- 指定范围内的开始值,包含在范围内。
    # # # stop -- 指定范围内的结束值, 不包含  在范围内。
    # # # step -- 指定递增基数。
    # ------------------------------------------------------------
    
    '''
    # import random
    # ret1 = random.randrange(1,5,)
    # print(random.randrange(1,5),random.randrange(1,5),
    #       random.randrange(1,5),random.randrange(1,5),
    #       random.randrange(1,5),random.randrange(1,5))
    #
    # # D:Anaconda3python.exe D:/C_cache/py/day21_time_random_ChangYongMoKuaiYuBao/day21_lesson_package/test.py
    # # 3 4 4 1 4 2
    # #
    # # Process finished with exit code 0
    
    
    
    
    
    
    '''
    
    # ------------------------------------------------------------
    # # 29、choice() 方法
    # # # choice() 方法,对给定的括号内的内容进行选择,默认抽取一项,
    # # # 返回一个列表,元组或字符串的随机项。
    # # # random.choice(内容),内容可以是字符串,列表,但是不可以是集合、元组
    # ------------------------------------------------------------
    
    '''
    # import random
    # print(random.choice([1,'23',[4,5]]))#23
    # print(random.choice([1,'23',[4,5]]))#23
    # print( "choice('A String') : ", random.choice('A String') )
    
    # D:Anaconda3python.exe D:/C_cache/py/day21_time_random_ChangYongMoKuaiYuBao/day21_lesson_package/test.py
    # 23
    # 23
    # choice('A String') :  A
    #
    # Process finished with exit code 0
    
    
    
    
    
    '''
    # ------------------------------------------------------------
    # # 30、sample() 方法
    # # # sample() 方法,对给定的括号内的内容进行选择,根据给定决定抽取的不重复项数
    # ------------------------------------------------------------
    
    '''
    
    # import random
    # print(random.sample([1,'23',[4,5]],2))      # 抽取2项
    # print(random.sample([1,'23',[4,5]],1))      # 抽取1项
    # print(random.sample([1,'23',[4,5]],3))      # 抽取3项
    #
    # D:Anaconda3python.exe D:/C_cache/py/day21_time_random_ChangYongMoKuaiYuBao/day21_lesson_package/test.py
    # ['23', [4, 5]]
    # [1]
    # ['23', [4, 5], 1]
    #
    # Process finished with exit code 0
    
    
    
    
    
    
    
    
    '''
    # ------------------------------------------------------------
    # # 31、uniform() 方法
    # # # uniform() 方法将随机生成下一个实数,它在 [x, y) 范围内。
    # # # 注意:uniform()是不能直接访问的,需要导入 random 模块,然后通过 random 静态对象调用该方法。
    # ------------------------------------------------------------
    '''
    # import random
    # print(random.uniform(1,3))
    # print(random.uniform(1,3))
    # print(random.uniform(1,3))
    #
    # # D:Anaconda3python.exe D:/C_cache/py/day21_time_random_ChangYongMoKuaiYuBao/day21_lesson_package/test.py
    # # 2.883114382491064
    # # 2.873907630119383
    # # 2.136903515729406
    # #
    # # Process finished with exit code 0
    
    
    
    
    '''
    # ------------------------------------------------------------
    # # 32、shuffle() 方法
    # # # shuffle() 方法将序列的所有元素随机排序。
    # # # 注意:uniform()是不能直接访问的,需要导入 random 模块,然后通过 random 静态对象调用该方法。
    # ------------------------------------------------------------
    
    '''
    # import random
    # item = [1,3,5,4,6,7,2]
    # random.shuffle(item)
    # print(item)
    #
    # random.shuffle(item)
    # print(item)
    #
    # # D:Anaconda3python.exe D:/C_cache/py/day21_time_random_ChangYongMoKuaiYuBao/day21_lesson_package/test.py
    # # [4, 3, 5, 2, 1, 7, 6]
    # # [7, 3, 1, 2, 4, 6, 5]
    # #
    # # Process finished with exit code 0
    
    
    
    
    '''
    
    # ------------------------------------------------------------
    # # 33、实例应用:生成验证码
    # ------------------------------------------------------------
    
    '''
    
    #
    # import random
    # def identifying_code():
    #     code =''
    #     for i in range(1,5):
    #         num = random.randint(0,9)
    #         alf_small = chr( random.randint(65,90))
    #         alf_large = chr(random.randint(97, 122))
    #         s = str( random.choice( [num,alf_small,alf_large] )  )
    #         code += s
    #     return code
    # print(identifying_code())
  • 相关阅读:
    86. Partition List
    328. Odd Even Linked List
    19. Remove Nth Node From End of List(移除倒数第N的结点, 快慢指针)
    24. Swap Nodes in Pairs
    2. Add Two Numbers(2个链表相加)
    92. Reverse Linked List II(链表部分反转)
    109. Convert Sorted List to Binary Search Tree
    138. Copy List with Random Pointer
    为Unity的新版ugui的Prefab生成预览图
    ArcEngine生成矩形缓冲区
  • 原文地址:https://www.cnblogs.com/jyfootprint/p/9426117.html
Copyright © 2011-2022 走看看