run python code from vim and keep python workspace - python

There are some ways to execute python code from within vim. E.g.:
typing ":! python %" on the command line! or
using the marvellous python-mode plugin!
These approaches invoke a new python instance, transfer the code, execute the code, feed back the output and close the instance. Thus, the python workspace will always be empty at start of execution. No possibility to access previously calculated results. When I want to execute just an incremental subset (some lines) of my code, I would have to take care of all the prerequisites as well (cf.: working with MATLAB provides an interactive mode where the workspace is always persistent).
Is there a way to keep a python instance alive (keep the objects within the workspace) and feed incremental code portions from vim into the open python instance and execute them with respect to the retained workspace?
One could think of storing / retrieving the python workspace on each exit / entry of a new python instance, but this might not be the best way to keep the workspace alive.

Related

How do I save and run a script I've altered in Pycharm?

I'm trying to learn some basics from an online course here so bear with me please?
I have an example script which i ran and this error;
the following arguments are required: source_ip, destination_ip
In my unknowing brain it means i need to actually add the ip addresses yes? It's kind of irrelevant really if I'm wrong I'm more interested in how I alter an existing script through Pycharm save it as a different name, then run that that script in my terminal which is gitbash?
So I opened the script in Pycharm and added the ip addresses. How do I save that script as a different name and then run it through pycharm?
If you select Run, Edit Configurations... on Pycharm top menu, you will see a screen to define the arguments used when you run your python script:
In the Parameters box you can enter the actual values of the parameters you want to use. I entered names in the example, but you should see the actual parameters values you would pass to the function from the command prompt.
That will pass the configured parameters to your script every time you run it in Pycharm.

When does python parse code files?

I am currently running evaluations with multiple parameter configurations in a medium sized project.
I set certain parameters and change some code parts and run the main file with python.
Since the execution will take several hours, after starting it I make changes to some files (comment out some lines and change parameter) and start it again in a new tmux session.
While doing this, I observed behaviour, where the first execution will use configuration options of the second execution, so it seems like python was not done parsing the code files or maybe lazy loads them.
Therefore I wonder how python loads modules / code files and if changing them after I started the execution will have an impact on the execution?

Python interactive library(pymidas) session warning

I'm using Python2.7 and a library called pymidas.
Within my python script I call the library with the following comand:
from pymidas import midas
midas.do('INDISK/FITS test.fits test.bdf')
All the code that I have further written does exactly what I want, but whenever the script imports midas I first get a welcome output of (py)midas, which is ok with me, but afterwards it asks me if I want a parallel or a new session.
Saddly this point needs human interaction in selecting parallel mode. By reading the documentation of midas I found, that midas has an option (-P) which causes exactly what I need, and forces midas to open without any questions asked and directly going to parallel mode.
Does anybody know how to achieve this in my python script?
Thanks!
At the end of your script add :
midas.do('.exit')
This ensures you dont get asked the next time you run the script.

Will changes made to a Python script affect another run in progress on the same file?

Suppose that I have put a Python script to run. Let's say while it is running, I open the source code and change the value of a variable to different value. Now, in another terminal if I start running the latest source code, what happens to the previous run that is progress?
Will it be get affected because of this latest change that I did while I was running it?
The thing is that I want to do parallel runs of the program for different values of a particular variable. Any better way to do this?
Python compiles your source into bytecode and runs that bytecode. Once the source file has been read it is no longer needed to run that bytecode. Changes to the source file won't then affect already running code.
However, if an exception is raised and Python tries to format a traceback for display, it'll reach back to the source code, mapping line markers in the bytecode back to source lines. If the source file changed after compilation, this could mean the wrong lines are being displayed. That can create confusion.
You can easily give your program command line parameters to vary how it behaves. Take a look at the sys.argv list, and perhaps the argparse module for more complex command-line option handling. That way your code remains stable and flexible.
Python typically compiles the source code to a *.pyc file. Changing the value in the script usually won't affect the value already in memory.
The better way to do this is take an argument from argv
python your_script.py value
You can access it with
import sys
sys.argv[1] #this is the 'value' from the command line

Alternate between Executing a MATLAB file and a Python script

I have a MATLAB file that currently saves its variables into a .mat workspace. The python script uses SciPy.io to read these variables from the workspace. The python script performs some operations & resaves variables into a MATLAB workspace(agin using Scipy.io) which matlab should then reopen. I'm using MATLABR2013a and I dont think there's an easy way to run the python script from within the .m file itself.
There may be an easier way then the method I'm going about doing it but my current plan is to create a bash script that runs the matlab file and only proceeds to the latter section if a random variable (stored in another file) is of a certain value. The script then calls the python script, sets the random variable to a different (can view as a sort of boolean). The matlab script will then execute the second section but not the first section. I need to have about 5 or 6 such exclusive sections however and it's easier to have them all in the same .m file than it is to separate them
This seems tedious however when all I really want is a way to have the system pause the matlab script, run the python script and come back to that spot in the matlab script.
Appreciate all creative suggestions to make this workflow as efficient as possible and easy to modify
MATLAB code detailed below
I saved the workspace using MATLAB's save function
Used MATLAB's system() function to execute the python script.
Within python, used scipy.iosavemat to save variables I wanted to access in matlab
Used MATLAB's load function to load the variables from python back into matlab's workspace
writeto=['insert path to save to here']
save(writeto)
first_Pypath=['insert path of python script here']
py_call=horzcat('python ',first_Pypath);
system(py_call);

Categories