zoukankan      html  css  js  c++  java
  • 博客整理day17 time/datetime/re/random/hashlib/hmac/requests

    Python 博客整理 day17

    ​ 包本质上就是模块,可以和模块一样,用import导入

    ​ 包是含有__init__.py的文件;导包就是导入__inti__

    ​ 包一定是被当做模块文件导入,模块文件 的搜索路径以执行文件 的路径为准

    模块和包

    导入模块发生的三件事:

    1. 创建一个包的名称空间
    2. 执行py文件,将执行过程中产生的名字存放于名称空间中
    3. 在当前执行文件中拿到一个名字   ,  该名字是指向内包的名称空间的
    

    导入包发生的三件事:

    1. 创建一个包的名称空间
    2. 由于包是一个文件夹,无法执行包,因此执行包下的 `.py` 文件,将执行过程中产生的名字存放于包名称空间中(即包名称空间中存放的名字都是来自于`.py`)
    3. 在当前执行文件中拿到一个名字  ,  该名字是指向包的名称空间的
    

    绝对导入和相对导入

    ​ 绝对导入:

    ​ 用上一级包名导入

    ​ 相对导入;

    .代表当前被导入文件所在的文件夹

    ..代表当前被导入文件所在的文件夹的上一级

    ...代表当前被导入文件所在的文件夹的上一级的上一级

    time模块

    #time 模块:提供了三种不同类型的时间(时间戳),三种不同类型的时间可以互相转换
    import time
    
    print(time.time())#时间戳形式
    
    #格式化时间
    print(time.strftime('%Y-%m-%d %X'))
    
    #结构化时间
    print(time.localtime())
    
    #结构化时间 --> 格式化时间
    struct_time = time.localtime(3600*24*365)
    print(time.strftime('%Y-%m-%d %X',struct_time))
    
    #格式化时间-->结构化时间
    format_time = time.strftime('%Y-%m-%d %X')
    print(time.strptime(format_time,'%Y-%m-%d %X'))
    
    #结构化时间 --> 时间戳
    struct_time = time.localtime(3600*24*365)
    print(time.mktime(stryct_time))
    
    #时间戳 --> 结构化时间
    time_stamp = time.time()
    print(time.localtime(time_stamp))
    
    time.time.sleep()
    

    datetime模块

    datetime模块: 时间的加减

    import datetime
    
    now = datetime.datetime.now()
    print(now)
    
    #默认三天
    print(now + datetime.timedelta(3))
    
    #加3周
    print(now + datetime.timedelta(weeks = 3))
    
    #加3小时
    print(now + datetime.timedelta(hours = 3))
    
    #减3小时
    print(now + datetime.timedelta(hours = 3))
    
    print(now.replace(year = 1949,month = 10, hour = 10,minute = 1,secend = 0))
    

    random模块

    random模块:随机数

    #随机生成0-1
    print(radom.random())
    
    #随机生成  1- 3整数
    print(random.randint(1,3))
    
    #打乱顺序
    lt= [1,2,3]
    random.shuffle(lt)
    print(lt)
    
    #随机选择一个  -->  梅森旋转算法
    random.seed(time.time())
    print(random.random())
    
    
    #做了解 -->  从列表中随机选两个
    print(random.sample([1,'a','c',2,3,4],2))
    

    hashlib模块/hmac模块

    hashlib模块:对字符加密

    #叠加性
    import hashlib
    
    m = hashlib.md5()
    m.update(b'say')
    m.update(b'hello')
    print(m.hexdigest())
    

    hmac模块: 对字符加密,并且加上密钥

    #hamc 密钥(加盐)
    import hamc
    
    m = hmac.new(b'')
    m.updateb(b'password')
    print(m.hexdigest())
    

    typing模块

    typing模块:与函数连用,控制函数参数的数据类型,提供了基础数据类型之外的数据类型

    lt = [1, 2, 3, 4]
    print(type(lt) is list)
    
    from tpying import Iterable
    
    def func(x:int ,lt: Iterable) -> list:
        return [1,2,3
                
    func(10, '123123')
    

    requests模块

    requests模块:模拟浏览器,向网站发送请求

    import requests
    
    response = requests.get('http://www.baidu.com')
    data = response.text
    
    with open('test.txt','w',encoding='utf8') as fw:
        fw.write(data)
    

    re模块

    re模块:去字符串找符合某种特点的字符串

    import re
    
    s = 'abcdabc'
    #^ :以...开头
    res = re.findall('^ab',s)
    
    #$ :以...结尾
    res = re.findall('bc$',s)
    
    # . :任意字符
    res = re.findall('abc',s)
    
    # d :数字
    res = re.findall('d',s)
    
    # w :非空,数字字母下划线
    res = re.findall('w',s)
    
    # s :空,空格
    res = re.findall('s',s)
    
    # D:非数字
    res = re.findall('D',s)
    
    # W:空
    res = re.findall('W',s)
    
    #S :非空
    res = re.findall('S',s)
    
    # + :前面的一个字符至少一个
    print(re.findall('abc+',s))
    
    # ? :寻找到?前面的一个字符 0-1 个
    print(re.findall('abcdd?', s))
    
    # * :前面的一个字符至少0个
    print(re.findall('abcd*',s))
    
    # [] :中括号内的都可以
    print(re.findall('[abc]bc',s))
    
    # [^] :中括号内的都不可以
    print(re.findall('[^abc]bc',s))
    
    # | : 或
    print(re.findall('abc|bbc',s))
    
    # {2}:前面的字符  2个
    print(re,findall('abc{2}',s))
    
    # {1,2} :前面的字符 2 个
    print(re,findall('abc{1,2}',s))
    
    #贪婪模式
    # .(任意字符) * (0-无穷个)
    print(re.findall('a.*g',s))
    
    #非贪婪模式
    # . (任意字符) * (0-无穷个)
    print(re,findall('a.*?g',s))
    
    #bug  # . (任意字符) * (0-无穷个)
    print(re.findall('.*?',s))
    
  • 相关阅读:
    洛谷 P1092 虫食算
    2018.3.25校内互测
    洛谷 P2915 [USACO08NOV]奶牛混合起来Mixed Up Cows
    洛谷 P1879 [USACO06NOV]玉米田Corn Fields
    洛谷 P3052 [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper
    ZJOI Day 2 游记
    editorial-render A
    BZOJ2904
    BZOJ 1600
    构造脚本语言
  • 原文地址:https://www.cnblogs.com/samoo/p/11608400.html
Copyright © 2011-2022 走看看