所以我写了一个小脚本从一个网站下载图片。它通过一个7个alpha字符值,其中第一个字符总是一个数字。问题是如果我想停止脚本并重新启动它,我必须从头开始。在
我能播种吗itertools.product不知怎么的,我得到了最后一个值,所以我不必再重复一遍了。在
谢谢你的意见。在
以下是部分代码:
numbers = '0123456789'
alnum = numbers + 'abcdefghijklmnopqrstuvwxyz'
len7 = itertools.product(numbers, alnum, alnum, alnum, alnum, alnum, alnum) # length 7
for p in itertools.chain(len7):
currentid = ''.join(p)
#semi static vars
url = 'http://mysite.com/images/'
url += currentid
#Need to get the real url cause the redirect
print "Trying " + url
req = urllib2.Request(url)
res = openaurl(req)
if res == "continue": continue
finalurl = res.geturl()
#ok we have the full url now time to if it is real
try: file = urllib2.urlopen(finalurl)
except urllib2.HTTPError, e:
print e.code
im = cStringIO.StringIO(file.read())
img = Image.open(im)
writeimage(img)
一旦你对迭代器有了一个合理的方法,使用dropwhile将需要一段时间才能到达目的地。在
您可能应该调整一个像this这样的配方,这样就可以在运行之间用pickle保存状态。在
请确保您的脚本一次只能运行一次,否则您将需要一些更复杂的东西,例如将id分发给脚本的服务器进程
这里有一个基于pypy库代码的解决方案(感谢agf在评论中的建议)。在
状态通过
.state
属性可用,并且可以通过.goto(state)
重置,其中state
是序列的索引(从0开始)。最后有一个演示(恐怕你需要向下滚动)。在这比丢弃值快得多。在
你应该对它进行更多的测试——我可能犯了一个愚蠢的错误——但是这个想法很简单,所以你应该能够修复它:o)你可以自由使用我的更改;不知道原始的pypy许可是什么。在
而且
state
不是真正的完整状态-它不包括原始参数-它只是序列的一个索引。也许把它称为索引会更好,但代码中已经有了标记。。。在更新
这里有一个更简单的版本,它的想法是一样的,但是通过转换一系列的数字来工作。所以你只要
^{pr2}$imap
在count(n)
上,就可以得到n
的序列偏移量。在(这里的缺点是,如果您想停止并重新启动,您需要跟踪自己已经使用了多少次)
如果您的输入序列没有任何重复的值,这可能比}快,因为它不需要您通过计算正确的点来比较所有被删除的值来继续迭代。在
dropwhile
前进{相关问题 更多 >
编程相关推荐