Re: Can I replace this for loop with a join?

2009-06-22 Thread Ulrich Eckhardt
Ben Finney wrote:
 Paul Watson paul.hermeneu...@gmail.com writes:
 On Mon, 2009-04-13 at 17:03 +0200, WP wrote:
  dict = {1:'astring', 2:'anotherstring'}
  for key in dict.keys():
   print 'Press %i for %s' % (key, dict[key])
 
 In addition to the comments already made, this code will be quite
 broken if there is ever a need to localize your package in another
 language.
 
 How is this code especially broken? AFAICT, it merely needs the strings
 marked for translation, which is the best i18n situation any regular
 program can hope for anyway.
 

The problem is that some language might require you to write For X press
Y, which is impossible to achieve here. I think percent-formatting
supports using keys, like

  Press %{key} for %{action} % {'key': key, 'action':dict[key]}

However, I would also like to hear what Paul really meant and also the
alternatives he proposes.

Uli


-- 
Sator Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Can I replace this for loop with a join?

2009-06-22 Thread Paul Rubin
WP no.i.d...@want.mail.from.spammers.com writes:
 I could do it like this:
 dict = {1:'astring', 2:'anotherstring'}
 for key in dict.keys():
  print 'Press %i for %s' % (key, dict[key])
 Press 1 for astring
 Press 2 for anotherstring

Note that dict.keys() will return the keys in random order.

 but can I use a join instead?

  print '\n'.join('Press %s for %s' for (k,v) in sorted(dict.iteritems()))

(untested) should print them in order.  Yes it is ok to use %s to
format integers.

Note: normally you should not call your dictionary 'dict', since
'dict' is a built-in value and overriding it could confuse people
and/or code.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Can I replace this for loop with a join?

2009-06-21 Thread Paul Watson
On Mon, 2009-04-13 at 17:03 +0200, WP wrote:
 Hello, I have dictionary {1:astring, 2:anotherstring, etc}
 
 I now want to print:
 Press 1 for astring
 Press 2 for anotherstring etc
 
 I could do it like this:
 dict = {1:'astring', 2:'anotherstring'}
 for key in dict.keys():
  print 'Press %i for %s' % (key, dict[key])
 
 Press 1 for astring
 Press 2 for anotherstring
 
 but can I use a join instead?
 
 Thanks for any replies!
 
 - WP

In addition to the comments already made, this code will be quite broken
if there is ever a need to localize your package in another language.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Can I replace this for loop with a join?

2009-06-21 Thread Ben Finney
Paul Watson paul.hermeneu...@gmail.com writes:

 On Mon, 2009-04-13 at 17:03 +0200, WP wrote:
  dict = {1:'astring', 2:'anotherstring'}
  for key in dict.keys():
   print 'Press %i for %s' % (key, dict[key])
 
 In addition to the comments already made, this code will be quite
 broken if there is ever a need to localize your package in another
 language.

How is this code especially broken? AFAICT, it merely needs the strings
marked for translation, which is the best i18n situation any regular
program can hope for anyway.

-- 
 \   “Crime is contagious… if the government becomes a lawbreaker, |
  `\  it breeds contempt for the law.” —Justice Louis Brandeis |
_o__)  |
Ben Finney
-- 
http://mail.python.org/mailman/listinfo/python-list


Can I replace this for loop with a join?

2009-04-13 Thread WP

Hello, I have dictionary {1:astring, 2:anotherstring, etc}

I now want to print:
Press 1 for astring
Press 2 for anotherstring etc

I could do it like this:
dict = {1:'astring', 2:'anotherstring'}
for key in dict.keys():
print 'Press %i for %s' % (key, dict[key])

Press 1 for astring
Press 2 for anotherstring

but can I use a join instead?

Thanks for any replies!

- WP
--
http://mail.python.org/mailman/listinfo/python-list


Re: Can I replace this for loop with a join?

2009-04-13 Thread Kushal Kumaran
On Mon, Apr 13, 2009 at 8:33 PM, WP
no.i.d...@want.mail.from.spammers.com wrote:
 Hello, I have dictionary {1:astring, 2:anotherstring, etc}

 I now want to print:
 Press 1 for astring
 Press 2 for anotherstring etc

 I could do it like this:
 dict = {1:'astring', 2:'anotherstring'}
 for key in dict.keys():
    print 'Press %i for %s' % (key, dict[key])

 Press 1 for astring
 Press 2 for anotherstring

 but can I use a join instead?


Sure.  Look up list comprehensions and generator expressions in the docs.

'\n'.join('Press %i for %s' % (key, value) for (key, value) in
d.items()) will get you the entire string.  Whether this is better
than what you have done is a consideration I leave to you.

-- 
kushal
--
http://mail.python.org/mailman/listinfo/python-list


Re: Can I replace this for loop with a join?

2009-04-13 Thread Tim Chase

WP wrote:

Hello, I have dictionary {1:astring, 2:anotherstring, etc}

I now want to print:
Press 1 for astring
Press 2 for anotherstring etc

I could do it like this:
dict = {1:'astring', 2:'anotherstring'}
for key in dict.keys():
 print 'Press %i for %s' % (key, dict[key])

Press 1 for astring
Press 2 for anotherstring


Remember that a dict is inherently unordered, and if you get it 
ordered, you're just lucky :)  Also, it's bad style to mask the 
builtin dict with your own.



but can I use a join instead?


If you want to lean on this ordered assumption, you can simply do

  d = {1:astring, 2:another string}
  s = '\n'.join(
Press %i for %s % pair
for pair in d.iteritems()
)

and you'd have the resulting value in s.

But remember that if the order appears broken, you get to keep 
both parts :)  Better to do something like


  s = '\n'.join(
Press %i for %s % pair
for pair in sorted(d.items())
)

-tkc








--
http://mail.python.org/mailman/listinfo/python-list


Re: Can I replace this for loop with a join?

2009-04-13 Thread Nicolas Dandrimont
* WP no.i.d...@want.mail.from.spammers.com [2009-04-13 17:03:17 +0200]:

 Hello, I have dictionary {1:astring, 2:anotherstring, etc}

 I now want to print:
 Press 1 for astring
 Press 2 for anotherstring etc

 I could do it like this:
 dict = {1:'astring', 2:'anotherstring'}
 for key in dict.keys():
 print 'Press %i for %s' % (key, dict[key])

 Press 1 for astring
 Press 2 for anotherstring

 but can I use a join instead?

 Thanks for any replies!

First off, you shouldn't use 'dict' as a variable name, as it shadows
the built-in 'dict'.

If you really want to obfuscate this, you could use:

my_dict = {1: 'astring', 2: 'anotherstring'}
print \n.join('Press %i for %s' % (key, value) for key, value in 
my_dict.items())

I find this highly ugly and non readable, though.

HTH,
-- 
Nicolas Dandrimont

sic transit discus mundi
(From the System Administrator's Guide, by Lars Wirzenius)


signature.asc
Description: Digital signature
--
http://mail.python.org/mailman/listinfo/python-list