Nathanus
Nathanus

Reputation: 267

How to create an increasing counter of ranges?

In a script I'm running, there is a memory leak issue (due to the proprietary software being used) that prevents a script for running for more than about 325k iterations. To get around this, I created a list of values for it to iterate through in a for loop like so:

squery = 
['OBJECTID >=0 AND OBJECTID <=300000',
'OBJECTID >=300001 AND OBJECTID <=600000',
'OBJECTID >=600001 AND OBJECTID <=900000',
....
'OBJECTID >=8700001 AND OBJECTID <=8766184']

for query in squery:
    do work

Is there a method to create these ranges in 300k intervals so that the list can be built at runtime with a intervals until the value reaches X? As in, 3 intervals for 900k records, and 5 intervals for 1.5 million records.

Upvotes: 0

Views: 568

Answers (2)

Henry Gomersall
Henry Gomersall

Reputation: 8692

Just for completeness, a generator would also solve this problem nicely:

def generator_function(n):
    for i in range(0,n):
        yield ('OBJECTID>=%s AND OBJECTID<=%s' % (i*300000+(i!=0),(i+1)*300000))

# now use it:
for each_string in generator_function(3):
    print each_string

This method generates each string on the fly, so in some situations might be preferable (for example if you want to generate a huge number of strings rather than just 5 - in which case itertools is a good companion to generators).

Upvotes: 1

manji
manji

Reputation: 47978

squery =
['OBJECTID>=%s AND OBJECTID<=%s' % (i*300k+(i!=0),(i+1)*300k) for i in range(0,3)]

(300k = 300 000 and number of intervals is in range(0,*3*))

Upvotes: 4

Related Questions