I have an python 2.7 application that I am occasionally interrupting.
I am processing a very large data file. To deal with memory constraints, I have divided up the data file into grids that are identified by an x and y components. Each grid is processed independently.
It takes a very long time to process so occasionally, I need to stop the processing at a certain point. Ideally, I would like to update the y_start and x_start and resume the application at the place I left off (without processing a grid that had already been processed).
The main action occurs within a nested for-loop:
x_start=0.0
x_step=0.05
x_size=10.0
y_start=0.0
y_step=0.05
y_size=10.0
x_ranges = zip(np.arange(x_start,x_size,x_step), np.arange(x_step+x_start,x_size+x_step,x_step))
y_ranges = zip(np.arange(0.0,y_size,y_step), np.arange(y_step,y_size+y_step,y_step))
for x_min,x_max in x_ranges:
for y_min,y_max in y_ranges:
doAction()
In the code above, I have the x_start handled. y_start should only be used for when x_min = x_start. For all other values of x, it should start at 0.0.
Here's my proposed solution. Is there a better, more pythonic way:
y_ranges_resume = zip(np.arange(y_start,y_size,y_step),np.arange(y_start+y_step,y_size+y_step,y_step)
for x_min,x_max in x_ranges:
if x_min == x_start:
for y_min,y_max in y_ranges_resume:
doAction()
else:
for y_min,y_max in y_ranges:
doAction()
I'm not sure about a more pythonic way, but you could rewrite it like this (in any language really):
y_ranges_resume = zip(np.arange(y_start,y_size,y_step),np.arange(y_start+y_step,y_size+y_step,y_step)
for x_min,x_max in x_ranges:
y_ranges_used = y_ranges
if x_min == x_start:
y_ranges_used = y_ranges_resume
for y_min,y_max in y_ranges_used:
doAction()
At least then the inner loop is only written once.
Alternatively you could use the ternary, but I error on the side of easier to read rather than smaller code. But for completeness sake you could write the same thing this way:
y_ranges_resume = zip(np.arange(y_start,y_size,y_step),np.arange(y_start+y_step,y_size+y_step,y_step)
for x_min,x_max in x_ranges:
for y_min, y_max in y_ranges_resume if x_min == x_start else y_ranges:
doAction()
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments