ipdb: how to continue until next file is reached - python

I'm debugging python code and I would find it very helpful if I can continue the execution of the program (just like using c) BUT break at the first executed line if a new file is being run. Is it possible?
The reason I want to do that is I want to skip a bunch of site packages and see exactly where the entrypoint to my own code is.

Related

Checking if a .cmd file was executed successfully with python

I wrote a script that executes certain .cmd files. I'm trying to find a way to check if the execution finished with errors or not. This is how the final line of the .cmd file looks like, it shows you the number of warnings and errors:
https://i.stack.imgur.com/y6K1m.png (sorry i do not have enough rep to make the image embedded between the text :()
I tried saving the console output to a variable, and then check if the substring "Error(s)" was inside the text, but that didn't seem to work... I'm fairly new to python and I'm running out of ideas and to stuff to try, any suggestions would be appreciated. Let me know if you need more details. Thanks in advance guys!
Batch scripts usually have a return code that you can check if it completed successfully.
Check this link for the exit codes
And check this question for the python code to get them

Debug Python starting on specific line, not at beginning, like VBA

I am using the Spyder IDE, version 3.3.1, and I would like to debug only a specific line of Python code that's about 1,100 lines into the program. I don't want to run all the code above line 1,100. Running those 1,100 lines would take time since it would execute various large database queries, and be unnecessary for my purposes.
In Visual Basic (VBA) for Excel, when debugging, I can drag the yellow arrow to any line and run the code line-by-line from that point forward. Is there something similar in Python (specifically in the Spyder IDE), or do I have to set break points and run the entire code?
I suppose one option is to move this specific line of code to the top of the file (i.e. line 1, not line 1,100), so that it runs first when debugging. Maybe I've answered my own question, but I'm still curious whether there's a way to start debugging from a certain line.
My options seem limited to the following, judging by Spyder's debug menu:
Run current line (i.e. starting at line 1)
Step into function or method of current line
Run until current function or method returns
Continue execution until next breakpoint
I don't want to reorganize all my code, since this is just an ad hoc/one-time analysis. For context, I am a business analyst and I am just doing some data exploration/correlation/regression analysis. I am a functional programmer whose background is in business/finance, not computer science.
It seems the easiest/quickest solution is to move the specific line of code to the top of the file (i.e. line 1, not line 1,100), so that it runs first when debugging.

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

Python, subprocesses and text file creation

Apologies if this kind of thing has been answered elsewhere. I am using Python to run a Windows executable file using subprocess.Popen(). The executable file produces a .txt file and some other output files as part of its operation. I then need to run another executable file using subprocess.Popen() that uses the output from the original .exe file.
The problem is, it is the .exe file and not Python that is controlling the creation of the output files, and so I have no control over knowing how long it takes the first text file to write to disk before I can use it as an input to the second .exe file.
Obviously I cannot run the second executable file before the first text file finishes writing to disk.
subprocess.wait() does not appear to be helpful because the first executable terminates before the text file has finished writing to disk. I also don't want to use some kind of function that waits an arbitrary period of time (say a few seconds) then proceeds with the execution of the second .exe file. This would be inefficient in that it may wait longer than necessary, and thus waste time. On the other hand it may not wait long enough if the output text file is very large.
So I guess I need some kind of listener that waits for the text file to finish being written before it moves on to execute the second subprocess.Popen() call. Is this possible?
Any help would be appreciated.
UPDATE (see Neil's suggestions, below)
The problem with os.path.getmtime() is that the modification time is updated more than once during the write, so very large text files (say ~500 Mb) require a relatively large wait time in between os.path.getmtime() calls. I use time.sleep() to do this. I guess this solution is workable but is not the most efficient use of time.
On the other hand, I am having bigger problems with trying to open the file for write access. I use the following loop:
while True:
try:
f = open(file, 'w')
except:
# For lack of something else to put in here
# (I don't want to print anything)
os.path.getmtime(file)
else:
break
This approach seems to work in that Python essentially pauses while the Windows executable is writing the file, but afterwards I go to use the text file in the next part of the code and find that the contents that were just written have been wiped.
I know they were written because I can see the file size increasing in Windows Explorer while the executable is doing its stuff, so I can only assume that the final call to open(file, 'w') (once the executable has done its job) causes the file to be wiped, somehow.
Obviously I am doing something wrong. Any ideas?
There's probably many ways to do what you want. One that springs to mind is that you could poll the modification time with os.path.getmtime(), and see when it changes. If the modification date is after you called the executable, but still a couple seconds ago, you could assume it's done.
Alternatively, you could try opening the file for write access (just without actually writing anything). If that fails, it means someone else is writing it.
This all sounds so fragile, but I assume your hands are somewhat tied, too.
One suggestion that comes to mind is if the text file that is written might have a recognizable end-of-file marker to it. I created a text file that looks like this:
BEGIN
DATA
DATA
DATA
END
Given this file, I could then tell if "END" had been written to the end of the file by using os.seek like this:
>>> import os
>>> fp = open('test.txt', 'r')
>>> fp.seek(-4, os.SEEK_END)
>>> fp.read()
'END\n'

Categories