Re: tuple to string?

2005-07-24 Thread Francois De Serres
Francois De Serres wrote:

hiho,

what's the clean way to translate the tuple (0x73, 0x70, 0x61, 0x6D) to 
the string 'spam'?

TIA,
Francois
  

thanks to all!

I'll pick ('%c' * len(t)) % t, for it's readability and the fact that 
join() is on the deprec'd list.

Francois

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


Re: tuple to string?

2005-07-24 Thread John Machin
Steven D'Aprano wrote:
 On Sat, 23 Jul 2005 23:26:19 +1000, John Machin wrote:
 
 
Steven D'Aprano wrote:



''.join(map(lambda n: chr(n), (0x73, 0x70, 0x61, 0x6D)))

'spam'

Why the verbal diarrhoea? 
 
 
 One line is hardly verbal diarrhoea.
 
 
What's wrong with the (already posted)

''.join(map(chr, (0x73, 0x70, 0x61, 0x6D)))

???
 
 
 Nothing.
 
 If I had seen the already posted solution using chr on its own without
 lambda, I wouldn't have bothered posting the lambda solution. But I
 didn't, so I did.
 
 As another poster has already pointed out, lambda cries out for over-use,
 and this was a perfect example of it.

Here are a couple of reductions you can use in future, in the order given:

(1)
lambda args: foo(args) - foo # for *any* function foo, not just chr

(2)
lambda args: almost_any_guff
-
def meaningful_func_name(args):
almost_any_guff
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: tuple to string?

2005-07-24 Thread John Machin
Steven D'Aprano wrote:
 On Sat, 23 Jul 2005 23:31:04 +1000, John Machin wrote:
 
 
You don't need the sissy parentheses; '%c' * len(t) % t works just fine :-)


Ah, ok. Didn't want to lookup the precedence rules...


Look up the precedence rules? Are you aware of any language where * / 
and % _don't_ have the same precedence??
 
 
 Do languages like Pascal that don't have string formatting expressions, or
 use the % operator, count?

A thousand pardons; I should have said Are you aware of any language 
which has % (as primarily a numeric remainder/modulo operator) but * /
and % _don't_ have the same precedence??

OK, given a language which does have * and / used among other things for 
numerical multiply and divide, (a) are you aware of any such language 
which does does not have * and / at the same precedence level (b) 
supposing one wanted to introduce % as a numerical 
remainder/modulo/whatever operator (plus other meaning(s) for 
non-numeric types), would you care to argue that it should not have the 
same precedence level (as * and /)?

Pascal was/is a prime example of bad precedence choice:
a  b or c  d
means
a  (b or c)  d
in Pascal (not very useful)
and
(a  b) or (c  d)
in many other languages.


 
 How about languages like Forth that don't have precedence rules at all,
 unless first come, first served is a precedence rule?

No precedence rules - no relevance to the topic
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: tuple to string?

2005-07-24 Thread John Machin
Francois De Serres wrote:
 Francois De Serres wrote:
 
 hiho,

 what's the clean way to translate the tuple (0x73, 0x70, 0x61, 0x6D) 
 to the string 'spam'?

 TIA,
 Francois
  

 thanks to all!
 
 I'll pick ('%c' * len(t)) % t, for it's readability and the fact that 
 join() is on the deprec'd list.

I presume you mean deprecated.

AFAIK there is no such thing as a deprecated list.

Certain constructs cause a deprecation warning to be emitted at run time 
-- like passing a float argument where an int is expected.

Other constructs could be loosely described as deprecated because there 
is now a better way to do it, but no messages are generated. This is so 
for almost all of the functions in the string module. One example of 
this is join: instead of string.join(alist, sep) one now does 
sep.join(alist)

Given a non-string sequence of single characters, the 
common/standard/well-known idiom for producing a string uses join; it is 
''.join(seq)

Backing up to readability, I wouldn't have picked
('%c' * len(t)) % t (nor the version with 2 fewer parentheses!) as 
particulary readable -- mainly because %c is AFAIK relatively little 
used in Python and only someone familar with C etc would understand why 
it works, or why it even exists. OTOH something like ''.join(chr(x) for 
x in t) is made up of well-known frequently-used components.

Cheers,
John
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: tuple to string?

2005-07-24 Thread Robert Kern
Francois De Serres wrote:

 I'll pick ('%c' * len(t)) % t, for it's readability and the fact that 
 join() is on the deprec'd list.

''.join() is certainly not deprecated. What made you think that?

-- 
Robert Kern
[EMAIL PROTECTED]

In the fields of hell where the grass grows high
  Are the graves of dreams allowed to die.
   -- Richard Harter

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


Re: tuple to string?

2005-07-24 Thread Francois De Serres
Robert Kern wrote:

Francois De Serres wrote:

  

I'll pick ('%c' * len(t)) % t, for it's readability and the fact that 
join() is on the deprec'd list.



''.join() is certainly not deprecated. What made you think that?

  

this:
http://www.python.org/doc/2.4.1/lib/node110.html

but I now realize it's a different version of join() that was proposed 
here...

thank you,
Francois

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


Re: tuple to string?

2005-07-24 Thread Steven D'Aprano
On Sun, 24 Jul 2005 21:55:19 +1000, John Machin wrote:

Look up the precedence rules? Are you aware of any language where * / 
and % _don't_ have the same precedence??
 
 
 Do languages like Pascal that don't have string formatting expressions, or
 use the % operator, count?
 
 A thousand pardons; I should have said Are you aware of any language 
 which has % (as primarily a numeric remainder/modulo operator) but * /
 and % _don't_ have the same precedence??

[slaps head]

Ah, I had completely forgotten that Pascal has a MOD operator that is
equivalent to % and has the same precedence as * / and DIV. So scratch
Pascal off the list.

But APL uses right-to-left precedence for all operators, and Forth uses
left-to-right. There may be others.


 OK, given a language which does have * and / used among other things for 
 numerical multiply and divide, (a) are you aware of any such language 
 which does does not have * and / at the same precedence level (b) 
 supposing one wanted to introduce % as a numerical 
 remainder/modulo/whatever operator (plus other meaning(s) for 
 non-numeric types), would you care to argue that it should not have the 
 same precedence level (as * and /)?

Yes I would.

Since the remainder (or modulo) operator is not distributive, the only
unambiguous usage is to use parentheses, or to decide on precedence rules.
The usual mathematical convention is that modulus has lower precedence
than addition, eg in clock arithmetic we expect that three hours after
ten is one: 10+3 modulo 12 is 1, not 13.


-- 
Steven.

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


Re: tuple to string?

2005-07-24 Thread Robert Kern
John Machin wrote:

 No precedence rules - no relevance to the topic

Precedence rules of other languages - no relevance to the topic

-- 
Robert Kern
[EMAIL PROTECTED]

In the fields of hell where the grass grows high
  Are the graves of dreams allowed to die.
   -- Richard Harter

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


Re: tuple to string?

2005-07-24 Thread Steven D'Aprano
On Sun, 24 Jul 2005 10:39:44 -0700, Robert Kern wrote:

 John Machin wrote:
 
 No precedence rules - no relevance to the topic
 
 Precedence rules of other languages - no relevance to the topic


I thought the topic was -- or at least had wandered in the direction of --
whether or not it was unthinkable for the precedence of % to be
anything but that of multiplication and division. Surely the precedence
rules of other languages have some relevance to that question. 

Still, the subject is rapidly losing whatever interest it may have had.


-- 
Steven.

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


Re: tuple to string?

2005-07-24 Thread Peter Hansen
Steven D'Aprano wrote:
 Still, the subject is rapidly losing whatever interest it may have had.

It had none.  Kill it.  Kill the witch!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: tuple to string?

2005-07-23 Thread Reinhold Birkenfeld
John Machin wrote:
 Reinhold Birkenfeld wrote:
 Berthold Höllmann wrote:
 
Francois De Serres [EMAIL PROTECTED] writes:


hiho,

what's the clean way to translate the tuple (0x73, 0x70, 0x61, 0x6D)
to the string 'spam'?

. t = (0x73, 0x70, 0x61, 0x6D)
. ''.join('%c' % c for c in t)
'spam'
 
 
 Or:
 
 t = (0x73, 0x70, 0x61, 0x6D)
 ('%c' * len(t)) % t
 
 You don't need the sissy parentheses; '%c' * len(t) % t works just fine :-)

Ah, ok. Didn't want to lookup the precedence rules...

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

Re: tuple to string?

2005-07-23 Thread Steven D'Aprano
On Fri, 22 Jul 2005 06:07:28 -0700, Robert Kern wrote:

 Francois De Serres wrote:
 hiho,
 
 what's the clean way to translate the tuple (0x73, 0x70, 0x61, 0x6D) to 
 the string 'spam'?
 
 In [1]: t = (0x73, 0x70, 0x61, 0x6D)
 
 In [2]: ''.join(chr(x) for x in t)
 Out[2]: 'spam'

I get a syntax error when I try that. I guess anyone who hasn't started
using Python 2.4 will also get the same error.

Since t is just a tuple, there isn't a big advantage as far as I can
see to build up and dispose of the generator machinery just for grabbing
the next item in a tuple. So a list comprehension will work just as well,
and in older versions of Python:

''.join([chr(x) for x in (0x73, 0x70, 0x61, 0x6D)])

For an even more version-independent method:

L = []
for n in (0x73, 0x70, 0x61, 0x6D):
L.append(chr(n))
print ''.join(L)


or even:

 ''.join(map(lambda n: chr(n), (0x73, 0x70, 0x61, 0x6D)))
'spam'



-- 
Steven.

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


Re: tuple to string?

2005-07-23 Thread Scott David Daniels
Steven D'Aprano wrote:
 On Fri, 22 Jul 2005 06:07:28 -0700, Robert Kern wrote:
 ... or even:
 
''.join(map(lambda n: chr(n), (0x73, 0x70, 0x61, 0x6D)))
 
 'spam'

This is exactly what is wrong with lambda.  It yearns for over-use.
This last should be:

 ''.join(map(chr, (0x73, 0x70, 0x61, 0x6D)))

--Scott David Daniels
[EMAIL PROTECTED]
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: tuple to string?

2005-07-23 Thread John Machin
Steven D'Aprano wrote:

 
 
''.join(map(lambda n: chr(n), (0x73, 0x70, 0x61, 0x6D)))
 
 'spam'

Why the verbal diarrhoea? What's wrong with the (already posted)

''.join(map(chr, (0x73, 0x70, 0x61, 0x6D)))

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


Re: tuple to string?

2005-07-23 Thread John Machin
Reinhold Birkenfeld wrote:
 John Machin wrote:
 
Reinhold Birkenfeld wrote:

Berthold Höllmann wrote:


Francois De Serres [EMAIL PROTECTED] writes:



hiho,

what's the clean way to translate the tuple (0x73, 0x70, 0x61, 0x6D)
to the string 'spam'?

. t = (0x73, 0x70, 0x61, 0x6D)
. ''.join('%c' % c for c in t)
'spam'


Or:

t = (0x73, 0x70, 0x61, 0x6D)
('%c' * len(t)) % t

You don't need the sissy parentheses; '%c' * len(t) % t works just fine :-)
 
 
 Ah, ok. Didn't want to lookup the precedence rules...


Look up the precedence rules? Are you aware of any language where * / 
and % _don't_ have the same precedence??
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: tuple to string?

2005-07-23 Thread Robert Kern
John Machin wrote:
 Reinhold Birkenfeld wrote:

Ah, ok. Didn't want to lookup the precedence rules...
 
 Look up the precedence rules? Are you aware of any language where * / 
 and % _don't_ have the same precedence??

Given that % is somewhat more esoteric, I certainly have never committed 
to memory its position in a precedence hierarchy of *any* language.

-- 
Robert Kern
[EMAIL PROTECTED]

In the fields of hell where the grass grows high
  Are the graves of dreams allowed to die.
   -- Richard Harter

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


Re: tuple to string?

2005-07-23 Thread Steven D'Aprano
On Sat, 23 Jul 2005 23:26:19 +1000, John Machin wrote:

 Steven D'Aprano wrote:
 
 
 
''.join(map(lambda n: chr(n), (0x73, 0x70, 0x61, 0x6D)))
 
 'spam'
 
 Why the verbal diarrhoea? 

One line is hardly verbal diarrhoea.

 What's wrong with the (already posted)
 
 ''.join(map(chr, (0x73, 0x70, 0x61, 0x6D)))
 
 ???

Nothing.

If I had seen the already posted solution using chr on its own without
lambda, I wouldn't have bothered posting the lambda solution. But I
didn't, so I did.

As another poster has already pointed out, lambda cries out for over-use,
and this was a perfect example of it.


-- 
Steven.

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


Re: tuple to string?

2005-07-23 Thread Steven D'Aprano
On Sat, 23 Jul 2005 23:31:04 +1000, John Machin wrote:

You don't need the sissy parentheses; '%c' * len(t) % t works just fine :-)
 
 
 Ah, ok. Didn't want to lookup the precedence rules...
 
 
 Look up the precedence rules? Are you aware of any language where * / 
 and % _don't_ have the same precedence??

Do languages like Pascal that don't have string formatting expressions, or
use the % operator, count?

How about languages like Forth that don't have precedence rules at all,
unless first come, first served is a precedence rule?

I'm not being academic here. I have used both these languages extensively,
admittedly many years ago.


-- 
Steven.

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


Re: tuple to string?

2005-07-22 Thread Jason Drew
''.join((chr(e) for e in (0x73, 0x70, 0x61, 0x6D)))

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


Re: tuple to string?

2005-07-22 Thread Robert Kern
Francois De Serres wrote:
 hiho,
 
 what's the clean way to translate the tuple (0x73, 0x70, 0x61, 0x6D) to 
 the string 'spam'?

In [1]: t = (0x73, 0x70, 0x61, 0x6D)

In [2]: ''.join(chr(x) for x in t)
Out[2]: 'spam'

-- 
Robert Kern
[EMAIL PROTECTED]

In the fields of hell where the grass grows high
  Are the graves of dreams allowed to die.
   -- Richard Harter

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


Re: tuple to string?

2005-07-22 Thread Peter Hansen
Francois De Serres wrote:
 hiho,
 
 what's the clean way to translate the tuple (0x73, 0x70, 0x61, 0x6D) to 
 the string 'spam'?

  mytuple = (0x73, 0x70, 0x61, 0x6D)
  ''.join(chr(v) for v in mytuple)
'spam'

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


Re: tuple to string?

2005-07-22 Thread deelan
Francois De Serres wrote:
 hiho,
 
 what's the clean way to translate the tuple (0x73, 0x70, 0x61, 0x6D) to 
 the string 'spam'?

one way is to use a list expression:

  ''.join([chr(c) for c in (0x73, 0x70, 0x61, 0x6D)])
'spam'

another is to use map:

  ''.join(map(chr, (0x73, 0x70, 0x61, 0x6D)))
'spam'

HTH,
deelan.

-- 
deelan, #1 fan of adriana lima!
http://www.deelan.com/




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


Re: tuple to string?

2005-07-22 Thread Qiangning Hong
On 7/22/05, Francois De Serres [EMAIL PROTECTED] wrote:
 what's the clean way to translate the tuple (0x73, 0x70, 0x61, 0x6D) to
 the string 'spam'?

Use ''.join and chr() as others have pointed out.  Here are
just-for-fun versions  ;)

. t = (0x73, 0x70, 0x61, 0x6D)

(use string formatter):
. '%c%c%c%c' % t
'spam'

(use struct model):
. import struct
. struct.pack('', *t)
'spam' 

-- 
Qiangning Hong

I'm usually annoyed by IDEs because, for instance, they don't use VIM
as an editor. Since I'm hooked to that, all IDEs I've used so far have
failed to impress me.
   -- Sybren Stuvel @ c.l.python

Get Firefox! http://www.spreadfirefox.com/?q=affiliatesamp;id=67907amp;t=1
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: tuple to string?

2005-07-22 Thread Qiangning Hong
Francois De Serres wrote:
 hiho,
 
 what's the clean way to translate the tuple (0x73, 0x70, 0x61, 0x6D) to 
 the string 'spam'?

Use ''.join and chr() as others have pointed out.  Here are just-for-fun 
versions ;)

. t = (0x73, 0x70, 0x61, 0x6D)

(use string formatter):
. '%c%c%c%c' % t
'spam'

(use struct model):
. import struct
. struct.pack('', *t)
'spam'

-- 
Qiangning Hong

I'm usually annoyed by IDEs because, for instance, they don't use VIM
as an editor. Since I'm hooked to that, all IDEs I've used so far have
failed to impress me.
 -- Sybren Stuvel @ c.l.python

Get Firefox! 
http://www.spreadfirefox.com/?q=affiliatesamp;id=67907amp;t=1
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: tuple to string?

2005-07-22 Thread Berthold Höllmann
Francois De Serres [EMAIL PROTECTED] writes:

 hiho,

 what's the clean way to translate the tuple (0x73, 0x70, 0x61, 0x6D)
 to the string 'spam'?

. t = (0x73, 0x70, 0x61, 0x6D)
. ''.join('%c' % c for c in t)
'spam'

-- 
Es gelten die Regeln der christlichen Seefahrt: Rot und Grün markiert
 das sichere Fahrwasser, Schwarz und Gelb markieren Untiefen und
 Wracks.
Christa Sager, Bundestagsfraktionsvorsitzende Bündnis 90/Grüne
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: tuple to string?

2005-07-22 Thread Reinhold Birkenfeld
Berthold Höllmann wrote:
 Francois De Serres [EMAIL PROTECTED] writes:
 
 hiho,

 what's the clean way to translate the tuple (0x73, 0x70, 0x61, 0x6D)
 to the string 'spam'?
 
 . t = (0x73, 0x70, 0x61, 0x6D)
 . ''.join('%c' % c for c in t)
 'spam'

Or:

t = (0x73, 0x70, 0x61, 0x6D)
('%c' * len(t)) % t

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

Re: tuple to string?

2005-07-22 Thread John Machin
Reinhold Birkenfeld wrote:
 Berthold Höllmann wrote:
 
Francois De Serres [EMAIL PROTECTED] writes:


hiho,

what's the clean way to translate the tuple (0x73, 0x70, 0x61, 0x6D)
to the string 'spam'?

. t = (0x73, 0x70, 0x61, 0x6D)
. ''.join('%c' % c for c in t)
'spam'
 
 
 Or:
 
 t = (0x73, 0x70, 0x61, 0x6D)
 ('%c' * len(t)) % t

You don't need the sissy parentheses; '%c' * len(t) % t works just fine :-)
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: tuple to string?

2005-07-22 Thread Patricia J. Hawkins
QH On 7/22/05, Francois De Serres [EMAIL PROTECTED] wrote:
 what's the clean way to translate the tuple (0x73, 0x70, 0x61, 0x6D) to
 the string 'spam'?

QH Use ''.join and chr() as others have pointed out.  Here are
QH just-for-fun versions  ;)

. t = (0x73, 0x70, 0x61, 0x6D)

QH (use string formatter):
. '%c%c%c%c' % t

Or more generally:

 t = (0x73, 0x70, 0x61, 0x6D)
 '%c'*len(t) % t
'spam'

but that's VERY perlonic python.  Still, it's a technique that can
come in handy when building, say, SQL queries on the fly.

-- 
Patricia J. Hawkins
Hawkins Internet Applications
www.hawkinsia.com
-- 
http://mail.python.org/mailman/listinfo/python-list