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 / ''
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]: ('', '')
10.functools模块===>对参数序列中元素进行累积方法
from functools import reduce num1 = [1, 2, 3, 4, 5]
# reduce方法遍历合并可迭代对象中的每个元素
msg = reduce(lambda i, j: i + j, num1, 10)
print(msg) #
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'\x1e\x00\x00\x00'
b_len1 = struct.pack("i", 3000)
print(b_len1) # b'\xb8\x0b\x00\x00' 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'\xa5\xc4#\x19\xa4\xae\x83\x887m\xa5\xaeC\x0c\xc6j'
18_Python常用的模块中的某个方法的更多相关文章
- itertools模块中的product方法
itertools模块中的product方法 itertools.product(*iterables[, repeat]) 笛卡尔积 创建一个迭代器,生成表示item1,item2等中的项目的笛卡尔 ...
- Python OS模块中的fork方法实现多进程
import os '''使用OS模块中的fork方式实现多进程''' '''fork方法返回两次,分别在父进程和子进程中返回,子进程中永远返回0,父进程返回的是子进程的is''' if __name ...
- 引入math模块中的log()方法,导致"TypeError: return arrays must be of ArrayType",什么原因?
from math import log from numpy import *import operator ............ re = log(pro,2) ............ Ty ...
- python 查看模块中的方法
way 1.help() way 2.dir() # dir() 函数不带参数时,返回当前范围内的变量.方法和定义的类型列表: way 3. 使用inspect模块, inspect.getmembe ...
- os、os.path模块(文件/目录方法)
1.模块的概念:模块是一个包含所有定义的变量.函数的文件,模块可以被其余模块调用. 2.利用OS模块实现对系统文件的. os模块中常见的方法: gercwd() 返回当前工作目录 chdir( ...
- 测试脚本中的等待方法 alter对话框处理
测试脚本中的等待方法 等待是为了使脚本执行更加稳定 1. 常用的休眠方式:time模块的sleep方法 2. selenium模块中的等待方法 等待查找5s 查找不到就报错 对登录测试py进行修改 a ...
- python模块中的__all__属性
转自:http://blog.csdn.net/sxingming/article/details/52903377 python模块中的__all__属性,可用于模块导入时限制,如:from mod ...
- python基础:os模块中关于文件/目录常用的函数使用方法
Python是跨平台的语言,也即是说同样的源代码在不同的操作系统不需要修改就可以同样实现 因此Python的作者就倒腾了OS模块这么一个玩意儿出来,有了OS模块,我们不需要关心什么操作系统下使用什么模 ...
- os模块中关于文件/目录常用的函数使用方法
os模块中关于文件/目录常用的函数使用方法 函数名 使用方法 getcwd() 返回当前工作目录 chdir(path) 改变工作目录 listdir(path='.') 列举指定目录中的文件名('. ...
随机推荐
- SpringCloud系列之服务容错保护Netflix Hystrix
1. 什么是雪崩效应? 微服务环境,各服务之间是经常相互依赖的,如果某个不可用,很容易引起连锁效应,造成整个系统的不可用,这种现象称为服务雪崩效应. 如图,引用国外网站的图例:https://www. ...
- JS 仿京东放大镜
css代码 body{ margin:; } .box { width: 1210px; position: relative; background-color: pink; margin: 0 a ...
- SpringBoot介绍,快速入门小例子,目录结构,不同的启动方式,SpringBoot常用注解
SpringBoot介绍 引言 为了使用ssm框架去开发,准备ssm框架的模板配置 为了Spring整合第三方框架,单独的去编写xml文件 导致ssm项目后期xml文件特别多,维护xml文件的成本也是 ...
- 2020-04-13:TCP协议本身会导致什么样的安全问题?
福哥答案2020-04-14: 洪泛攻击
- 2020-04-18:synchronized和reentrantLock的异同
福哥答案2020-04-19:采纳群员答案: 1 synchronized是关键字,reentrantlock是类,API层面的2 前者是通过monitor来实现锁机制,后者是基于AQS实现的,通过内 ...
- 将Asp.Net Core3.1项目,使用Docker 部署到Centos 8
一.准备工具 Win 10 Centos 8 Visual Studio 2019 Docker Desktop 下载地址:https://download.docker.com/win/stabl ...
- 关于在Visual Studio 2019预览版中的用户体验和界面的变化
原文地址:https://blogs.msdn.microsoft.com/visualstudio/2018/11/12/a-preview-of-ux-and-ui-changes-in-visu ...
- C#设计模式之11-享元模式
享元模式(Flyweight Pattern) 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/409 访问. 享元模式属 ...
- hge引擎示例教程cmake项目
hge引擎的示例代码在vs2017不能很好的运行,需要调不少东西,所以我将其重新整理成cmake的项目. 所有示例均在vs2017 msvc 下测试可以正常运行. 由于缺少libhgehelp.a所以 ...
- Android Studio 突然无法识别真机问题
最近在赶项目,今天AS突然疯狂跟我作对,森气!! 平时连接手机没有问题,今天突然各种识别不到真机!! 1.数据线,check.没有问题. 2.重启AS,还是不行. 3.安装驱动,行不通. 4.已经弹出 ...