如何在Python中运行一段代码,类似于Matlab

时间:2014-05-08 19:29:47

标签: python matlab python-idle

我有一个代码,包括1)加载数据,2)处理数据。

# Load data
import geoio.vtio as vtio
(traces, params)=vtio.vt_read("../Seismic/R2771_10PRDMkf_Full_T_Rzn_RMO.vt")

# Do things with the data
N=params['num_samples']
dt=params['digi']

通常当我运行Python脚本时,我只需点击" F5"在IDLE中,重新运行任何先前的计算并不需要太长时间。但是,在这种情况下,加载数据需要几秒钟,我只想运行其他命令,类似于在Matlab中运行m文件中的一段代码。这可能吗?

1 个答案:

答案 0 :(得分:1)

如果你真的在谈论iPython

学习iPython好东西

使用?开始使用

>>> ?
IPython -- An enhanced Interactive Python
=========================================

IPython offers a combination of convenient shell features, special commands
and a history mechanism for both input (command history) and output (results
caching, similar to Mathematica). It is intended to be a fully compatible
replacement for the standard Python interpreter, while offering vastly
improved functionality and flexibility.

At your system command line, type 'ipython -h' to see the command line
options available. This document only describes interactive features.

MAIN FEATURES
-------------

* Access to the standard Python help. As of Python 2.1, a help system is
  available with access to object docstrings and the Python manuals. Simply
  type 'help' (no quotes) to access it.

* Magic commands: type %magic for information on the magic subsystem.

* System command aliases, via the %alias command or the configuration file(s).

* Dynamic object information:

  Typing ?word or word? prints detailed information about an object.  If
  certain strings in the object are too long (docstrings, code, etc.) they get
  snipped in the center for brevity.

  Typing ??word or word?? gives access to the full information without
  snipping long strings. Long strings are sent to the screen through the less
  pager if longer than the screen, printed otherwise.
  ....etc....

了解%magic

>>> %magic    
IPython's 'magic' functions
===========================

The magic function system provides a series of functions which allow you to
control the behavior of IPython itself, plus a lot of system-type
features. There are two kinds of magics, line-oriented and cell-oriented.

Line magics are prefixed with the % character and work much like OS
command-line calls: they get as an argument the rest of the line, where
arguments are passed without parentheses or quotes.  For example, this will
time the given statement::

        %timeit range(1000)

Cell magics are prefixed with a double %%, and they are functions that get as
an argument not only the rest of the line, but also the lines below it in a
separate argument.  These magics are called with two arguments: the rest of the
call line and the body of the cell, consisting of the lines below the first.
For example::

        %%timeit x = numpy.random.randn((100, 100))
        numpy.linalg.svd(x)

will time the execution of the numpy svd routine, running the assignment of x
as part of the setup phase, which is not timed.
...etc...

我的收藏夹

  • %edit - 在外部编辑器中编辑某些内容(上一行,函数,我的文件)
  • %load - 从文件加载数据,并允许您添加一些文本,然后运行它
  • %debug - debug crashed script
  • %历史记录 - 显示到目前为止所做的工作
  • %粘贴 - 粘贴并运行剪贴板中的代码
  • %macro - 这可能是您问题的解决方案
  • %timeit - 衡量执行时间
  • %召回 - 也接近您的问题
  • %重新运行 - 尝试一下

总的来说,如果我通过它的帮助,我总是在iPython中找到一些生产力助推器。也试一试。

注意:我在控制台中使用iPyhton,但其他UI版本应提供非常相似的功能(如果不完全相同)。