473,395 Members | 2,713 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Resuming a program's execution after correcting error

Hi.

Does anyone know if one can resume a python script at the error point
after the error is corrected?
I have a large program that take forever if I have to restart from
scratch everytime. The error was the data writing a file so it seemed
such a waste if all the data was lost and must be recalculated again.

Sincerely,
Sheldon

Sep 28 '06 #1
13 1727
Sheldon wrote:
Hi.

Does anyone know if one can resume a python script at the error point
after the error is corrected?
I have a large program that take forever if I have to restart from
scratch everytime. The error was the data writing a file so it seemed
such a waste if all the data was lost and must be recalculated again.
As I said before, this can be done by finding out where the error is raised,
what the cause is and by inserting an appropriate try-except-statement in
the code.

There's no way to do that without modifying the code, if the program itself
doesn't offer such an option.

Georg
Sep 28 '06 #2
Sheldon wrote:
Does anyone know if one can resume a python script at the error point
after the error is corrected?
I have a large program that take forever if I have to restart from
scratch everytime. The error was the data writing a file so it seemed
such a waste if all the data was lost and must be recalculated again.
May be you could put some parameter to your main loop:

start_point=begin
if start_point==begin: output=open('outputfile', 'w')
else : output=open('outputfile', 'a')

while start_point <= case <= end_point:

try:
do_complex_computation(case)
except Exception:
print case
break

If you get an error you repair the program and set

start_point=case

and go on with the program.

Tuomas
Sep 28 '06 #3
Sheldon wrote:
Hi.

Does anyone know if one can resume a python script at the error point
after the error is corrected?
I have a large program that take forever if I have to restart from
scratch everytime. The error was the data writing a file so it seemed
such a waste if all the data was lost and must be recalculated again.
Afaik, the best you can do is run the script with "python -i", which
will give you an interactive prompt from which you have access to the
module-level variables. Or even better, test your script with small
data sets, and do the real calculations only after you've fixed any
obvious bugs.

Sep 28 '06 #4
Jay
I don't know how much help this is going to be, but you could store
values in a temporary file on the hard disk and write checkpoints to
read in the data and begin from a point somewhere in the middle of the
script.

Sheldon wrote:
Hi.

Does anyone know if one can resume a python script at the error point
after the error is corrected?
I have a large program that take forever if I have to restart from
scratch everytime. The error was the data writing a file so it seemed
such a waste if all the data was lost and must be recalculated again.

Sincerely,
Sheldon
Sep 28 '06 #5
Georg Brandl wrote:
As I said before, this can be done by finding out where the error is raised,
what the cause is and by inserting an appropriate try-except-statement in
the code.
I could be mistaken, but I *think* the OP is asking how to re-enter the
stack at the same point as the exception exited from and continue with
the execution as if the exception never happened. AFAIK, that isn't
possible; however, given that he has a file to work from that indicates
a portion of the state at the time of the exception, I think he may be
able simulate that kind of functionality by reading in the file on
exception and then returning a call to the function where the exception
occured with the data from the file. Something like this mockup:

def faulty_function(a, b, c=None):
if not c:
c = 0
try:
# modify c, write c to file...
# oops hit an exception
c += a / b
except:
# read from the file here
# c = ...
# and fix the error
b += 1
return faulty_function(a, b, c)
return c

print faulty_function(2, 0) # =2

Of course, it's probably much better to just fix the code and avoid the
exception in the first place. ;)

Regards,
Jordan

Sep 28 '06 #6

Sheldon wrote:
Hi.

Does anyone know if one can resume a python script at the error point
after the error is corrected?
I have a large program that take forever if I have to restart from
scratch everytime. The error was the data writing a file so it seemed
such a waste if all the data was lost and must be recalculated again.
You could modify the program while you're debugging it so that instead
of, say:

calculate data
write data

you have:

if saved data exists:
load data
else:
calculate data
save data
write data

The pickle module would be useful here.

Matthew

Sep 28 '06 #7

MonkeeSage wrote:
Georg Brandl wrote:
As I said before, this can be done by finding out where the error is raised,
what the cause is and by inserting an appropriate try-except-statement in
the code.

I could be mistaken, but I *think* the OP is asking how to re-enter the
stack at the same point as the exception exited from and continue with
the execution as if the exception never happened. AFAIK, that isn't
possible; however, given that he has a file to work from that indicates
a portion of the state at the time of the exception, I think he may be
able simulate that kind of functionality by reading in the file on
exception and then returning a call to the function where the exception
occured with the data from the file. Something like this mockup:

def faulty_function(a, b, c=None):
if not c:
c = 0
try:
# modify c, write c to file...
# oops hit an exception
c += a / b
except:
# read from the file here
# c = ...
# and fix the error
b += 1
return faulty_function(a, b, c)
return c

print faulty_function(2, 0) # =2

Of course, it's probably much better to just fix the code and avoid the
exception in the first place. ;)

Regards,
Jordan
Thanks Jordon,
I think you understood my problem best. I know now that this is not
possible but I would like to create an exception that saves all the
current variables when there is an error. I think pickle is the answer
but I never got it to work. My program is very large and it is being
modified often.
Any advice on how to save the variables.

/Sheldon

Oct 3 '06 #8

MRAB wrote:
Sheldon wrote:
Hi.

Does anyone know if one can resume a python script at the error point
after the error is corrected?
I have a large program that take forever if I have to restart from
scratch everytime. The error was the data writing a file so it seemed
such a waste if all the data was lost and must be recalculated again.
You could modify the program while you're debugging it so that instead
of, say:

calculate data
write data

you have:

if saved data exists:
load data
else:
calculate data
save data
write data

The pickle module would be useful here.

Matthew
I like your idea Matthew but I don't know how to pickle the many
variables in one file. Do I need to pickle each and every variable into
a seperate file?
var1,var2
pickle.dump(var1,f)
pickle.dump(var2,f2)

/Sheldon

Oct 3 '06 #9

Sheldon wrote:
MRAB wrote:
Sheldon wrote:
Hi.
>
Does anyone know if one can resume a python script at the error point
after the error is corrected?
I have a large program that take forever if I have to restart from
scratch everytime. The error was the data writing a file so it seemed
such a waste if all the data was lost and must be recalculated again.
>
You could modify the program while you're debugging it so that instead
of, say:

calculate data
write data

you have:

if saved data exists:
load data
else:
calculate data
save data
write data

The pickle module would be useful here.

Matthew

I like your idea Matthew but I don't know how to pickle the many
variables in one file. Do I need to pickle each and every variable into
a seperate file?
var1,var2
pickle.dump(var1,f)
pickle.dump(var2,f2)
Using the 'pickle' module:

# To store:
f = open(file_path, "wb")
pickle.dump(var1, f)
pickle.dump(var2, f)
f.close()

# To load
f = open(file_path, "rb")
var1 = pickle.load(f)
var2 = pickle.load(f)
f.close()

A more flexible alternative is to use the 'shelve' module. This behaves
like a dict:

# To store
s = shelve.open(file_path)
s["var1"] = "first"
s["var2"] = [2, 3]
s.close()

# To load
s = shelve.open(file_path)
print s["var1"] # This prints "first"
print s["var2"] # This prints [2, 3]
s.close()

Hope that helps
Matthew

Oct 3 '06 #10
On 3 Oct 2006 16:58:17 -0700, MRAB <go****@mrabarnett.plus.comwrote:
I like your idea Matthew but I don't know how to pickle the many
variables in one file. Do I need to pickle each and every variable into
a seperate file?
var1,var2
pickle.dump(var1,f)
pickle.dump(var2,f2)
Using the 'pickle' module:

# To store:
f = open(file_path, "wb")
pickle.dump(var1, f)
pickle.dump(var2, f)
f.close()

# To load
f = open(file_path, "rb")
var1 = pickle.load(f)
var2 = pickle.load(f)
f.close()

A more flexible alternative is to use the 'shelve' module. This behaves
like a dict:

# To store
s = shelve.open(file_path)
s["var1"] = "first"
s["var2"] = [2, 3]
s.close()

# To load
s = shelve.open(file_path)
print s["var1"] # This prints "first"
print s["var2"] # This prints [2, 3]
s.close()
As long as we're on the subject of data serialization, I should like
to bring up PyYaml. YAML is a portable format for storing data of all
kinds; it became popular via Ruby I think, but there are
implementations for many other languages. If you stick to storing
simple stuff like lists, strings, and dictionaries, you can use your
YAML data almost anywhere, but PyYaml even supports reifying things
like lambdas.
Oct 3 '06 #11

MRAB wrote:
Sheldon wrote:
MRAB wrote:
Sheldon wrote:
Hi.

Does anyone know if one can resume a python script at the error point
after the error is corrected?
I have a large program that take forever if I have to restart from
scratch everytime. The error was the data writing a file so it seemed
such a waste if all the data was lost and must be recalculated again.

You could modify the program while you're debugging it so that instead
of, say:
>
calculate data
write data
>
you have:
>
if saved data exists:
load data
else:
calculate data
save data
write data
>
The pickle module would be useful here.
>
Matthew
I like your idea Matthew but I don't know how to pickle the many
variables in one file. Do I need to pickle each and every variable into
a seperate file?
var1,var2
pickle.dump(var1,f)
pickle.dump(var2,f2)
Using the 'pickle' module:

# To store:
f = open(file_path, "wb")
pickle.dump(var1, f)
pickle.dump(var2, f)
f.close()

# To load
f = open(file_path, "rb")
var1 = pickle.load(f)
var2 = pickle.load(f)
f.close()

A more flexible alternative is to use the 'shelve' module. This behaves
like a dict:

# To store
s = shelve.open(file_path)
s["var1"] = "first"
s["var2"] = [2, 3]
s.close()

# To load
s = shelve.open(file_path)
print s["var1"] # This prints "first"
print s["var2"] # This prints [2, 3]
s.close()

Hope that helps
Matthew
Perfect Matthew!

Much obliged!

/Sheldon

Oct 4 '06 #12
Sheldon wrote:
MRAB wrote:
>Sheldon wrote:
>>MRAB wrote:
Sheldon wrote:
Hi.
>
Does anyone know if one can resume a python script at the error point
after the error is corrected?
I have a large program that take forever if I have to restart from
scratch everytime. The error was the data writing a file so it seemed
such a waste if all the data was lost and must be recalculated again.
>
Sorry if this is off-topic here but Dylan (http://www.opendylan.org) is
a nice language (I sometimes like even more than python itself) that
allows you to continue the work right where the exception was thrown.

Henning
Oct 5 '06 #13
Henning Hasemann wrote:
Sheldon wrote:
....
>>>>>Does anyone know if one can resume a python script at the error point
>after the error is corrected?
>I have a large program that take forever if I have to restart from
>scratch everytime. The error was the data writing a file so it seemed
>such a waste if all the data was lost and must be recalculated again.

Sorry if this is off-topic here but Dylan (http://www.opendylan.org) is
a nice language (I sometimes like even more than python itself) that
allows you to continue the work right where the exception was thrown.
As I have explained before in this newsgroup, Xerox Parc had that
ability (the ability to finish an exception by returning to its source)
in their system implementation language, and finally removed the
capability when they saw how many bugs were related to its use.

--Scott David Daniels
sc***********@acm.org
Oct 5 '06 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Bruce W...1 | last post by:
A scripting newbie question... I'm trying to understand some code I found. This script conducts a poll and writes the results to a text file. The following statement is part of the source file. ...
0
by: kajol | last post by:
Hi I am creating a setup project in VS.NET, I have "PluginInstaller.msi" installed in my computer, When I am running this setup program in a test machine where no .NET framework and MDAC2.7 is...
21
by: kimimaro | last post by:
Is there anymore methods in exiting your program using pure C language other than return 0?
4
by: Chris | last post by:
I posted this in the C# language group, then thought it might be more appropriate in this group. I would not cross-post except I want the answer so badly. I built small C# Web and Web Service...
0
by: mike | last post by:
Sound problems resuming from Hibernation I have an MP3 player that plays MP3s off DVD. Uses mci sendstring errreturn = mciSendString("play sound", rs, 128, cb) Been working great for...
14
by: Odinn | last post by:
Greetings , This is my first year at programming and 3rd week at this so count me as a pure newbie. I am trying to make a program that gets a number and prints out starts as this: For input 3 :...
8
by: Jothishankar | last post by:
Hi, I am new to c#. I am trying to build an application that does backup of files to an external hard disk. My application behaves strangely. When i run the application under debug mode (F5),...
4
by: Jerry West | last post by:
In VB6 one could use 'Resume Next' to have program execution resume at the statement following where an error occurred. Is there something comparable in VB .NET? Try i = 0 / 0 DoEvenMore
3
by: Sooraj | last post by:
Hi, I'm a mechanical engineer new to programming. I was trying to write a program in Visual Basic Express Edition 2005. I want to run a file using an application called madymo601 in command...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.