开发者

How to create an increasing counter of ranges?

开发者 https://www.devze.com 2023-02-26 21:01 出处:网络
In a script I\'m running, there is a memory leak issue (due to the propr开发者_C百科ietary software being used) that prevents a script for running for more than about 325k iterations. To get around th

In a script I'm running, there is a memory leak issue (due to the propr开发者_C百科ietary 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.


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*))


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).

0

精彩评论

暂无评论...
验证码 换一张
取 消