[Tutor] design question

Knacktus knacktus at googlemail.com
Sat Sep 11 10:23:53 CEST 2010

Am 10.09.2010 16:11, schrieb Albert-Jan Roskam:
> Hi Jan,
> Here's a screendump of my program: http://nl.tinypic.com/r/2qtlojc/7 .
> This might make my description a little bit clearer. The beautiful
> sunset will in reality be a dull, handwritten form. ;-)
> Regarding the iterator pattern, I was referring to this:
> http://www.suttoncourtenay.org.uk/duncan/accu/pythonpatterns.html#iterators-and-generators
> Inside my program I have to keep a list of all the image files that are
> scheduled for data entry. The main purpose is to be able to read in the
> image files one by one. Another one of the purposes of this list is to
> show some information on the title bar. Currently, my program only has a
> 'next' button and the fact that implementing a 'previous' button is
> causing problems suggests to me that I have to look for a more
> fundamentally better solution.
Sounds to me that the easiest solution would be to simply use a list to 
hold the images, a reference to the current image and your desired 
methods in a class.

class ImageStack(object):

     def __init__(self, images=None)
         self.images = images or []
         self.current_image = None

     def next_image(self):
         if self.current_image:
             current_index = self.images.index(self.current_image)
                 next_image = self.images[current_index + 1]
             except IndexError:
                 print "All images done" # or return the first one
             next_image = self.images[0]
         self.current_image = next_image
         return next_image

     def prev_image(self):
         if self.current_image:
             current_index = self.images.index(self.current_image)
                 prev_image = self.images[current_index - 1]
                 self.current_image = next_image
                 return next_image
             except IndexError:
                 print "There's no prev image"
         print "There's no current image"

You could also track the current_index instead of the current_image. 
That would make the class easier, but how to track the current image 
depends on what other methods you need.



> Cheers!!
> Albert-Jan

More information about the Tutor mailing list