忽略 python 文件的其余部分 [英] Ignore the rest of the python file

查看:56
本文介绍了忽略 python 文件的其余部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 Python 脚本通常在文件的第一部分包含可执行代码"(函数、类和 &c),并在最后包含测试代码"(交互式实验).

我希望 pythonpy_compilepylint &c 完全忽略最后的实验内容.

我正在为 cpp 寻找类似 #if 0 的东西.

如何做到这一点?

以下是一些想法以及它们不好的原因:

  1. sys.exit(0):适用于 python 但不适用于 py_compilepylint
  2. 将所有实验代码放在 def test() 下::我无法再将代码复制/粘贴到 python REPL 中,因为它具有非平凡的缩进
  3. 将所有实验代码放在带有 """ 的行之间:emacs 不再正确地缩进和字体化代码
  4. 一直对代码进行注释和取消注释:我太懒了(是的,这是一次按键操作,但我必须记住这样做!)
  5. 将测试代码放到一个单独的文件中:我想把相关的东西放在一起

附注.我的 IDE 是 Emacs,我的 Python 解释器是 pyspark.

解决方案

在你的 REPL 中使用 ipython 而不是 python 它有更好的代码补全和内省,当你粘贴的时候缩进的代码,它可以自动取消缩进";粘贴的代码.

因此,您可以将您的实验代码放在测试函数中,然后将部分粘贴到其中,而无需担心和取消缩进代码.

如果您要粘贴可以被视为单个块的大块,那么您将需要使用 %paste%cpaste 魔法.

例如

 for i in range(3):我*= 2# 加上下面的空行,这是一个完整的块打印(一)

使用普通粘贴:

In [1]: for i in range(3):...:我*= 2...:在 [2] 中:打印(i)4

使用%paste

在 [3]: %paste对于范围内的我(10):我*= 2打印(一)## -- 结束粘贴的文本 --024在 [4] 中:

PySpark 和 IPython

<块引用>

还可以在增强型 Python 解释器 IPython 中启动 PySpark.PySpark 适用于 IPython 1.0.0 及更高版本.要使用 IPython,请在运行 bin/pyspark 时将 IPYTHON 变量设置为 1:1

$ IPYTHON=1 ./bin/pyspark

My python scripts often contain "executable code" (functions, classes, &c) in the first part of the file and "test code" (interactive experiments) at the end.

I want python, py_compile, pylint &c to completely ignore the experimental stuff at the end.

I am looking for something like #if 0 for cpp.

How can this be done?

Here are some ideas and the reasons they are bad:

  1. sys.exit(0): works for python but not py_compile and pylint
  2. put all experimental code under def test():: I can no longer copy/paste the code into a python REPL because it has non-trivial indent
  3. put all experimental code between lines with """: emacs no longer indents and fontifies the code properly
  4. comment and uncomment the code all the time: I am too lazy (yes, this is a single key press, but I have to remember to do that!)
  5. put the test code into a separate file: I want to keep the related stuff together

PS. My IDE is Emacs and my python interpreter is pyspark.

解决方案

Use ipython rather than python for your REPL It has better code completion and introspection and when you paste indented code it can automatically "de-indent" the pasted code.

Thus you can put your experimental code in a test function and then paste in parts without worrying and having to de-indent your code.

If you are pasting large blocks that can be considered individual blocks then you will need to use the %paste or %cpaste magics.

eg.

    for i in range(3):
        i *= 2 
        # with the following the blank line this is a complete block

        print(i)

With a normal paste:

In [1]:         for i in range(3):
   ...:                 i *= 2
   ...:     

In [2]:             print(i)
4

Using %paste

In [3]: %paste
        for i in range(10):
            i *= 2

            print(i)

## -- End pasted text --
0
2
4

In [4]: 

PySpark and IPython

It is also possible to launch PySpark in IPython, the enhanced Python interpreter. PySpark works with IPython 1.0.0 and later. To use IPython, set the IPYTHON variable to 1 when running bin/pyspark:1

$ IPYTHON=1 ./bin/pyspark

这篇关于忽略 python 文件的其余部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆