[Tutor] Long and Nasty List Comprehensions... for fun.

israel at uandmedance.com israel at uandmedance.com
Thu Jul 15 00:13:04 CEST 2004

I've recently cooked up a very long list comprehension (1027  
characters!) out of sheer perversity that creates a simple html image  
gallery script.

It's got a few quirks like requiring a specific directory structure,  
specific file types, as well as asking for the desired base directory  

I was wondering if any of you had any tips for improving this  
monstrosity of mine.
I'd also be interested in seeing any of your absurdly long one liners.  
I know I'm only an artist posing as a programmer, so I feel assured  
there are other out there who are far more insane than me.

I think this exercise has tempered my love for list comprehensions into  
something a little more manageable.
debugging something this long and all on one line grew to be a big  
pain, fun though it was. :)


import os

'''This Super Long List Comprehension Simple and Ugly Image Gallery
requires you to have a directory structure as follows

root directory
     |--Any Amount of Directories set up like so.  ( filled with the  
full sized .png images. )
            |--Thumbnail directory called 'thumb'  ( filled with small  
identically named thumbnail .jpg images )

You merely cd to the directory and call 'python gallery.py' and answer  
the two
requests for the directory you want to use.  Usually this is './'

The resulting html page will look for two stylesheets.
They aren't provided, but you can use them to style your fabulous new
image gallery script.

if __name__ == '__main__':
	file(os.path.join(raw_input('What is the base  
directory?'),'gallery.html'),'w').write('<!DOCTYPE html\n\tPUBLIC  
"-//W3C//DTD XHTML 1.0  
strict.dtd">\n\n<html xmlns="http://www.w3.org/1999/xhtml"  
xml:lang="en" lang="en">\n<head>\n<title>Simple, Ugly and Super Long  
List Comprehension Image Gallery</title>\n<link rel="stylesheet"  
href="base.css" />\n<style type="text/css" media="all">\n\t at import  
"null?\"\{";\n\t at import  
id="%s" class="directory">\n%s\n</div>\n' %  
(os.path.basename(dirname[0]),'\n'.join(['<a href="%s" title="%s"><img  
src="%s" alt="%s"/></a>' %  
4],os.path.join(dirname[0],'thumb',image[:-4]+'.jpg'),image[:-4]) for  
image in dirname[2] if image[0] not in ['.'] and image[-4:] in  
['.png','.jpg']])) for dirname in os.walk(raw_input('What is the base  
directory?')) if os.path.basename(dirname[0]) not in ['thumb'] and  
dirname[2] ])+'\n</body>\n</html>')

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: text/enriched
Size: 3066 bytes
Desc: not available
Url : http://mail.python.org/pipermail/tutor/attachments/20040714/443ab1d1/attachment.bin

More information about the Tutor mailing list