Ignore the rest of the python file

My python scripts often contain “executable code” (functions, classes, and 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 I do that?

Here are some ideas and reasons why they are bad:

  • sys.exit(0) : works for python , but not py_compile and pylint
  • put all the experimental code under def test(): I can no longer copy / paste the code in python REPL, because it has a non-trivial indent
  • put all the experimental code between the lines with """ : emacs are no longer indented and correctly encodes the code
  • comment and uncomment the code all the time: I'm too lazy (yes, this is one keystroke, but I have to remember that!)
  • put the test code in a separate file: I want to save the linked material together

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

+4
source share
7 answers

Use ipython , not python for your REPL. It has better code completion and introspection, and when you insert an embedded code, it can automatically "back off" from the inserted code.

This way you can put your experimental code in a test function and then insert the parts without worrying, and you will have to cancel your code.

If you insert large blocks that can be thought of as separate blocks, you will need to use the magic of %paste or %cpaste .

eg.

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

With regular 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

You can also run PySpark in IPython, an advanced Python interpreter. PySpark works with IPython 1.0.0 and later. To use IPython, set the IPYTHON variable to 1 when bin / pyspark starts: 1

 $ IPYTHON=1 ./bin/pyspark 
+3
source

Unfortunately, there is no broad (or any) standard describing what you are talking about, so getting a bunch of python-specific things to work this way will be difficult.

However, you can wrap these commands so that they are read only to the mark. For example (if you are on a unix system):

 cat $file | sed '/exit(0)/q' |sed '/exit(0)/d' 

The command will read until "exit (0)" is found. You can transfer this to your checkers or create a temporary file that your checkers read. You can create wrapper executables in your path that can work with your editors.

Windows can use a similar technique.

I would recommend a different approach. Shared files may be the best. You can research iPython laptops as a possible solution, but I'm not sure what your use case is.

+2
source

With python-mode.el, mark arbitrary fragments as a section - for example, via py-sectionize-region .

Than calling py-execute-section .

Updated after comment:

python-mode.el is delivered by melpa.

Mx list-packages RET

Look for python mode - the built-in python.el provides 'python, and python-mode.el provides' python-mode.

Just redone development: https://gitlab.com/python-mode-devs/python-mode

+1
source

Follow the example of option 2.
I usually use experimental code in the main method.

 def main (): *experimental code goes here * 

Then, if you want to run the experimental code, just call main.

 main() 
+1
source

I think the standard ("Pythonic") way to handle this is to do this:

 class MyClass(object): ... def my_function(): ... if __name__ == '__main__': # testing code here 

hit>

Edit after your comment

I don’t think you want using a simple Python interpreter. You could take a look at the Python IEP editor ( website , bitbucket ): it supports something like Matlab cell mode, where a cell can be defined using the double comment character ( ## ):

 ## main code class MyClass(object): ... def my_function(): ... ## testing code do_some_testing_please() 

All code from line ## -beginning until the next such line or the end of the file becomes a single cell. Whenever the cursor is inside a certain cell and you press a hotkey (Ctrl + Enter by default), the code inside that cell is executed in the current interpreter. An additional feature of the IEP is that the selected code can be executed with F9; a pretty standard feature, but the nice thing here is that the IEP will deal with spaces, so just selecting and pasting material from a method will work automatically.

+1
source

I suggest you use the right version control system to keep the “real” and “experimental” parts.

For example, using Git, you can only include real code without experimental parts in your commits (using add -p ), and then temporarily stash experimental parts to run various tools.

You can also save the experimental parts in your own branch, which then rebase on top of the non-experimental parts when you need them.

+1
source

Another possibility is to put the doctests tests in the docstrons of your code, which, admittedly, is only practical for simpler cases.

Thus, they are processed only as executable code by the doctest module, but as comments otherwise.

0
source

All Articles