Beginner question: Converting Single-Element tuples to list

V

vdavidster

Hello everyone,

I want to convert a tuple to a list, and I expected this behavior:

list(('abc','def')) -> ['abc','def']
list(('abc')) -> ['abc']

But Python gave me this behavior:

list(('abc','def')) -> ['abc','def']
list(('abc')) -> ['a','b','c']

How do I do get Python to work like the in former case?

Many thanks!

David
 
P

Paul McGuire

('abc') is not a tuple - this is an unfortunate result of using ()'s as
expression grouping *and* as tuple delimiters. To make ('abc') a
tuple, you must add an extra comma, as ('abc',).
['abc']

-- Paul
 
V

vdavidster

Hi,

Thanks for your reply! A new thing learned....

Allow me to follow that up with another question:

Let's say I have a result from a module called pyparsing:

Results1 = ['abc', 'def']
Results2 = ['abc']

They are of the ParseResults type:
<class 'pyparsing.ParseResults'>

I want to convert them into Python lists. list() will work fine on
Results1, but on Results2, it will return:

['a', 'b', 'c']

Because 'abc' is a string. But I want it to return ['abc'].

In short, my question is: how do I typecast an arbitrary object,
whether a list-like object or a string, into a Python list without
having Python split my strings into characters?

Thanks very much for your help!

David

p.s. I know pyparsing has an asList() method, but let's just say I am
unable to use it in my circumstances.
 
R

Reinhold Birkenfeld

Hi,

Thanks for your reply! A new thing learned....

Allow me to follow that up with another question:

Let's say I have a result from a module called pyparsing:

Results1 = ['abc', 'def']
Results2 = ['abc']

They are of the ParseResults type:
<class 'pyparsing.ParseResults'>

I want to convert them into Python lists. list() will work fine on
Results1, but on Results2, it will return:

['a', 'b', 'c']

Because 'abc' is a string. But I want it to return ['abc'].

In short, my question is: how do I typecast an arbitrary object,
whether a list-like object or a string, into a Python list without
having Python split my strings into characters?

This seems like a glitch in pyparsing. If a ParseResults class emulates
a list, it should do so every time, not only if there is more than one
value in it.

Reinhold
 
J

John Roth

Reinhold Birkenfeld said:
Hi,

Thanks for your reply! A new thing learned....

Allow me to follow that up with another question:

Let's say I have a result from a module called pyparsing:

Results1 = ['abc', 'def']
Results2 = ['abc']

They are of the ParseResults type:
type(Results1)
type(Results2)
<class 'pyparsing.ParseResults'>

I want to convert them into Python lists. list() will work fine on
Results1, but on Results2, it will return:

['a', 'b', 'c']

Because 'abc' is a string. But I want it to return ['abc'].

In short, my question is: how do I typecast an arbitrary object,
whether a list-like object or a string, into a Python list without
having Python split my strings into characters?

This seems like a glitch in pyparsing. If a ParseResults class emulates
a list, it should do so every time, not only if there is more than one
value in it.

Unfortunately, I've seen that behavior a number of times:
no output is None, one output is the object, more than one
is a list of objects. That forces you to have checks for None
and list types all over the place. Granted, sometimes your
program logic would need checks anyway, but frequently
just returning a possibly empty list would save the caller
a lot of grief.

John Roth
 
P

Paul McGuire

David -

I'm not getting the same results. Run this test program:
---------------
import pyparsing as pp
import sys

def test(s):
results = pp.OneOrMore( pp.Word(pp.alphas) ).parseString( s )
print repr(s),"->",list(results)

print "Python version:", sys.version
print "pyparsing version:", pp.__version__
test("abc def")
test("abc")
---------------
The output I get is:
Python version: 2.4.1 (#65, Mar 30 2005, 09:13:57) [MSC v.1310 32 bit
(Intel)]
pyparsing version: 1.3.1
'abc def' -> ['abc', 'def']
'abc' -> ['abc']

What versions of pyparsing and Python are you using?

-- Paul
 
P

Paul McGuire

John -

I just modified my test program BNF to use ZeroOrMore instead of
OneOrMore, and parsed an empty string. Calling list() on the returned
results gives an empty list. What version of pyparsing are you seeing
this None/object/list behavior?

-- Paul
 
J

Jeff Epler

Unfortunately, I've seen that behavior a number of times:
no output is None, one output is the object, more than one
is a list of objects. That forces you to have checks for None
and list types all over the place.

maybe you can at least push this into a single convenience function...

def destupid(x, constructor=tuple, sequencetypes=(tuple, list)):
if x is None: return constructor()
if isinstance(x, sequencetypes): return x
return constructor((x,))

Jeff

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (GNU/Linux)

iD8DBQFCwCU1Jd01MZaTXX0RAivoAKCKcSf5YQaOSp3HWDYnctqzYzxKHwCePRb2
j564UN61oP7i8Cyoy1VHxL0=
=rasL
-----END PGP SIGNATURE-----
 
P

Paul McGuire

Modified version of test program, to handle empty strings -> empty
lists.

import pyparsing as pp
import sys

def test(s):
results = pp.ZeroOrMore( pp.Word(pp.alphas) ).parseString( s )
print repr(s),"->",list(results)

print "Python version:", sys.version
print "pyparsing version:", pp.__version__
test("abc def")
test("abc")
test("")

Prints:
Python version: 2.4.1 (#65, Mar 30 2005, 09:13:57) [MSC v.1310 32 bit
(Intel)]
pyparsing version: 1.3.1
'abc def' -> ['abc', 'def']
'abc' -> ['abc']
'' -> []
 
V

vdavidster

Hi Paul and everyone else,

I ran the script and here's what I got:

Python version: 2.4.1 (#2, Mar 31 2005, 00:05:10)
[GCC 3.3 20030304 (Apple Computer, Inc. build 1666)]
pyparsing version: 1.3
'abc def' -> ['abc', 'def']
'abc' -> ['abc']

It seems to work fine.

I figured out what my problem was: my earlier description was
erroneous. The type for Results2 was actually 'str', not
'pyparsing.ParseResults' as I had mentioned. (I was typing from memory
-- I didn't actually run my code.... sorry, my bad).

It seems that the way I was parsing it, I sometimes got
pyparsing.parseResults lists, and sometimes got *strings* (not
actually single-element lists as I thought).

I had expected pyparsing results to always return lists, I guess with
my token description below, it doesn't. Here's the bit of code I've
wrote:

from pyparsing import *
unit = Word(alphanums)
section = unit + Literal(':').suppress()
line = ~section + Combine(unit + SkipTo(Literal(';'), include=True))
block = ZeroOrMore(line)
file = Dict(ZeroOrMore(Group(section + block)))
Results = dict(file.parseString(MyFileString)

Where MyFileStrings (the string I want to parse) is something like
this:

var:
x, y, z;
constraints:
x <= 1;
y <= 2;
z <= 3;

And the results I'd get would be something like this:
{'var': 'x, y, z;', 'constraints': (['x <= 1', 'y <= 2', 'z <= 3'],
{})}

So going along Jeff Epler's line of thinking, I wrote a convenience
function:

def convertToList(input):
if type(input) == str:
output = [input]
else:
output = input
return output

So when I iterate through the keys of the Results dictionary, I just
have to convertToList(value) and I will always get a list.

Thanks for your feedback, everyone!

(and embarassingly, I just realized I was talking to the author of
pyparsing. Thanks for pyparsing, Paul! I'm using it to write a
mini-language for defining process control models used in chemical
engineering, and it has really given me a lot of leverage in terms of
churning actual working code out in minimal time. I was thinking of
doing it the lex/yacc way at first, but pyparsing is so much easier.)
 
S

Steven Bethard

if type(input) == str:

You might consider writing this as:

if isinstance(input, basestring):

I don't know if pyparsing ever produces unicode objects, but in case it
does (or it starts to in the future), isinstance is a better call here.

STeVe
 
P

Paul McGuire

Steve -

Good catch - in v1.3, I added some Unicode support for pyparsing,
although I have not gotten much feedback that anyone is using it, or
how well it works. So it is preferable to test against basestring
instead of str.

-- Paul
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top