• 18_Python常用的模块中的某个方法


    1.imp模块==========>重新加载已加载过的模块方法

    import imp
    
    imp.reload(mymod)  # 重新加载已经加载过的mymod模块

    2.ctypes模块=======>加载动态库方法

    from ctypes
    
    # 加载由C语言编译的libdead_loop.so动态库文件,并返回一个动态库对象
    lib = ctypes.cdll.LoadLibrary("libdead_loop.so")
    lib.DeadLoop()  # 可以直接执行C语言编译后的动态库中的函数

    3.copy模块=========>浅拷贝和深拷贝方法

    import copy  # 导入copy模块
    
    li = [3.1, 3.2]
    li1 = [1, 2, li]  # [1, 2, [3.1, 3.2]]
    li2 = copy.copy(li1)  # [1, 2, [3.1, 3.2]]浅拷贝
    id(li2[2]) == id(li1[2])  # True
    li3 = copy.deepcopy(li1)  # [1, 2, [3.1, 3.2]]深拷贝
    id(li3[2]) == id(li1[2])  # False

    4.contextlib模块===>上下文管理装饰器方法

    from contextlib import contextmanager
    
    
    @contextmanager  # 装饰后保证了使用with管理my_open函数执行时产生异常一定会调用close,同理可可以装饰类
    def my_open(path, mode):
        f = open(path, mode)
        # 通过 yield 将函数分割成两部分,yield 之前的语句在 __enter__ 方法中执行
        # yield 之后的语句在 __exit__ 方法中执行,紧跟在 yield 后面的值是函数的返回值
        yield f
        f.close()
    
    with my_open('out.txt', 'w') as f:
        f.write("hello , the simplest context manager")

    5.urllib模块=======>url编解码方法

    import urllib.parse
    
    # Python3 url编码
    print(urllib.parse.quote("响应头 响应体"))  # %E5%93%8D%E5%BA%94%E5%A4%B4%20%E5%93%8D%E5%BA%94%E4%BD%93
    # Python3 url解码
    print(urllib.parse.unquote("%E5%93%8D%E5%BA%94%E5%A4%B4%20%E5%93%8D%E5%BA%94%E4%BD%93"))  # 响应头 响应体

    6.traceback模块====>更专业的打印异常信息方法

    import traceback
    
    a = 10
    try:
        b = a / '2'
    except TypeError:
        traceback.print_exc()
    print("---end---")
    """执行结果
        Traceback (most recent call last):
          File "<ipython-input-1-929daf19ab90>", line 5, in <module>
            b = a / '2'
        TypeError: unsupported operand type(s) for /: 'int' and 'str'
        ---end---
    """

    7.keyword模块======>列表形式列出Python中的所有关键字方法

    import keyword
    
    print(keyword.kwlist)  # 返回一个包含Python关键字的列表

    8.types模块========>判断一个对象是方法还是函数

    from types import FunctionType
    from types import MethodType
            
    
    class MyClass:
    
        def test(self):
            print("方法被调用")
    
    
    def func():
        print("函数被调用")
    
    
    my_obj = MyClass()
    
    # 判断一个对象是否是函数
    print(isinstance(func,FunctionType))  # True
    # 类名.方法名是函数
    print(isinstance(MyClass.test,FunctionType))  # True
    # 判断一个对象是否是函数
    print(isinstance(obj.test,FunctionType))  # False
    # 实例.方法名是方法
    print(isinstance(obj.test,MethodType))  # True

    9.getpass模块======>隐式输入密码方法

    In [1]: import getpass
    
    In [2]: passwd = getpass.getpass()  # 在输入时会有密码输入提示,且加密输入
    Password:
    
    In [3]: passwd1 = getpass.getpass("确认密码")  # 可以指定提示语
    确认密码:
    
    In [4]: passwd, passwd1
    Out[4]: ('123456', '123456')

    10.functools模块===>对参数序列中元素进行累积方法

    from functools import reduce
    
    num1 = [1, 2, 3, 4, 5]
    # reduce方法遍历合并可迭代对象中的每个元素
    msg = reduce(lambda i, j: i + j, num1, 10)
    print(msg)  # 25

    11.subprocess模块==>执行系统命令方法

    import subprocess
    
    # 语法: res = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.subprocess.PIPE)
    # cmd: 代表系统命令
    # shell=True: 代表这条命令是系统命令,告诉操作系统将cmd当做系统命令去执行
    # stdout: 是执行系统命令后用于保存正确结果的一个管道
    # stderr: 是执行系统命令后用于保存错误结果的一个管道
    
    r = subprocess.Popen("pwd", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    print("正确的返回结果是: %s" % r.stdout.read().decode("utf-8"))  # 正确的返回结果是: /Users/tangxuecheng
    
    r = subprocess.Popen("pwds", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    print("错误的返回结果是: %s" % r.stderr.read().decode("utf-8"))  # 错误的返回结果是: /bin/sh: pwds: command not found

    12.struct模块======>将一个不定长的int数据打包成一个固定4位的bytes类型方法

    import struct
    
    b_len = struct.pack("i", 30)
    print(b_len)  # b'x1ex00x00x00'
    b_len1 = struct.pack("i", 3000)
    print(b_len1)  # b'xb8x0bx00x00'
    
    num1 = struct.unpack("i", b_len)
    print(num1)  # (30,)
    num2 = struct.unpack("i", b_len1)
    print(num2)  # (3000,)

    13.hmac模块========>md5加密算法方法

    import hmac
    
    md5_obj = hmac.new("".encode("utf-8"), "用户密码".encode("utf-8"))
    r = md5_obj.digest()
    print(r)  # b'xa5xc4#x19xa4xaex83x887mxa5xaeCx0cxc6j'
  • 相关阅读:
    OpenGLES 怎样在十天内掌握线性代数
    Matlab自己定义函数
    小小小女神啊~~~
    Format类及其子类功能和使用方法具体解释
    数据库集群
    分布式SESSION
    二级缓存
    应用服务器集群部署
    业务拆分和分级
    最简中间件集群方案
  • 原文地址:https://www.cnblogs.com/tangxuecheng/p/13584593.html
Copyright © 2020-2023  润新知