[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Is there a simple way to parse this string ?

Stef Mientki

12/19/2007 9:17:00 PM

hello,

I need to translate the following string
a = '(0, 0, 0, 255), (192, 192, 192, 255), True, 8'

into the following list or tuple
b = [(0, 0, 0, 255), (192, 192, 192, 255), True, 8 ]

Is there a simple way to to this.
(Not needed now, but might need it in the future: even deeper nested
lists, represented by a string.)

thanks,
Stef Mientki
13 Answers

josepharmbruster@gmail.com

12/19/2007 9:51:00 PM

0

Stef,

You can quickly get a tuple via:

t = eval('(0, 0, 0, 255), (192, 192, 192, 255), True, 8')

Joseph Armbruster

On Dec 19, 4:17 pm, Stef Mientki <stef.mien...@gmail.com> wrote:
> hello,
>
> I need to translate the following string
> a = '(0, 0, 0, 255), (192, 192, 192, 255), True, 8'
>
> into the following list or tuple
> b = [(0, 0, 0, 255), (192, 192, 192, 255), True, 8 ]
>
> Is there a simple way to to this.
> (Not needed now, but might need it in the future: even deeper nested
> lists, represented by a string.)
>
> thanks,
> Stef Mientki

Larry Bates

12/19/2007 10:10:00 PM

0

Stef Mientki wrote:
> hello,
>
> I need to translate the following string
> a = '(0, 0, 0, 255), (192, 192, 192, 255), True, 8'
>
> into the following list or tuple
> b = [(0, 0, 0, 255), (192, 192, 192, 255), True, 8 ]
>
> Is there a simple way to to this.
> (Not needed now, but might need it in the future: even deeper nested
> lists, represented by a string.)
>
> thanks,
> Stef Mientki

There are threads on this list about eval and how you need to be careful.
Make sure you know where the string is coming from and can control the
contents. If you read if from a user they could type in:

os.system('rm -rf *') or os.system('del *.*')

eval that and it deletes all the files on your disk

-Larry

John Machin

12/19/2007 10:24:00 PM

0

On Dec 20, 9:10 am, Larry Bates <larry.ba...@websafe.com> wrote:
> Stef Mientki wrote:
> > hello,
>
> > I need to translate the following string
> > a = '(0, 0, 0, 255), (192, 192, 192, 255), True, 8'
>
> > into the following list or tuple
> > b = [(0, 0, 0, 255), (192, 192, 192, 255), True, 8 ]
>
> > Is there a simple way to to this.
> > (Not needed now, but might need it in the future: even deeper nested
> > lists, represented by a string.)
>
> > thanks,
> > Stef Mientki
>
> There are threads on this list about eval and how you need to be careful.

In particular Paul Maguire recently pointed to a safe evaluator that
was restricted (IIRC) to something like lists/dicts/etc of ints/floats/
string/etc constants -- looks like just what you need.

> Make sure you know where the string is coming from and can control the
> contents. If you read if from a user they could type in:
>
> os.system('rm -rf *') or os.system('del *.*')
>
> eval that and it deletes all the files on your disk
>

Does anyone know of a newsreader that can automatically killfile
people who suggest eval without any warnings at all? Or should we let
Darwinian selection take its effect?

Gabriel Genellina

12/20/2007 12:57:00 AM

0

En Wed, 19 Dec 2007 19:23:36 -0300, John Machin <sjmachin@lexicon.net>
escribió:

> On Dec 20, 9:10 am, Larry Bates <larry.ba...@websafe.com> wrote:
>> Stef Mientki wrote:
>>
>> > I need to translate the following string
>> > a = '(0, 0, 0, 255), (192, 192, 192, 255), True, 8'
>>
>> > into the following list or tuple
>> > b = [(0, 0, 0, 255), (192, 192, 192, 255), True, 8 ]
>>
>> > Is there a simple way to to this.
>
> In particular Paul Maguire recently pointed to a safe evaluator that
> was restricted (IIRC) to something like lists/dicts/etc of ints/floats/
> string/etc constants -- looks like just what you need.

There is also a Cookbook recipe for a safe_eval function at
http://aspn.activestate.com/ASPN/Cookbook/Python/Rec...

> Does anyone know of a newsreader that can automatically killfile
> people who suggest eval without any warnings at all? Or should we let
> Darwinian selection take its effect?

Doesn't work, Darwininan selection would act on the unfortunate people
asking, not on who careless answers "use eval" :(

--
Gabriel Genellina

Paul McGuire

12/20/2007 1:45:00 AM

0

On Dec 19, 4:23 pm, John Machin <sjmac...@lexicon.net> wrote:
> On Dec 20, 9:10 am, Larry Bates <larry.ba...@websafe.com> wrote:
>
>
>
>
>
> > Stef Mientki wrote:
> > > hello,
>
> > > I need to translate the following string
> > > a = '(0, 0, 0, 255), (192, 192, 192, 255), True, 8'
>
> > > into the following list or tuple
> > > b = [(0, 0, 0, 255), (192, 192, 192, 255), True, 8 ]
>
> > > Is there a simple way to to this.
> > > (Not needed now, but might need it in the future: even deeper nested
> > > lists, represented by a string.)
>
> > > thanks,
> > > Stef Mientki
>
> > There are threads on this list about eval and how you need to be careful.
>
> In particular Paul Maguire recently pointed to a safe evaluator that
> was restricted (IIRC) to something like lists/dicts/etc of ints/floats/
> string/etc constants -- looks like just what you need.
>

I think the last thread of this nature also cited a similar tool by
the effbot, which he describes here: http://www.effbot.org/zone/simple-iterator-....
This parser is about 10X faster than the equivalent pyparsing parser.

-- Paul (McGuire)

Paul McGuire

12/20/2007 2:29:00 AM

0

On Dec 19, 4:23 pm, John Machin <sjmac...@lexicon.net> wrote:
> On Dec 20, 9:10 am, Larry Bates <larry.ba...@websafe.com> wrote:
>
> In particular Paul Maguire recently pointed to a safe evaluator that
> was restricted (IIRC) to something like lists/dicts/etc of ints/floats/
> string/etc constants -- looks like just what you need.
>

The pyparsing parser can be viewed at
http://pyparsing.wikispaces.com/space/showimage/parsePyth....

-- Paul

George Sakkis

12/20/2007 4:34:00 PM

0

On Dec 19, 8:44 pm, Paul McGuire <pt...@austin.rr.com> wrote:

> I think the last thread of this nature also cited a similar tool by
> the effbot, which he describes here:http://www.effbot.org/zone/simple-iterator-....
> This parser is about 10X faster than the equivalent pyparsing parser.

Here's the relevant thread: http://preview.tinyurl..... Note
that the builtin eval() is around 5x faster than this parser, and from
the statement above, 50x faster than the pyparsing solution.

George

josepharmbruster@gmail.com

12/21/2007 4:27:00 AM

0

Stef,

For clarification, there is nothing hazardous about using eval on the
string that you presented.

t = eval('(0, 0, 0, 255), (192, 192, 192, 255), True, 8')

Whether or not this is the "simplest" solution, remains a question.

Steven D'Aprano

12/21/2007 7:09:00 AM

0

On Thu, 20 Dec 2007 20:27:23 -0800, josepharmbruster@gmail.com wrote:

> Stef,
>
> For clarification, there is nothing hazardous about using eval on the
> string that you presented.
>
> t = eval('(0, 0, 0, 255), (192, 192, 192, 255), True, 8')
>
> Whether or not this is the "simplest" solution, remains a question.


For clarification, if all the poster wanted was to convert the *specific*
*known* string to a tuple, he would be better off just writing it as a
tuple:

t = (0, 0, 0, 255), (192, 192, 192, 255), True, 8

is much faster than calling eval().

But obviously that's not what the Original Poster wants to do. The tuple
give was indicative of input that comes from somewhere -- perhaps a
config file, perhaps a web form, perhaps a command line argument, who
knows? The point is, if the string comes from a user, then it could
contain anything:

'(0, 0, 0, 255), (192, 192, 192, 255), True, 8'
'1000, 10001, 100002, 1000004'
'"foo bar baz".split()'
'[i for i in range(100000)]'
'[19852.7412]*100000**2'
'__import__("os").system("ls -r *")'


Just because the OP's specific example is safe doesn't make eval() safe.


--
Steven

josepharmbruster@gmail.com

12/22/2007 3:21:00 PM

0

Steven D'Aprano,

On Dec 21, 2:08 am, Steven D'Aprano
<ste...@REMOVE.THIS.cybersource.com.au> wrote:
> On Thu, 20 Dec 2007 20:27:23 -0800, josepharmbrus...@gmail.com wrote:
> > Stef,
>
> > For clarification, there is nothing hazardous about using eval on the
> > string that you presented.
>
> > t = eval('(0, 0, 0, 255), (192, 192, 192, 255), True, 8')
>
> > Whether or not this is the "simplest" solution, remains a question.
>
> For clarification, if all the poster wanted was to convert the *specific*
> *known* string to a tuple, he would be better off just writing it as a
> tuple:

Steven,

No, that's not what he asked. Read the original question.

>
> t = (0, 0, 0, 255), (192, 192, 192, 255), True, 8
>
> is much faster than calling eval().
>
> But obviously that's not what the Original Poster wants to do.

There's nothing "Obviously" Implied about what the author wants to do
here, besides convert an innocent string object to a tuple.

> The tuple
> give was indicative of input that comes from somewhere

Really? I personally can't tell that from his provided example.
There's definitely not enough info on this one.

> -- perhaps a
> config file, perhaps a web form, perhaps a command line argument, who
> knows? The point is, if the string comes from a user, then it could
> contain anything:
>
> '(0, 0, 0, 255), (192, 192, 192, 255), True, 8'
> '1000, 10001, 100002, 1000004'
> '"foo bar baz".split()'
> '[i for i in range(100000)]'
> '[19852.7412]*100000**2'
> '__import__("os").system("ls -r *")'
>
> Just because the OP's specific example is safe doesn't make eval() safe.

Agreed. And after the last couple comments, he was probably made
aware of that. Thank you for reiterating :-)

>
> --
> Steven