psutil (introduction)
Obtain information about the processes running on the system and system utilization (including CPU, memory, disk, network, etc.).
psutil (understand)
psutil is a cross-platform library (http://pythonhosted.org/psutil/)
It is mainly used for system monitoring, performance analysis, and process management.
It implements the functionality provided by equivalent command line tools.
psutil (reference code)
The proportion of memory used by the program to run
import subprocess, psutil, gc
mem1 = psutil.virtual_memory()
print(f'某程序前-内存已使用:{mem1.used}')
print(f'某程序前-内存剩余:{mem1.free}')
print(f'某程序前-内存百分比:{mem1.percent}')
app1 = subprocess.Popen(r'C:\Windows\write.exe')
app2 = subprocess.Popen(r'C:\Windows\winhlp32.exe')
app3 = subprocess.Popen(r'C:\Windows\write.exe')
mem2 = psutil.virtual_memory()
print(f'某程序后-内存已使用:{mem2.used}')
print(f'某程序后-内存剩余:{mem2.free}')
print(f'某程序后-内存百分比:{mem2.percent}')
app1.kill()
app2.kill()
app3.kill()
gc.collect()#启用垃圾回收模式
mem3 = psutil.virtual_memory()
print(f'GC回收后-内存已使用:{mem3.used}')
print(f'GC回收后-内存剩余:{mem3.free}')
print(f'GC回收后-内存百分比:{mem3.percent}')
sys(Introduction)
An interface for Python interpreter interaction, mainly surrounding some auxiliary tools for Python programs. For example: control the running of Python programs, change the Python environment...
sys (parameter list)
sys.exit() |
exit the program The default is 0, which means a normal exit, and it can also be 1, which means an abnormal exit. [Example] sys.exit(0) or sys.exit(1) |
sys.path.append(path) |
Call Python file 【example】 #Add path (AG.py is placed on the computer desktop) import sys sys.path.append('C:/Users/Administrator/Desktop') # Call file # import AG |
sys.argv |
Implementation of passing parameters from outside the program to the program |
sys.path |
List of directory names containing input modules |
sys.version | Get Python interpreter version |
sys.exc_info() | Return exception information triplet |
sys.getdefaultencoding() | Get the current encoding of the system, the default is utf-8 |
sys.setdefaultencoding() | Set the system's default encoding |
sys.getfilesystemencoding() | Get the encoding used by the file system, the default is utf-8 |
sys.modules | Returns all imported modules in the current Python environment in the form of a dictionary |
sys.copyright | Current Python copyright information |
sys.getrefcount(object) | Returns the number of references to the object |
sys.getrecursionlimit() | Returns the maximum recursion depth of Python, default is 1000 |
sys.getsizeof(object[, default]) | Returns the size of the object |
sys.getwindowsversion() | Returns the version information of the current Windows system |
sys.executable | Absolute path to python.exe |
sys.prefix | Current python installation path |
sys.exec_prefix | Installation path of basic python environment |
sys.base_exec_prefix | Installation path of basic python environment |
sys.builtin_module_names | All modules in the python interpreter |
sys.stdlib_module_names | A frozen collection containing standard library module name strings |
sys.hexversion | integer version number |
sys.is_finalizing() | #True if the Python interpreter is exiting |
argparse (introduction)
Custom python commands.
argparse (understand)
[Self-built command tool]
Terminal: python file command command function
【example】
Terminal: python demo.py --help
# help command (see documentation)
argparse (reference code)
Simple example
import argparse
parser = argparse.ArgumentParser(description="【文档描述】")
parser.add_argument("-f", "--function", help="【-f或--function命令 关于函数功能的使用方法】")
parser.add_argument("-i", "--info", help="【-i或--info命令 关于文档的注释】")
args = parser.parse_args()
if args.function == "function":
print(">>>输入了function命令")
elif args.info == "info":
print(">>>输入了info命令")
else:
pass
distutils (introduction)
Custom Python module.
distutils (understand)
【Instructions】
Package Python' modules, then publish, install and use.
【Module installation】
Source package: python setup.py install
Online installation: pip install package name (linux) / easy_install package name (window)
distutils (reference code)
Custom module (simplified version)
from distutils.core import setup
setup(
name='test_md', # 安装的模块名
version='1.0', # 版本
author='AG', # 作者
author_email='@xxx.com', # 邮箱
py_modules=['test'], # 使用的模块名
)
# @使用方法 /
# 1.新建test.py文件 -> 内容(随便写个函数)
# 2.新建setup.py文件 -> 内容(上面代码加入)
# 3.两个文件同处一个目录下->当前目录->终端:python setup.py sdist
# 4.(安装模块) 终端:python setup.py install
# 5.(测试):import test #test.test()
# --------------------------------- /
Custom module (detailed version)
# -*- encoding: UTF-8 -*-
from setuptools import setup
setup(
name='testFunc', # 安装包名 注释:尽量与packages属性的项目文件夹名相同
version='0.1', # 版本信息
author='AG', # 作者
author_email='[email protected]', # 作者邮箱地址
description='关于程序的描述', # 程序的简单描述
long_description='关于程序的详细描述', # 程序的详细描述
keywords='关于程序的关键词', # 程序的关键字列表
packages=['testFunc'], # 要打包的项目文件夹
include_package_data=True, # 自动打包文件夹内所有数据
zip_safe=True, # 设定项目包为安全,不用每次都检测其安全性
install_requires=[ # 需要安装的依赖包
'docutils>=0.3',
'requests',
],
)
# @流程 /
# 1.新建setup.py文件 -> 内容(上面代码加入)
# 2.新建文件夹 -> func.py , init.py
# 3.setup.py 所在目录 ->
# 终端:python setup.py sdist 进行打包
# :python setup.py build 构建目录
# :python setup.py install 安装包
# ---------------------------------------/