zoukankan      html  css  js  c++  java
  • configparser ,shelve ,hashlib,random模块

    configparser模块
    一句话概念:可以通过configparser将配置信息字典化的读写
    import configparser
    #生成一个这样的文档
    config = configparser.ConfigParser()        #生成一个对象
    config["DEFAULT"] = {'ServerAliveInterval': '45',
                          'Compression': 'yes',
                         'CompressionLevel': '9'}
    
    config['bitbucket.org'] = {'User': 'hg'}
    
    config['topsecret.server.com'] = {'Port': '50022',
                                      'ForwardX11' : 'no'}
    
    with open('example.ini', 'w') as f:
        config.write(f)
    

      

    
    
    #读取
    import configparser
    config = configparser.ConfigParser()        #定义一个对象
    
    config.read('example.ini')      #读取文件
    
    print(config.sections())      #显示除default之外的模块
    
    for key in config['bitbucket.org']: print(key)
    

      

    shelve模块

    一句话概念:shelve模块是一个简单的k,v将内存数据通过文件持久化的模块,可以持久化任何pickle可支持的python数据格式

    写入
    import shelve
    
    d = shelve.open('shelve_test')   #打开一个文件
    
    def stu_data(name,age):
        print("register",name,age)
    
    name = ["alex","rain","test"]
    info = {"name":"alex","age":22}
    
    d["test"] = name
    d["info"] = info
    d['func'] = stu_data
    

      读取

    import shelve
    
    def stu_data(name,age):
        print("stu",name,age)
    
    d = shelve.open("shelev_test")
    print(d.get("test"))
    print(d.get("info"))
    print(d.get("func")("test",30))
    

      

    hashlib模块

    一句话概念:通过md等方式进行加密

    import hashlib      #md5算法,支持中文
    m = hashlib.md5()
    m.update(b"Hello")      #加密
    m.update("支持中文".encode("utf-8"))
    print(m)
    print(m.hexdigest())
    

      

    import hmac                 #hmac算法,双层加密
    h = hmac.new(b'wueiqi','your are 250'.encode("utf-8"))      #自定义key
    h.update(b'hellowo')        #加密
    print(h.hexdigest())
    

      

    random模块

    一句话概念:用来生成一个随机数

    常用语法
    print(random.random())          #区间就0-1
    print(random.uniform(1,10))     #指定区间
    print(random.randint(1,5))      #取整数
    print(random.randrange(1,10))  #不包含10
    print(random.choice('hello'))       #随机找一个值,也可以是列表,元祖
    print(random.sample('hello',2))     #就取前两位
    

      

    洗牌
    items = [1,2,3,4,5,6,7]
    print(items) #[1, 2, 3, 4, 5, 6, 7]
    random.shuffle(items)
    print(items) #[1, 4, 7, 2, 5, 3, 6]
    

      

    字母加数字的验证码
    print(string.digits)        #打印数字
    print(string.ascii_letters)     #打印字母
    str_source = string.ascii_letters + string.digits
    print(random.sample(str_source,7))
    

      

    生成一个验证码方法二
    checkcode = ''
    for i in range(4):
        current = random.randrange(0,4)
        if current != i:
            temp = chr(random.randint(65,90))
        else:
            temp = random.randint(0,9)
        checkcode += str(temp)
    print(checkcode)
    

      






  • 相关阅读:
    scp命令(基于ssh上传文件等)
    mac上安装ruby
    Access denied for user ''@'localhost' to database 'mysql'
    3.ruby语法基础,全部变量,实例变量,类变量,局部变量的使用和注意的要点
    2.ruby基本语法,类的定义
    1.ruby基本格式
    neo4j在linux下的安装
    mongodb导入json文件
    mongodb 安装启动
    Junit4
  • 原文地址:https://www.cnblogs.com/fengdao/p/6080188.html
Copyright © 2011-2022 走看看