• 模块


    1、time & datatime

    #_*_coding:utf-8_*_
    import time
    import datetime
     
    print(time.clock()) #返回处理器时间,3.3开始已废弃
    print(time.process_time()) #返回处理器时间,3.3开始已废弃
    print(time.time()) #返回当前系统时间戳
    print(time.ctime()) #输出Tue Jan 26 18:23:48 2016 ,当前系统时间
    print(time.ctime(time.time()-86640)) #将时间戳转为字符串格式
    print(time.gmtime(time.time()-86640)) #将时间戳转换成struct_time格式
    print(time.localtime(time.time()-86640)) #将时间戳转换成struct_time格式,但返回 的本地时间
    print(time.mktime(time.localtime())) #与time.localtime()功能相反,将struct_time格式转回成时间戳格式
    #time.sleep(4) #sleep
    print(time.strftime("%Y-%m-%d %H:%M:%S",time.gmtime()) ) #将struct_time格式转成指定的字符串格式
    print(time.strptime("2016-01-28","%Y-%m-%d") ) #将字符串格式转换成struct_time格式
     
    #datetime module
     
    print(datetime.date.today()) #输出格式 2016-01-26
    print(datetime.date.fromtimestamp(time.time()-864400) ) #2016-01-16 将时间戳转成日期格式
    current_time = datetime.datetime.now() #
    print(current_time) #输出2016-01-26 19:04:30.335935
    print(current_time.timetuple()) #返回struct_time格式
     
    #datetime.replace([year[, month[, day[, hour[, minute[, second[, microsecond[, tzinfo]]]]]]]])
    print(current_time.replace(2014,9,12)) #输出2014-09-12 19:06:24.074900,返回当前时间,但指定的值将被替换
     
    str_to_date = datetime.datetime.strptime("21/11/06 16:30", "%d/%m/%y %H:%M") #将字符串转换成日期格式
    new_date = datetime.datetime.now() + datetime.timedelta(days=10) #比现在加10天
    new_date = datetime.datetime.now() + datetime.timedelta(days=-10) #比现在减10天
    new_date = datetime.datetime.now() + datetime.timedelta(hours=-10) #比现在减10小时
    new_date = datetime.datetime.now() + datetime.timedelta(seconds=120) #比现在+120s
    print(new_date)
    

    time.time()  #返回当前系统的时间戳(时间戳是指格林威治时间1970年01月01日00时00分00秒(北京时间1970年01月01日08时00分00秒)起至现在的总秒数)

    time.ctime() #返回当前时间(年 月 日 时间)

    time.ctime(time.time()) #时间戳转为字符串格式

    time.gmtime(time.time()) #将时间戳转换成struct_time格式

    2、sys

    sys.argv  #捕捉传入的参数(会捕捉传入脚本的参数并生成一个列表,第一个元素是程序本身路径,第二个元素才是传入的第一个参数,往脚本中传参数都是在cmd中进行)

    sys.path  # 返回模块的搜索路径,初始化时使用PYTHONPATH环境变量的值

    大家在导入模块的时候用到import,模块到底是从什么地方导入的呢?利用sys.pash就可以查看python内部在导入过程中都到哪些地方去找了

    下面的地址就是在导入的过程中python找的地方
    C:UserslenovoPycharmProjects老男孩 C:UserslenovoPycharmProjects老男孩 E:Python35python35.zip E:Python35DLLs E:Python35lib               #内置模块存放地址 E:Python35 E:Python35libsite-packages      #第三方下载的模块存放地址

     大家写好的模块也可以放到下面的某一个目录中,以便可以实现全局调用,当然也可以添加自己的模块路径利用sys.pash.append(路径)

    如果想让自己的模块全局执行,则把你的模块放lib目录下

    sys.exit(n)  #退出程序,正常退出时exit(0)

    import sys
    a=input("退出?:")
    if a=="y":
        sys.exit("goodboy")
         #或exit(goodboy)
    

    sys.version    #获取Python解释程序的版本信息

    sys.platform  #返回操作系统平台名称

    安装模块

    例:为python27下安装django模块

    1、在cmd中进行安装,先切换到根目录下

    2、用cd命令切换到python27下

    3、用dir命令查看python下有没有Scripts

    4、用cd命令切换到Scripts下

    5、利用pip.exe install djanjo进行安装

    6、想卸载的话利用pip uninstall django

    sys.stdout.write()   #在屏幕上输出,而且不换行,与print不同(输出后换行)

    import sys,time
    for i in range(12):
        time.sleep(1)
        sys.stdout.write("@")
    @@@@@@@@@@@@
    

     进度条

    import sys,time
    for i in range(30):
        sys.stdout.write("
    ")        #清空上一次的内容
        sys.stdout.write("%s %%| %s" % (int((i+1)/30*100),i*"*"))   # %%输出一个%
        sys.stdout.flush()          #强制刷新
        time.sleep(0.3)
    100%| *****************************
    

     对文件操作flush的理解

    一般的文件流操作都包含缓冲机制,write方法并不直接将数据写入文件,而是先写入内存中特定的缓冲区。

    flush方法是用来刷新缓冲区的,即将缓冲区中的数据立刻写入文件,同时清空缓冲区。

    正常情况下缓冲区满时,操作系统会自动将缓冲数据写入到文件中。

    至于close方法,原理是内部先调用flush方法来刷新缓冲区,再执行关闭操作,这样即使缓冲区数据未满也能保证数据的完整性。

    如果进程意外退出或正常退出时而未执行文件的close方法,缓冲区中的内容将会丢失

    3、hashlib

    用于加密相关的操作,代替了md5模块和sha模块,主要提供 SHA1, SHA224, SHA256, SHA384, SHA512 ,MD5 算法

    import hashlib
    
    # ######## md5 ########
    
    hash = hashlib.md5()
    
    # help(hash.update)
    
    hash.update(bytes('admin', encoding='utf-8'))
    
    print(hash.hexdigest())
    
    print(hash.digest()) 
    
    ######## sha1 ########
    
    hash = hashlib.sha1()
    
    hash.update(bytes('admin', encoding='utf-8'))
    
    print(hash.hexdigest())
    
    
    # ######## sha256 ########
    
    hash = hashlib.sha256()
    
    hash.update(bytes('admin', encoding='utf-8'))
    
    print(hash.hexdigest())
    
    # ######## sha384 #######
    
    hash = hashlib.sha384()
    
    hash.update(bytes('admin', encoding='utf-8'))
    
    print(hash.hexdigest())
    
    # ######## sha512 ########
    
    hash = hashlib.sha512()
    
    hash.update(bytes('admin', encoding='utf-8'))
    
    print(hash.hexdigest()) 
    

     以上加密算法虽然依然非常厉害,但时候存在缺陷,即:通过撞库可以反解。所以,有必要对加密算法中添加自定义key再来做加密。

    import hashlib
     
    # ######## md5 ########
     
    hash = hashlib.md5(bytes('898oaFs09f',encoding="utf-8"))
    hash.update(bytes('admin',encoding="utf-8"))
    print(hash.hexdigest())
    

     python内置还有一个 hmac 模块,它内部对我们创建 key 和 内容 进行进一步的处理然后再加密

    import hmac
     
    h = hmac.new(bytes('898oaFs09f',encoding="utf-8"))
    h.update(bytes('admin',encoding="utf-8"))
    print(h.hexdigest())
    

     4、logging

    用于便捷记录日志且线程安全的模块

    只有大于当前日志等级的操作才会被记录

    import logging
    logging.basicConfig(filename='log.log',
                        format='%(asctime)s - %(name)s - %(levelname)s -%(module)s:  %(message)s',
                        datefmt='%Y-%m-%d %H:%M:%S %p',
                        level=10)
    logging.debug('debug')
    logging.info('info')
    logging.warning('warning')
    logging.error('error')
    logging.critical('critical')
    logging.log(10,'log')
    

     对于等级:

    CRITICAL = 50
    
    FATAL = CRITICAL
    
    ERROR = 40
    
    WARNING = 30
    
    WARN = WARNING
    
    INFO = 20
    
    DEBUG = 10
    
    NOTSET = 0
    
  • 相关阅读:
    jQuery文档操作之删除操作
    jQuery文档操作之修改操作
    jQuery文档操作之克隆操作
    jQuery文档操作之插入操作
    jQuery的使用
    js Demo
    使用jQuery操作input的value值
    Flask-Request
    Flask-Response
    Flask-认识flask
  • 原文地址:https://www.cnblogs.com/yezuhui/p/6853291.html
Copyright © 2020-2023  润新知