How to run a "section" of a python script, like Matlab - python

When using vim (or any other text editor) for python scripts, is there a way to run a section of your script alone?
Matlab has a feature where beginning a line with %% splits the lines of code that follow into a section of its own, that can be run independently. Is there a way of achieving this without having to use iPython? Even if the solution is a bit hacky, it would be an improvement over what I currently am doing, which is commenting out blocks that I don't want to run.

A generic solution would be vim-slime: https://github.com/jpalardy/vim-slime
SLIME is an Emacs plugin to turn Emacs into a Lisp IDE. You can type
text in a file, send it to a live REPL, and avoid having to reload all
your code every time you make a change.
Vim-slime is a humble attempt at getting some of these features into
Vim. It works with any REPL and isn't tied to Lisp.

Related

Execute Python code line by line, like VBA?

I'm new to python and I'm having fun. So far I've only been on the road in VBA and SQL, but one thing bothers me. Is there no feature that goes through the code line by line like in VBA? This has always helped me a lot with VBA (F8), I could check the value of the variable on the fly (hold mouseover), i could check the whole code better and see where exactly it is running on a bug. Is this function not really available in python? I use PyCharm as IDE
Thanks!
You may try the following command: python -m pdb <script.py>. It will run the script in the Python debugger where you can traverse your code step by step.
There is nothing like VBA/VB6 IDE for python, unfortunately. Microsoft's IDE is simply unmatched in terms of debugging convenience. As a language, Python is more powerful, but good debugging solution is yet to come, you can't chance lines execution order or alter code on the fly in the debugger.

How to Auto-format Code in Python in NPP or Online

I do most of my Python coding in Notepad++. I would like a way to quickly and easily reformat my code using the default rules from PyCharm - things like spacing, number of blank lines between functions etc.
I find black playground too harsh for my requirements, and opening up PyCharm to paste code and reformat it defeats to purpose of using Notepad++ (for it's lightness).
So do you know of either a plugin for Notepad++ which does this (and I'm not just referring to Python Indent), or an online resource which applies PyCharm style formatting to pasted code?
PyCharm comes with a CLI, like documented here https://www.jetbrains.com/help/pycharm/command-line-formatter.html.
Alternatively and making the call slightly shorter: parallel to the mentioned there general batch file, you probably also have 'format.bat'. You can integrate this into Notepad++ via the [F5] button "Run...":
Put there
C:\__YourPyCharmRoot__\bin\format.bat "$(FULL_CURRENT_PATH)"
and press the [Save] button in this "Run..." dialog to assign a short-cut.
Pressing the newly defined short-cut on a saved and writable Python file should let Notepad++ ask you after a while (of running PyCharm in Windows cmd.exe) to reload the changed file .

iteratively and interactively writing and testing python code

I'd like to iteratively write and test my python code in the Python console. I can't find a way to easily load what i've got written in the editor (somefile.py) into the Python console in Pycharm. Is there a trick to doing so?
Pycharm will let me run the entire script but that's not useful because i want to build up my state in the shell by experimenting with the functions and data i've got in the environment (kind of like how a lisp programmer would use a REPL).
Pycharm have this alt-shift-E option.
This way you can write in the editor, select the lines you want to run and run them by alt-shift-E.
I use it all the time, although I find a lot of people not fameliar with this option.

Python Workflow like RStudio?

I use Python as my main programming language, sometimes switching to R for quick data-analysis tasks due to it’s huge library for statistical programming purposes (although pandas for python is great!). I guess that most R-Users go for R-Studio, which is like an IDE for R. There are probably hundreds of IDE’s for Python, but i’d like to stick with VIM and IPython. Although there is the vim-ipython-plugin, i can’t recreate the R-Studio workflow, which is mainly:
Write code in the source-editor
"push" or execute it within the console window via CTRL-Enter
after that, the results get displayed in the console window or the plotting window
When using the vim-ipython plugin, you can execute vim-lines via CTRL-S, but the results in the running IPython-Kernel/QTconsole are not updating. Instead, the output goes directly to a VIM window (<leader>s opens it, but the window closes all the time and isn´t able to display inline-graphics, like the ipython-qtconsole does).
I’m sure this isn’t a new "problem", but is there any setup for VIM and (I)Python, which mimics the RStudio behavior and integrates both the editor and the interactive interpreter like modern IDE’s do? Copying text from vim to the interactive interpreter can’t be the optimal solution, right?
The canonical way of doing this in vim is to run vim inside a gnu-screen or tmux session with the REPL in a second screen in the session. You can then use vim-slime to send code from vim to the REPL.

Vim for python and R scripting (in Screen or not)

I've been trying to find this information online but I'm not getting the answer.
I've used RStudio and Geany for editing files before. Now I'm trying to use ViM to edit python and R files (I know there's RPy, but nothing to do with my problem).
I would like to know how can I have 3 terminals (could also be vim buffers, or screen windows) with one running ViM and the others running R and Python. When I execute a Python script, the terminal (window or buffer) with python shows the output. The same when I run R scripts.
I would appreciate insight on this as this is something that's keeping me from using ViM regularly. I would also consider a solution with terminator terminal multiplexer or guake terminal. Any information about sending code for scripting from one instance to another is welcome.
Are you looking for a way to have a REPL inside Vim? If so, Vim wasn't really designed with that in mind, though there are some plugins that try. Conque is an example.
Some things I use to have a quicker code/run/test iteration with Python:
IPython's %edit feature, which starts editing a script with $EDITOR and will run the script after you exit.
vim-ipython which can send/execute/recieve code via an IPython interpreter.
tmux which allows you to have multiple shells side by side, but with little interaction between them.
Vim-slime is a general-purpose solution to this I'm pretty happy about, it will send blocks of code to any tmux pane, meaning it works for any language.
https://github.com/jpalardy/vim-slime
Your requirements for online information may not have been spelled out in enough detail, since I seem to find a wealth of information on using ViM as an IDE for both R and Python:
R:
http://www.r-bloggers.com/r-with-vim/
http://www.vim.org/scripts/script.php?script_id=2628
http://www.vim.org/scripts/script.php?script_id=1048
Python:
http://wiki.python.org/moin/Vim
http://blog.dispatched.ch/2009/05/24/vim-as-python-ide/
http://dancingpenguinsoflight.com/2009/02/python-and-vim-make-your-own-ide/
Have a look at vim-ipython, a plug-in for Vim.
You need to download the source (linked above), and run the Vim command :source path/to/file/ipy.vim.
Start by running a new IPython session (e.g. using IPython qtconsole or IPython notebook) and then type :IPython into Vim. Your Vim is now connected to the IPython instance you just opened.
You can press F5 to run the whole python script in your Vim, or Ctrl+s to run the current line. Ctrl+s will also run whatever is selected if you're in visual (i.e. 'select') mode.

Categories