Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7685429
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T19:14:58+00:00 2026-05-31T19:14:58+00:00

So I’ve wrote a small script to download pictures from a website. It goes

  • 0

So I’ve wrote a small script to download pictures from a website. It goes through a 7 alpha charactor value, where the first char is always a number. The problem is if I want to stop the script and start it up again I have to start all over.

Can I seed itertools.product somehow with the last value I got so I don’t have to go through them all again.

Thanks for any input.

here is part of the code:

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)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T19:14:59+00:00Added an answer on May 31, 2026 at 7:14 pm

    here’s a solution based on pypy’s library code (thanks to agf’s suggestion in the comments).

    the state is available via the .state attribute and can be reset via .goto(state) where state is an index into the sequence (starting at 0). there’s a demo at the end (you need to scroll down, i’m afraid).

    this is way faster than discarding values.

    > cat prod.py 
    
    class product(object):
    
        def __init__(self, *args, **kw):
            if len(kw) > 1:
                raise TypeError("product() takes at most 1 argument (%d given)" %
                                 len(kw))
            self.repeat = kw.get('repeat', 1)
            self.gears = [x for x in args] * self.repeat
            self.num_gears = len(self.gears)
            self.reset()
    
        def reset(self):
            # initialization of indicies to loop over
            self.indicies = [(0, len(self.gears[x]))
                             for x in range(0, self.num_gears)]
            self.cont = True
            self.state = 0
    
        def goto(self, n):
            self.reset()
            self.state = n
            x = self.num_gears
            while n > 0 and x > 0:
                x -= 1
                n, m = divmod(n, len(self.gears[x]))
                self.indicies[x] = (m, self.indicies[x][1])
            if n > 0:
                self.reset()
                raise ValueError("state exceeded")
    
        def roll_gears(self):
            # Starting from the end of the gear indicies work to the front
            # incrementing the gear until the limit is reached. When the limit
            # is reached carry operation to the next gear
            self.state += 1
            should_carry = True
            for n in range(0, self.num_gears):
                nth_gear = self.num_gears - n - 1
                if should_carry:
                    count, lim = self.indicies[nth_gear]
                    count += 1
                    if count == lim and nth_gear == 0:
                        self.cont = False
                    if count == lim:
                        should_carry = True
                        count = 0
                    else:
                        should_carry = False
                    self.indicies[nth_gear] = (count, lim)
                else:
                    break
    
        def __iter__(self):
            return self
    
        def next(self):
            if not self.cont:
                raise StopIteration
            l = []
            for x in range(0, self.num_gears):
                index, limit = self.indicies[x]
                l.append(self.gears[x][index])
            self.roll_gears()
            return tuple(l)
    
    p = product('abc', '12')
    print list(p)
    p.reset()
    print list(p)
    p.goto(2)
    print list(p)
    p.goto(4)
    print list(p)
    > python prod.py 
    [('a', '1'), ('a', '2'), ('b', '1'), ('b', '2'), ('c', '1'), ('c', '2')]
    [('a', '1'), ('a', '2'), ('b', '1'), ('b', '2'), ('c', '1'), ('c', '2')]
    [('b', '1'), ('b', '2'), ('c', '1'), ('c', '2')]
    [('c', '1'), ('c', '2')]
    

    you should test it more – i may have made a dumb mistake – but the idea is quite simple, so you should be able to fix it :o) you’re free to use my changes; no idea what the original pypy licence is.

    also state isn’t really the full state – it doesn’t include the original arguments – it’s just an index into the sequence. maybe it would have been better to call it index, but there are already indici[sic]es in the code…

    update

    here’s a simpler version that is the same idea but works by transforming a sequence of numbers. so you just imap it over count(n) to get the sequence offset by n.

    > cat prod2.py 
    
    from itertools import count, imap
    
    def make_product(*values):
        def fold((n, l), v):
            (n, m) = divmod(n, len(v))
            return (n, l + [v[m]])
        def product(n):
            (n, l) = reduce(fold, values, (n, []))
            if n > 0: raise StopIteration
            return tuple(l)
        return product
    
    print list(imap(make_product(['a','b','c'], [1,2,3]), count()))
    print list(imap(make_product(['a','b','c'], [1,2,3]), count(3)))
    
    def product_from(n, *values):
        return imap(make_product(*values), count(n))
    
    print list(product_from(4, ['a','b','c'], [1,2,3]))
    
    > python prod2.py 
    [('a', 1), ('b', 1), ('c', 1), ('a', 2), ('b', 2), ('c', 2), ('a', 3), ('b', 3), ('c', 3)]
    [('a', 2), ('b', 2), ('c', 2), ('a', 3), ('b', 3), ('c', 3)]
    [('b', 2), ('c', 2), ('a', 3), ('b', 3), ('c', 3)]
    

    (the downside here is that if you want to stop and restart you need to have kept track yourself of how many you have used)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I used javascript for loading a picture on my website depending on which small
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I have a text area in my form which accepts all possible characters from
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have an MVC Razor view @{ ViewBag.Title = Index; var c = (char)146;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.