n00bie wants advice.

A.T.Hofkamp hat at se-162.se.wtb.tue.nl
Wed Jul 2 03:56:03 EDT 2008


On 2008-07-02, bsagert at gmail.com <bsagert at gmail.com> wrote:
> This simple script writes html color codes that can be viewed in a
> browser.  I used short form hex codes (fff or 000, etc) and my list
> has only six hex numbers otherwise the results get rather large. I
> invite criticism as to whether my code is "pythonic". Are there other
> ways to generate the hex combos besides the nested "for" loops? Thanks
> in advance, Bill

ok.

variable names of 1 letter are very bad. Use more meaningful names like 'red'
'green' etc.

'list' is better, but also a name reserved by Python, so change that too.

Indenting is normally 4 spaces in Python

You can see "a + b +c" twice, compute it once, and assign it to a intermediate
variable

Use string formatting for better readability.

In this case, you could also open the file earlier, and write all strings
directly to file instead of first creating a string in memory

Otherways of creating the colour code permutations: In this case, this is most
Pythonic imho. You could write a list comprehension of even a recursive
function, but I think it wouldn't increase readability.


Albert


> list = ['3','6','9','b','d','f']
>
> s = '<html><head><style>h1{margin:0}</style></head><body>\n'
>
> for a in list:
> 	for b in list:
> 		for c in list:
> 			s += '<h1 style="background:#'+ a + b + c +'">'+ a + b + c +'</h1>
> \n'
>
> s += '</body></html>'
>
> f = open('c:/x/test.htm', 'w')
> f.write(s)
> f.close()



More information about the Python-list mailing list