zoukankan      html  css  js  c++  java
  • day17

    • 定义:

      包就是模块的一种形式,包的本质就是一个含有.py文件的文件夹

    • 为什么要用包:模块的第一个文件就只有十个功能,但是未来在扩展版本的时候,模块名和用法应该最好不要去修改,但是这只是对使用者友好,而由于版本扩展,文件越来越大,模块设计者对模块的管理、维护会越来越复杂,因此我们可以使用包来扩展模块的功能。

    • 导入包发生的三件事情:

    1. 创建一个包的名称空间
    2. 由于包是一个文件夹,无法执行包,因此执行包下的.py文件,将执行官过程中产生的名字存放于包名称空间中(即包名称空间中存放的名字都是来源于.py)
    3. 在当前执行文件中拿到一个名字aaa,aaa是指向包的名称空间的。
    • 导入包的两种方式:
    1. import...
    2. from...import...

    • 包是含有_init_.py的文件夹;导包就是导入_init_

    ​ 包一定是被当做模块文件导入的,模块文件m1.py/m2.py的搜索路径以执行文件 包的介绍.py路径为基准

    ​ 相对导入绝对导入:只能在包中内部使用

    time模块

    time模块:提供了三种不同类型的时间(时间戳),三种不同类型的时间可以互相转换

    *****主要掌握

    time.time()
    time.sleep(1)
    

    datetime模块

    datetime模块:时间的加减

    import datetime
    now = datetime.datetime,now()
    print(now)
    
    默认3天
    print(now + datetime.timedelta(3))
    
    减3周
    print(now - datetime.timedelta(weeks = 3))
    

    random模块

    随机数模块

    • 掌握

    随机0-1之间的数

    print(random.random())

    [1-3]

    print(random.randint(1,3))

    • 随机选择一个

    lt = [1,2,3]

    print(random.choice(lt))

    • 了解

    print(random.sample([1,2,'a','c',2],2))

    运行结果:

    ['c', 1]

    hashlib模块和hmac模块

    • hashlib模块:对字符加密
    • hmac模块:对字符加密,并且加上密钥
    import hashlib
    
    m=hashlib.md5()
    m.update(b'hfss1313')
    print(m.hexdigest())
    
    hash_pwd = '0562b36c3c5a3925dbe3c4d32a4f2ba2'
    
    pwd_list = [
        'hfss1313',
        'hash1313',
        'hash94139413',
        'hash123456',
        '123456hash',
        'h123ash',
    ]
    

    hash是一种算法

    tying模块

    与函数联用,控制函数参数的数据类型,提供了基础数据类型之外的数据类型

    • 类型检查,防止运行时出现参数和返回值类型不符合
    • 作为开发文档附加说明,方便使用者调用时传入和返回参数类型
    • 该模块加入后并不会影响程序的运行,不会报正式的错误,只有提醒
    • 注意:tying模块只有在python3.5以上的版本中才可以使用,pycharm目前支持tying检查

    tying常用类型

    • int、long、float:整型、长整型、浮点型
    • bool、str:布尔型,字符串类型
    • list、tuple、dict、set:列表、元组、字典、集合
    • iterable、iterator:可迭代类型、迭代器类型
    • generator:生成器类型

    requests模块

    爬虫——》爬取数据,模拟浏览器对url发送请求,拿到数据

    re模块

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

    • 贪婪模式

    .(任意类型)*(0-无穷个)

    • 非贪婪模式

    .(任意类型)*(0-无穷个)?(让他进入非贪婪模式)

    • 特殊构造

    s='a123 aaaa a234 abc'

    a(?=d):a后面是数字,但是不要数字,不消耗字符串内容

    匹配邮箱

    s=#@#@#@nickchen121@163.com$$$$////nick@qq.com$$#$#$[]]2287273393@162.com@$243lks'
    # w(字母/数字/下划线)+(0-无穷个)@ w(字母/数字/下划线)+(0-无穷个).com
    print(re.findall('w+@w+.com', s))
    

    以下必须记住的:

    • 贪婪.(任意字符)*(0-无穷个)
    s = 'abcd abcdkkkkkksb abcd '
    print(re.findall('a.*k',s))
    
    运行结果:['abcd abcdkkkkkk']
    
    • 非贪婪:.*?
    s = 'abckkkkkkkkkkkkkkkb '
    print(re.findall('a.*?b',s))
    
    运行结果:['ab']
    
    • findall:不匹配换行符
    s = 'abcd abcd*asb abcd '
    print(re.findall('abc.abc',s))
    print(re.findall('abcd.abcd',s,re.S))
    
    运行结果:
    []
    ['abcd abcd']
    
    • re.S:修饰符,匹配换行符
    s = 'abcd abcd*asb abcd '
    print(re.findall('abcd.abcd',s,re.S))
    
    运行结果:['abcd abcd']
    
    • match:从头开始找,找到就停止查找;找不到就报错
    s = 'abcd abc abcd '
    res = re.match('abcd*',s)
    print(res.group())
    
    运行结果:abcd
    
    • search:从字符串中找,找到就不找了
    s = 'abcd abc abcd '
    res = re.search('abc',s)
    print(res.group())
    
    运行结果:abc
    
    • 分组:只要括号里面的
    s = 'abcd aaaa abcd '
    print(re.findall('a(.)c(d)',s))
    
    运行结果:[('b', 'd'), ('b', 'd')]
    
    • 有名分组:给分组加名字
    s = 'abcd aaaa abcd '
    print(re.search('a(?P<name1>.)c(?P<name2>d)', s).groupdict())
    
    运行结果:{'name1': 'b', 'name2': 'd'}
    

    了解:

    元字符

    特殊构造的元字符

    特殊修饰符

    函数

  • 相关阅读:
    leetcode 86. Partition List
    leetcode 303. Range Sum Query
    leetcode 1310. XOR Queries of a Subarray
    leetcode 1309. Decrypt String from Alphabet to Integer Mapping
    leetcode 215. Kth Largest Element in an Array
    将numpy.ndarray写入excel
    leetcode 1021 Remove Outermost Parentheses
    leetcode 1306. Jump Game III
    leetcode 1305. All Elements in Two Binary Search Trees
    ICCV2019 oral:Wavelet Domain Style Transfer for an Effective Perception-distortion Tradeoff in Single Image Super-Resolution
  • 原文地址:https://www.cnblogs.com/gfhh/p/11604488.html
Copyright © 2011-2022 走看看