[Tutor] design question

Steven D'Aprano steve at pearwood.info
Sat Sep 11 10:40:17 CEST 2010

On Sat, 11 Sep 2010 12:11:37 am Albert-Jan Roskam wrote:

> Inside my program I have to keep a list of all the image files that
> are scheduled for data entry. 

Sounds like you need to keep a list of all the image files that are 
scheduled for data entry then.

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

No it isn't. You don't need a list of image files in order to show "some 
information" in the title bar (unless that information is the list of 
files). From your earlier post, you want to show:

filename 23 of 245 (9.4%)

or similar. For that, all you need is three pieces of information:

* the name of the current file
* the number of the current file
* the total number of files

You don't need all 245 files for that.

I'm being pedantic here. Obviously you need the list of files 
*somewhere*, and you need it for other reasons, but you don't *need* a 
list of 243 files in order to show the name of one of them!

You may decide that it is *convenient* to give the function which 
changes the title bar access to the entire list, but it's not a 
*requirement*. Think about alternatives: 

def change_titlebar(name, count, total):
    template = "%s %d of %d (%.1f %%)"
    percentage = count*100.0/total
    title = template % (name, count, total, percentage)
    do_magic_with(title)  # whatever it takes to change the title

It doesn't need the entire list.

> 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 you *don't* want the iterator pattern. The iterator 
pattern generally means that you step forward through each item. If you 
want to advance backwards or forwards, something with random access is 
probably better. That would probably mean a list.

Don't be too hung up about "design patterns". Most design patterns just 
exist to work around limitations of the language, lack of power, or 
slavish devotion to object oriented programming when the problem is 
crying out for a functional or procedural solution. Don't be afraid to 
write functions instead of classes -- there's no need for a 
TitlebarManipulator class just to change the titlebar.

My solution would be to keep *two* pieces of information:

* the list of filenames;
* the current position in that list

Set the current position to 0, and have the Next button add one to the 
position and refresh the page, and Prev subtract one and refresh the 
page. Obviously you need code to ensure that the position doesn't go 
out of bounds, code to save the data in the fields, and so forth. The 
page refresh code should do something like:

* given the current position, extract the filename to use;
* change the title bar;
* open and display the appropriate image;
* pre-populate any fields;
Good luck!

Steven D'Aprano

More information about the Tutor mailing list