• Python学习day5


    一、模块

    1. 模块定义、用途

        模块就是具有一定功能的程序块,实质上模块就是.py格式的文件,每个文件就是一个模块。模块可以把复杂的程序按功能分开,分别用不同的文件名存放。目的是使程序代码能够重用,也使得程序更便于维护。

        Python模块分为三类:(1)内置模块;(2)第三方模块;(3)自定义模块。

    2. 模块的导入

        模块使用前需要先导入,导入的方式常用的有:(1)import + [module name],例如要导入os模块时使用 import os;  (2)from [module name] import [function name].

         有些模块的名称很长,可以导入时,给它重新起个简单的名字:import [module name ] as other name.

    3.  包的引用

         Python的文件有一种组织,就是将几个功能相近的模块可组成一个python包,存放到一个目录结构,通过输入包的路径调用包中模块的变量、函数等。每个包下面都有个初始化文件__init__.py,该文件可以为空,也可以定义相关代码。包的引用有三种方式:(1)import [package name]只能使用初始化文件对象;(2)imoport [package name].[module name]可以使用引用模块的对象;(3) from [package name] import [function name]与第二种方式相同,可以使用引用模块的对象。

    4. 常用内置模块:

       (1)os 模块

    获取当前文件的绝对路径

    1 import os
    2 base_path=os.path.abspath(__file__)
    3 print(base_path)

    获取当前文件的父级目录绝对路径

    import os
    base_path=os.path.dirname(os.path.abspath(__file__))
    print(base_path)

          (2)sys模块包含了与python解释器和它的环境有关的函数,这个你可以通过dir(sys)来查看他里面的方法和成员属性

    1 import sys
    2 print(dir(sys))

    运行结果:

    ['__displayhook__', '__doc__', '__excepthook__', '__interactivehook__', '__loader__', '__name__', '__package__', '__spec__', '__stderr__', '__stdin__', '__stdout__', '_clear_type_cache', '_current_frames', '_debugmallocstats', '_getframe', '_home', '_mercurial', '_xoptions', 'api_version', 'argv', 'base_exec_prefix', 'base_prefix', 'builtin_module_names', 'byteorder', 'call_tracing', 'callstats', 'copyright', 'displayhook', 'dllhandle', 'dont_write_bytecode', 'exc_info', 'excepthook', 'exec_prefix', 'executable', 'exit', 'flags', 'float_info', 'float_repr_style', 'get_coroutine_wrapper', 'getallocatedblocks', 'getcheckinterval', 'getdefaultencoding', 'getfilesystemencoding', 'getprofile', 'getrecursionlimit', 'getrefcount', 'getsizeof', 'getswitchinterval', 'gettrace', 'getwindowsversion', 'hash_info', 'hexversion', 'implementation', 'int_info', 'intern', 'is_finalizing', 'maxsize', 'maxunicode', 'meta_path', 'modules', 'path', 'path_hooks', 'path_importer_cache', 'platform', 'prefix', 'set_coroutine_wrapper', 'setcheckinterval', 'setprofile', 'setrecursionlimit', 'setswitchinterval', 'settrace', 'stderr', 'stdin', 'stdout', 'thread_info', 'version', 'version_info', 'warnoptions', 'winver']
    View Code

     sys.path是一个list,默认情况下python导入文件或者模块的话,他会先在sys.path里找模块的路径。如果没有的话,

    程序就会报错。所以我们一般自己写程序的话,最好把自己的模块路径给加到当前模块扫描的路径里。

    1 import os,sys
    2 base_path=os.path.dirname(os.path.abspath(__file__))
    3 sys.path.append(base_path)

          (3)time模块

           在Python中时间有三种表示方式:

    (1)时间戳:时间戳就是从1970年1月1日00:00:00开始按秒计算的偏移量。;

    (2)格式化的时间字符串;

    (3)时间元组:包含一个时刻各种状态的元组, 包括:年,月,日,时,分,秒,第几周,第几天,夏令时标示。。

    •  返回当前时间的时间戳
    1 import time
    2 print(time.time())

    运行结果:

    1471843632.2690241
    •  将一个时间戳转变为当地时区的时间元组time.localtime([secs]),secs为以秒为单位的参数,当不提供参数时,取本地时间。
    import time
    print(time.localtime())

    运行结果:

    time.struct_time(tm_year=2016, tm_mon=8, tm_mday=22, tm_hour=13, tm_min=35, tm_sec=5, tm_wday=0, tm_yday=235, tm_isdst=0)

    我们可以根据偏移找到任何一个需要的量,比如今天是今年的第几天。 程序如下:

    import time
    print(time.localtime()[7])
    • time.gmtime([secs]):和localtime()方法类似,gmtime()方法是将一个时间戳转换为UTC时区(0时区)的struct_time。
    import time
    print(time.gmtime())

    运行结果:

    ime.struct_time(tm_year=2016, tm_mon=8, tm_mday=22, tm_hour=5, tm_min=39, tm_sec=44, tm_wday=0, tm_yday=235, tm_isdst=0)
    • 有把时间戳变成时间元组的函数,那么就有把时间元组变为时间戳的函数time.mktime(t)t是一个有九个元素的元组
    import time
    t=(2016,8,22,13,35,5,0,235,0)
    print(time.mktime(t))

    运行结果:

    1471844105.0
    • time.sleep(secs):线程推迟指定的时间运行。单位为秒。

     strftime() 函数接收以时间元组,并返回以可读字符串表示的当地时间,格式由参数format决定,time.strftime(format[, t])

    1 import time
    2 
    3 strtime = time.strftime("%Y/%m/%d %H:%M:%S",)
    4 print(strtime)
    5 print(type(strtime))

    运行结果:

    2016/08/26 11:31:08
    <class 'str'>
    • time strptime() 函数根据指定的格式把一个时间字符串解析为时间元组。
      1 import time
      2 
      3 strtime = time.strftime("%Y/%m/%d %H:%M:%S",)
      4 print(strtime)
      5 sptime=time.strptime(strtime,"%Y/%m/%d %H:%M:%S")
      6 print(sptime)

      运行结果:

      2016/08/26 11:40:16
      time.struct_time(tm_year=2016, tm_mon=8, tm_mday=26, tm_hour=11, tm_min=40, tm_sec=16, tm_wday=4, tm_yday=239, tm_isdst=-1)

     

  • 相关阅读:
    智能视频分析
    基于libuv的TCP设计(三)
    视频质量诊断----画面抖动检测
    视频质量诊断----画面冻结检测
    视频质量诊断----PTZ云台运动检测
    视频质量诊断----模糊检测
    视频质量诊断----信号丢失检测
    视频质量诊断----遮挡检测
    视频质量诊断----条纹噪声检测
    视频质量诊断----雪花噪声检测
  • 原文地址:https://www.cnblogs.com/iclq/p/5793257.html
Copyright © 2020-2023  润新知