Re: The inverse of .join

2010-06-18 Thread Neil Cerutti
On 2010-06-18, Jon Clements  wrote:
>> I just wondered if something smoother was available.
>
> In terms of behaviour and 'safety', I'd go for:
>
 rec = { 'code1': '1,2,3', 'code2': '' }
 next(csv.reader([rec['code1']]))
> ['1', '2', '3']
 next(csv.reader([rec['code2']]))
> []

Slick!

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


Re: The inverse of .join

2010-06-18 Thread Jon Clements
On 17 June, 21:03, Neil Cerutti  wrote:
> On 2010-06-17, Robert Kern  wrote:
>
> > On 6/17/10 2:08 PM, Neil Cerutti wrote:
> >> On 2010-06-17, Ian Kelly  wrote:
> >>> On Thu, Jun 17, 2010 at 11:45 AM, Neil Cerutti
> >>>   wrote:
>  What's the best way to do the inverse operation of the .join
>  function?
>
> >>> Use the str.split method?
>
> >> split is perfect except for what happens with an empty string.
>
> > Why don't you try it and find out?
>
> I'm currently using the following without problems, while reading
> a data file. One of the fields is a comma separated list, and may
> be empty.
>
>   f = rec['codes']
>   if f == "":
>     f = []
>   else:
>     f = f.split(",")
>
> I just wondered if something smoother was available.
>
> --
> Neil Cerutti

In terms of behaviour and 'safety', I'd go for:

>>> rec = { 'code1': '1,2,3', 'code2': '' }
>>> next(csv.reader([rec['code1']]))
['1', '2', '3']
>>> next(csv.reader([rec['code2']]))
[]

hth
Jon.

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


Re: The inverse of .join

2010-06-18 Thread Neil Cerutti
On 2010-06-18, Steven D'Aprano  wrote:
> On Thu, 17 Jun 2010 20:03:42 +, Neil Cerutti wrote:
>> I'm currently using the following without problems, while
>> reading a data file. One of the fields is a comma separated
>> list, and may be empty.
>> 
>>   f = rec['codes']
>>   if f == "":
>> f = []
>>   else:
>> f = f.split(",")
>> 
>> I just wondered if something smoother was available.
>
> Seems pretty smooth to me. What's wrong with it? I assume
> you've put it into a function for ease of use and reduction of
> code duplication.

The part that's wrong with it, and it's probably my fault, is
that I can never think of it. I had to go dig it out of my code
to remember what the special case was.

> You could also use the ternary operator, in which case it's a
> mere two- liner and short enough to inline wherever you need
> it:
>
> f = rec['codes']
> f = f.split(",") if f else []

That's pretty cool.

Thanks to everybody for their thoughts.

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


Re: The inverse of .join

2010-06-17 Thread Steven D'Aprano
On Thu, 17 Jun 2010 20:03:42 +, Neil Cerutti wrote:

> I'm currently using the following without problems, while reading a data
> file. One of the fields is a comma separated list, and may be empty.
> 
>   f = rec['codes']
>   if f == "":
> f = []
>   else:
> f = f.split(",")
> 
> I just wondered if something smoother was available.

Seems pretty smooth to me. What's wrong with it? I assume you've put it 
into a function for ease of use and reduction of code duplication.

You could also use the ternary operator, in which case it's a mere two-
liner and short enough to inline wherever you need it:

f = rec['codes']
f = f.split(",") if f else []




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


Re: The inverse of .join

2010-06-17 Thread Steven D'Aprano
On Thu, 17 Jun 2010 20:44:41 +0100, MRAB wrote:

> Should .split grow an addition keyword argument to specify the desired
> behaviour?

Please no.


>  (Although it's simple enough to define your own function.)

Exactly.


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


Re: The inverse of .join

2010-06-17 Thread Steven D'Aprano
On Thu, 17 Jun 2010 17:45:41 +, Neil Cerutti wrote:

> What's the best way to do the inverse operation of the .join function?

str.join is a many-to-one function, and so it doesn't have an inverse. 
You can't always get the input back unchanged:

>>> L = ["a", "b", "c|d", "e"]
>>> s = '|'.join(L)
>>> s
'a|b|c|d|e'
>>> s.split('|')
['a', 'b', 'c', 'd', 'e']


There's no general way of getting around this -- if split() takes input 
"a|b|c", there is no way even in principle for it to know which of these 
operations it should reverse:

"|".join(["a", "b", "c"])
"|".join(["a|b", "c"])
"|".join(["a", "b|c"])
"|".join(["a|b|c"])
"b".join(["a|", "|c"])

The behaviour with the empty string is just a special case of this.



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


Re: The inverse of .join

2010-06-17 Thread Robert Kern

On 6/17/10 3:03 PM, Neil Cerutti wrote:

On 2010-06-17, Robert Kern  wrote:

On 6/17/10 2:08 PM, Neil Cerutti wrote:

On 2010-06-17, Ian Kelly   wrote:

On Thu, Jun 17, 2010 at 11:45 AM, Neil Cerutti
   wrote:

What's the best way to do the inverse operation of the .join
function?


Use the str.split method?


split is perfect except for what happens with an empty string.


Why don't you try it and find out?


I would like to apologize. I read that sentence as a question for some reason.

That said, it always helps for you to show the results that you are getting (and 
the code that gives those results) and state what results you were expecting.


--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
 that is made terrible by our own mad attempt to interpret it as though it had
 an underlying truth."
  -- Umberto Eco

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


Re: The inverse of .join

2010-06-17 Thread Neil Cerutti
On 2010-06-17, Robert Kern  wrote:
> On 6/17/10 2:08 PM, Neil Cerutti wrote:
>> On 2010-06-17, Ian Kelly  wrote:
>>> On Thu, Jun 17, 2010 at 11:45 AM, Neil Cerutti
>>>   wrote:
 What's the best way to do the inverse operation of the .join
 function?
>>>
>>> Use the str.split method?
>>
>> split is perfect except for what happens with an empty string.
>
> Why don't you try it and find out?

I'm currently using the following without problems, while reading
a data file. One of the fields is a comma separated list, and may
be empty.

  f = rec['codes']
  if f == "":
f = []
  else:
f = f.split(",")

I just wondered if something smoother was available.

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


Re: The inverse of .join

2010-06-17 Thread Stephen Hansen
On 6/17/10 12:44 PM, MRAB wrote:
> Neil Cerutti wrote:
>> On 2010-06-17, Ian Kelly  wrote:
>>> On Thu, Jun 17, 2010 at 11:45 AM, Neil Cerutti
>>>  wrote:
 What's the best way to do the inverse operation of the .join
 function?
>>> Use the str.split method?
>>
>> split is perfect except for what happens with an empty string.
>>
> I see what you mean.
> 
> This is consistent:
> 
 ','.join([''])
> ''
 ''.split(',')
> ['']
> 
> but this isn't:
> 
 ','.join([])
> ''
 ''.split(',')
> ['']
> 
> An empty string could be the result of .join(['']) or .join([]).
> 
> Should .split grow an addition keyword argument to specify the desired
> behaviour? (Although it's simple enough to define your own function.)

Guido finds keyword-arguments-to-change-behavior to be unPythonic, IIRC.
It generally means 'make a new API'. But, the question is-- is it worth
the mental strain of a new API?

This is such an extreme edge case, having to do:

  if blah:
result = blah.split(',')
  else:
result = []

Is really not asking too much, I think.

-- 

   Stephen Hansen
   ... Also: Ixokai
   ... Mail: me+list/python (AT) ixokai (DOT) io
   ... Blog: http://meh.ixokai.io/



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


Re: The inverse of .join

2010-06-17 Thread Robert Kern

On 6/17/10 2:08 PM, Neil Cerutti wrote:

On 2010-06-17, Ian Kelly  wrote:

On Thu, Jun 17, 2010 at 11:45 AM, Neil Cerutti
  wrote:

What's the best way to do the inverse operation of the .join
function?


Use the str.split method?


split is perfect except for what happens with an empty string.


Why don't you try it and find out?

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
 that is made terrible by our own mad attempt to interpret it as though it had
 an underlying truth."
  -- Umberto Eco

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


Re: The inverse of .join

2010-06-17 Thread MRAB

Neil Cerutti wrote:

On 2010-06-17, Ian Kelly  wrote:

On Thu, Jun 17, 2010 at 11:45 AM, Neil Cerutti
 wrote:

What's the best way to do the inverse operation of the .join
function?

Use the str.split method?


split is perfect except for what happens with an empty string.


I see what you mean.

This is consistent:

>>> ','.join([''])
''
>>> ''.split(',')
['']

but this isn't:

>>> ','.join([])
''
>>> ''.split(',')
['']

An empty string could be the result of .join(['']) or .join([]).

Should .split grow an addition keyword argument to specify the desired
behaviour? (Although it's simple enough to define your own function.)
--
http://mail.python.org/mailman/listinfo/python-list


Re: The inverse of .join

2010-06-17 Thread Neil Cerutti
On 2010-06-17, Ian Kelly  wrote:
> On Thu, Jun 17, 2010 at 11:45 AM, Neil Cerutti
>  wrote:
>> What's the best way to do the inverse operation of the .join
>> function?
>
> Use the str.split method?

split is perfect except for what happens with an empty string.

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


Re: The inverse of .join

2010-06-17 Thread Ian Kelly
On Thu, Jun 17, 2010 at 11:45 AM, Neil Cerutti  wrote:
> What's the best way to do the inverse operation of the .join
> function?

Use the str.split method?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: The inverse of .join

2010-06-17 Thread MRAB

Neil Cerutti wrote:

What's the best way to do the inverse operation of the .join
function?


.split, possibly, although there will be problems if the string contains
other occurrences of the separator.
--
http://mail.python.org/mailman/listinfo/python-list


Re: The inverse of .join

2010-06-17 Thread nn

Neil Cerutti wrote:
> What's the best way to do the inverse operation of the .join
> function?
>
> --
> Neil Cerutti

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


The inverse of .join

2010-06-17 Thread Neil Cerutti
What's the best way to do the inverse operation of the .join
function?

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