Re: n00bie wants advice.

2008-07-02 Thread oj
On Jul 2, 7:25 am, [EMAIL PROTECTED] 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
>
> list = ['3','6','9','b','d','f']
>
> s = 'h1{margin:0}\n'
>
> for a in list:
>         for b in list:
>                 for c in list:
>                         s += ''+ a + 
> b + c +'
> \n'
>
> s += ''
>
> f = open('c:/x/test.htm', 'w')
> f.write(s)
> f.close()

You could write the loop like this:

for red, green, blue in [(r, g, b) for r in list for g in list for b
in list]:
s += blah blah blah

but, arguably, that isn't easier to read or understand. It's a matter
of taste, I guess.

As has already been mentioned, list is not a good name, because it is
already used.

Also, personally, I find it easier to read strings that aren't
constructed with concatenation, but using pythons string formatting
gubbins:

'' % (red, green, blue)

Again, I think this is mostly personal preference.
--
http://mail.python.org/mailman/listinfo/python-list


Re: n00bie wants advice.

2008-07-02 Thread A.T.Hofkamp
On 2008-07-02, [EMAIL PROTECTED] <[EMAIL PROTECTED]> 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 = 'h1{margin:0}\n'
>
> for a in list:
>   for b in list:
>   for c in list:
>   s += ''+ a + b 
> + c +'
> \n'
>
> s += ''
>
> f = open('c:/x/test.htm', 'w')
> f.write(s)
> f.close()
--
http://mail.python.org/mailman/listinfo/python-list


Re: n00bie wants advice.

2008-07-02 Thread Marc 'BlackJack' Rintsch
On Tue, 01 Jul 2008 23:25:53 -0700, bsagert 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".

You should not rebind the name `list` because it shadows the built in type
of that name then.  A more descriptive name would be nice anyway, i.e.
`hex_digits`.  And strings are iterable too, so it's a bit shorter and
easier to type the digits a string.

Repeatedly concatenating strings with ``+=`` might be performance problem.
Python strings are immutable so this operation has to copy the involved
and growing strings over and over again.  Although the current CPython
implementation can optimize here in some cases, the usual idiom is to use
the `join()` method of strings to build a string from components in a list
or iterable.

Alternative implementation of your script:

from __future__ import with_statement

def main():
html_template = ('h1{margin:0}\n'
 '%s\n'
 '\n')
header_template = '%s'
hex_digits = '369bdf'
colors = (a + b + c for a in hex_digits
for b in hex_digits
for c in hex_digits)
html = html_template % '\n'.join(header_template % (c, c) for c in colors)
with open('test.html', 'w') as html_file:
html_file.write(html)

if __name__ == '__main__':
main()
--
http://mail.python.org/mailman/listinfo/python-list


n00bie wants advice.

2008-07-01 Thread bsagert
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

list = ['3','6','9','b','d','f']

s = 'h1{margin:0}\n'

for a in list:
for b in list:
for c in list:
s += ''+ a + b 
+ c +'
\n'

s += ''

f = open('c:/x/test.htm', 'w')
f.write(s)
f.close()
--
http://mail.python.org/mailman/listinfo/python-list