zoukankan      html  css  js  c++  java
  • 自定义模块 time datetime random

    1.自定义模块

    import

    导入

    import test       # test是模块名
    test.func()       # func() 是test模块中的函数调用
    

    模块分类

    1.内置模块

    标准库,python解释器自带的.py文件(模块)

    2.第三方模块

    各种大神写的,需要额外下载的(并发编程开始讲解)(pypi)

    3.自定义模块

    自己写的,不需要额外下载

    导入的过程

    1.当前的名称空间中开辟一个新的空间(test)

    2.将模块中所有的代码执行

    3.通过模块名,进行查找函数(工具)

    示例

    test模块
    def func():
        print("这是test文件下的func函数")
    def foo():
        print("这是test文件下的foo函数")
    print(123)
    print(456)
    name = "这是名字"
    
    import test
    print(test.name)          
    print(test.func())    
    # 123
    # 456
    # 这是名字
    # 这是test文件下的func函数
    
    import test
    import test
    import test
    import test
    import test
    import test
    print(test.name)
    # 123
    # 456
    # 这是名字
    

    别名的使用

    import test as t
    print(t.name)
    # 123
    # 456
    # 这是名字
    

    兼容性

    test模块
    def func():
        print("扳手")
        
    meet模块
    def func():
        print("螺丝刀")
    
    msg = """
    1.扳手
    2.螺丝刀
    """
    choose = input(msg)
    if choose == "1":
        import test as t
    elif choose == "2":
        import meet as t
    t.func()
    

    import 和 from的区别

    import test # 把工具箱拿过来
    from test import func   # 把工具拿过来
    

    import的优缺点

    优点: 不会和当前文件定义的变量或者函数发生冲突

    test模块
    name = "alex"
    
    import test
    name = "宝元"
    print(test.name)
    print(name)
    # alex
    # 宝元
    

    缺点: 占用内存比较大

    from的优缺点

    优点: 占用内存比较小

    缺点: 会和当前文件定义的变量或者函数发生冲突

    test模块
    name = "alex"
    
    name = "宝元"
    from test import name
    print(name)
    # alex 下面的变量或者函数会覆盖上面的
    
    解决办法
    test模块
    name = "alex"
    
    name = "宝元"
    from test import name as n
    print(name)
    pirnt(n)
    # 宝元
    # alex
    

    from 模块名 import *

    test模块
    name = "alex"
    def func():
        print("这是test下的func函数")
    
    name = "宝元"
    def func():
        print("123")
    
    from test import *
    print(name)
    func()
    # alex
    # 这是test下的func函数
    # 注意会出现覆盖现象
    

    __all__ = ["要被导入的功能名字"]

    内存地址

    test模块
    name = "alex"
    def func():
        print("这是test下的func函数")
    def foo():
        print("这是test下的foo函数")
    
    from test import foo
    print(foo)
    
    import test
    print(test.foo)
    

    模块的用法

    1.脚本(在cmd中执行 python test.py)

    2.模块(不使用或者导入)

    测试接口

    if __name__ == "__main__"
    在当前模块中使用__name__就是"__main__"
    当模块被导入的时候__name__就是被导入的模块名
    

    路径

    导入路径

    import meet
    print(meet.name)
    

    使用相对路径

    from day15.t1 import meet
    print(meet.name)
    

    使用绝对路径

    from sys import path
    path.insert(0,"D:\")
    import meet
    print(meet.name)
    

    模块的查找顺序

    自定义模块 > 内置模块 > 第三方

    2.time

    time -- 时间(内置模块)

    time的方法

    import time
    时间戳
    print(time.time())
    # 时间戳 浮点数 可计算
    print(time.time() + 5000)
    
    睡眠
    time.sleep(3)
    # 睡眠 以秒为单位
    
    时间节点
    print(time.strftime("%Y-%m-%d %H:%M:%S"))
    # 年-月-日 时:分:秒
    # 2019-07-25 16:43:24
    
    结构化时间
    时间戳转结构化
    print(time.gmtime()) / print(time.localtime())
    # time.struct_time(tm_year=2019, tm_mon=7, tm_mday=25, tm_hour=8,
    # tm_min=46, tm_sec=15, tm_wday=3, tm_yday=206, tm_isdst=0)
    print(time.gmtime()[0]) / print(time.localtime()[0])
    # 2019
    print(time.gmtime().tm_year) / print(time.localtime().tm_year)
    # 2019
    
    字符串时间转结构化
    print(time.strptime("2008-9-1 12:30:30","%Y-%m-%d %H:%M:%S"))
    
    时间戳转换成字符串
    print(time.strftime("%Y-%m-%d %H:%M:%S",time.gmtime(156156641.46854)))
    # 1974-12-13 08:50:41
    
    字符串转结构化转换成时间戳
    print(time.mktime(time.strptime("2008-9-1 12:30:30","%Y-%m-%d %H:%M:%S")))
    # 1220243430.0
    

    3.datetime

    datetime -- 对象

    datetime的方法

    from datetime import datetime
    
    获取当前时间(浮点数)
    print(datetime.now())
    # 2019-07-25 17:24:58.037219
    
    计算时间差距
    print(datetime(2019,5,20,13,14,00) - datetime(2019,5,20,14,20,00))
    # -1 day, 22:54:00
    
    将当前时间转化成时间戳
    print(datetime.now().timestamp())
    # 1564047101.042584
    
    将时间戳转化成当前时间
    print(datetime.fromtimestamp(1564047101.042584))
    # 2019-07-25 17:31:41.042584
    
    将对象转成字符串
    print(str(datetime.now()))
    print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
    # 2019-07-25 17:40:47
    
    时间加减(datetime加减)
    from datetime import datetime,timedelta
    print(datetime.now() + timedelta(hours=30)) # 当前时间加30个小时
    # 2019-07-27 00:19:38.461757
    

    总结:

    用处: 记录日志时使用

    ​ 计算时间

    4.random

    random -- 随机

    random的方法

    import random
    
    0-1随机取值(浮点数)
    print(random.random())
    # 0.04233141839466259
    
    0-10随机取值(浮点数)
    print(random.uniform(1,10))
    # 1.0434503538907838
    
    1-3随机取值(包含1和3)
    print(random.randint(1,3))
    # 1/2/3
    
    randrange(起始,终止,步长)(顾头不顾尾)
    print(random.randrange(1,5,2))
    # 1/3 
    
    随机选择一个元素
    print(random.choice([1,2,3,4,5]))
    # 1/2/3/4/5
    
    随机选择两个元素(会重复)
    print(random.choices([1,2,3,4,5],k=2))
    # 从列表中随机选择两个元素(会有重复元素,比如:[1,1])
    
    随机选择两个元素(不会重复)
    print(random.sample([1,2,3,4,5],k=2))
    # 从列表中随机选择两个元素(不会有重复元素,除非列表只有两个重复元素,比如[1,1])
    
    顺序打乱
    lst = [1,2,3,4,5,6,7,8,9,0]
    random.shuffle(lst)
    print(lst)
    # [4, 3, 9, 5, 1, 2, 8, 7, 6, 0]
    # 随机打乱
    
  • 相关阅读:
    【转】VS2010中 C++创建DLL图解
    [转]error: 'retainCount' is unavailable: not available in automatic reference counting mode
    [转]关于NSAutoreleasePool' is unavailable: not available in automatic reference counting mode的解决方法
    【转】 Tomcat v7.0 Server at localhost was unable to start within 45
    【转】Server Tomcat v7.0 Server at localhost was unable to start within 45 seconds. If
    【转】SVN管理多个项目版本库
    【转】eclipse安装SVN插件的两种方法
    【转】MYSQL启用日志,和查看日志
    【转】Repository has not been enabled to accept revision propchanges
    【转】SVN库的迁移
  • 原文地址:https://www.cnblogs.com/beichen123/p/11253087.html
Copyright © 2011-2022 走看看