【Python进阶(四)】——魔术命令

简介: 【Python进阶(四)】——魔术命令

【Python进阶(四)】——魔术命令,建议收藏!

该篇文章首先利用Python展示了其常的魔术命令,包括类的运行程序;运行时间计算;更在异常信息;调试程序;程序运行情况;内存使用情况的相关魔术命令。

1 运行.py文件: %run

 运行程序:

import os
os.getcwd() #查询当前工作目录
os.chdir("D:\\pythonjupyter")  #改变当前工作目录
%run testme.py

testme.py 为"D:\pythonjupyter"工作路径下的.py文件

2 统计运行时间: %timeit与%%timeit

2.1 %timeit

  运行程序:

%timeit myList2=[n**2 for n in range(100)]
%timeit x=1#查看某行代码运行时间

  运行结果:

26.3 µs ± 522 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)
10.7 ns ± 0.414 ns per loop (mean ± std. dev. of 7 runs, 100000000 loops each)

2.2 %%timeit

  运行程序:

%%timeit x=1  #查看多行代码所需时间(%%timeit前不能有任何代码,注释类的代码都不行,否者报错)
x=x+2
x=x*2

运行结果:

2.71 µs ± 37.1 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

%timeit与%%timeit区别: %timeit为统计单行代码运行时间;%%timeit为统计多行代码所需时间。

3 更改异常信息的显示模式:%xmode

3.1 mode:Plain

  运行程序:

%xmode Plain
x1=2
x2=X1

  运行结果:

Exception reporting mode: Plain
Traceback (most recent call last):
  File "<ipython-input-115-59f69ef917e0>", line 3, in <module>
    x2=X1
NameError: name 'X1' is not defined

3.2 mode: Verbose

  运行程序:

%xmode Verbose
x1=2
x2=X1

  运行结果:

Exception reporting mode: Verbose
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-116-a2ff507801f7> in <module>
      1 get_ipython().run_line_magic('xmode', 'Verbose')
      2 x1=2
----> 3 x2=X1
        global x2 = undefined
        global X1 = undefined
NameError: name 'X1' is not defined

4 调试程序: %debug

  运行程序:

%debug
x1=2
x2=X1

  运行结果:

> <ipython-input-116-a2ff507801f7>(3)<module>()
      1 get_ipython().run_line_magic('xmode', 'Verbose')
      2 x1=2
----> 3 x2=X1
ipdb> q
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-117-e8d5ec0ce5b1> in <module>
      1 get_ipython().run_line_magic('debug', '')
      2 x1=2
----> 3 x2=X1
        global x2 = undefined
        global X1 = undefined
NameError: name 'X1' is not defined

5 程序运行的逐行统计:%prun与%lprun

  运行程序:

def myfunc1(n):
    n=n+1
    for i in [1,2,3,4,5]:
        n=n+i
    return(n)
%prun myfunc1(100)   #%prun:程序运行情况逐行统计
%lprun myfunc1(100)  #%lprun:程序运行情况逐行统计,并非ipython自带命令

6 内存使用情况的统计: %memit

  运行程序:

%load_ext memory_profiler  #导入包
%memit myfunc2(100)


相关文章
|
6天前
|
机器学习/深度学习 Shell 开发工具
Python使用管道执行git命令报错|4-7
Python使用管道执行git命令报错|4-7
|
19天前
|
Unix Shell Linux
nohup python -u ai_miniprogram_main.py > ../iwork.out 2>&1 & 这句命令是做什么的?
nohup python -u ai_miniprogram_main.py > ../iwork.out 2>&1 & 这句命令是做什么的?
9 1
|
4天前
|
Python Windows
Python:执行py命令,提示: Can‘t find a default Python.
Python:执行py命令,提示: Can‘t find a default Python.
|
8天前
|
Shell Linux Python
python执行linux系统命令的几种方法(python3经典编程案例)
文章介绍了多种使用Python执行Linux系统命令的方法,包括使用os模块的不同函数以及subprocess模块来调用shell命令并处理其输出。
12 0
|
2月前
|
API 开发工具 网络架构
【Azure Developer】使用Python SDK去Azure Container Instance服务的Execute命令的疑问解释
【Azure Developer】使用Python SDK去Azure Container Instance服务的Execute命令的疑问解释
【Azure Developer】使用Python SDK去Azure Container Instance服务的Execute命令的疑问解释
|
2月前
|
前端开发 计算机视觉
Building wheel for opencv-python (pyproject.toml) ,安装命令增加 --verbose 参数
Building wheel for opencv-python (pyproject.toml) ,安装命令增加 --verbose 参数
117 2
|
2月前
|
API 开发工具 网络架构
【Azure Developer】使用Python SDK去Azure Container Instance服务的Execute命令的疑问解释
Azure 容器实例(Azure Container Instances,简称 ACI)是一个无服务器容器解决方案,允许用户在 Azure 云环境中运行 Docker 容器,而无需设置虚拟机、集群或编排器。 ACI 适用于任何可以在隔离容器中操作的场景,包括事件驱动的应用程序、从容器开发管道快速部署、数据处理和生成作业。
|
2月前
|
Linux Shell 数据库
python Django教程 之 安装、基本命令、视图与网站
python Django教程 之 安装、基本命令、视图与网站
|
3月前
|
数据采集 网络协议 数据挖掘
网络爬虫进阶之路:深入理解HTTP协议,用Python urllib解锁新技能
【7月更文挑战第30天】网络爬虫是数据分析和信息聚合的关键工具。深入理解HTTP协议及掌握Python的urllib库对于高效爬虫开发至关重要。HTTP协议采用请求/响应模型,具有无状态性、支持多种请求方法和内容协商等特点。
39 3
|
3月前
|
Python
python常用命令
python常用命令
32 1
下一篇
无影云桌面