Re: string to list

2012-06-14 Thread Chris Rebert
On Thu, Jun 14, 2012 at 12:40 AM, Hemanth H.M  wrote:
 list(literal_eval('"aa","bb 'b'","cc"'))
> ['aa', 'bb ', 'cc']
>
> Strange?

Not really. You didn't properly escape the embedded quotation marks in
the string itself!
So before anything ever even gets passed to literal_eval(), that part
is parsed as two adjacent literals: '"aa","bb ' and b'","cc"'
In Python 3.x, the "b" prefix indicates a `bytes` literal rather than
a `str` literal.

Implicit adjacent string literal concatenation then occurs.
Thus:
>>> print '"aa","bb ' b'","cc"'
"aa","bb ","cc"
Compare:
>>> print '''"aa","bb 'b'","cc"'''
"aa","bb 'b'","cc"

But really, literal_eval() should not be used for CSV; it won't handle
unquoted fields at all, among other issues.

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


Re: string to list

2012-06-14 Thread Anoop Thomas Mathew
@group: Sorry for the mistake.
@Hemanth: Thank You for pointing out.
I just realized that, we should not copy paste from the console. :)

atm
___
Life is short, Live it hard.




On 14 June 2012 13:09, Hemanth H.M  wrote:

> @Annop Nice one, but you seem to have missed a parenthesis.
>
> >>> list(literal_eval("'aa','bb','cc'")  should have been >>>
> list(literal_eval("'aa','bb','cc'"))
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: string to list

2012-06-14 Thread Hemanth H.M
>>> list(literal_eval('"aa","bb 'b'","cc"'))
['aa', 'bb ', 'cc']

Strange?

On Thu, Jun 14, 2012 at 1:09 PM, Hemanth H.M  wrote:

> @Annop Nice one, but you seem to have missed a parenthesis.
>
> >>> list(literal_eval("'aa','bb','cc'")  should have been >>>
> list(literal_eval("'aa','bb','cc'"))
>
>
> On Thu, Jun 14, 2012 at 12:58 PM, Anoop Thomas Mathew wrote:
>
>> >>> list(literal_eval("'aa','bb','cc'")
>
>
>
>
> --
> *'I am what I am because of who we all are'*
> h3manth.com 
> *-- Hemanth HM *
>



-- 
*'I am what I am because of who we all are'*
h3manth.com 
*-- Hemanth HM *
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: string to list

2012-06-14 Thread Hemanth H.M
@Annop Nice one, but you seem to have missed a parenthesis.

>>> list(literal_eval("'aa','bb','cc'")  should have been >>>
list(literal_eval("'aa','bb','cc'"))


On Thu, Jun 14, 2012 at 12:58 PM, Anoop Thomas Mathew wrote:

> >>> list(literal_eval("'aa','bb','cc'")




-- 
*'I am what I am because of who we all are'*
h3manth.com 
*-- Hemanth HM *
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: string to list

2012-06-14 Thread Anoop Thomas Mathew
Hi,

You can use literal_eval from ast package.

>>> from ast import literal_eval
>>> list(literal_eval("'aa','bb','cc'")

this will return ['aa', 'bb', 'cc']

Thanks,
Anoop Thomas Mathew

atm
___
Life is short, Live it hard.




On 14 June 2012 12:28, Shambhu Rajak  wrote:

> This will do you job:
>
> >>> a = 'AAA,",,",EEE,FFF,GGG'
> >>> b = []
> >>> for x in a.split(','):
> ... if (x.find("\"") > -1):
> ... x = x.strip("\"")
> ... b.append(x)
>
> If you want reduce the lines of code u can go for this option:
> b = [x.strip("\"") for x in a.split(',')]
>
>
> So Just Cheerz,
> -Shambhu
>
> -Original Message-
> From: bruce g [mailto:bruceg113...@gmail.com]
> Sent: 14/06/2012 8:00 AM
> To: python-list@python.org
> Subject: string to list
>
> What is the best way to parse a CSV string to a list?
>
> For example, how do I parse:
>'AAA,",,",EEE,FFF,GGG'
> to get:
>['AAA','BBB,CCC,','EEE','FFF','GGG']
>
> Thanks,
> Bruce
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: string to list

2012-06-14 Thread Peter Otten
bruce g wrote:

> What is the best way to parse a CSV string to a list?
> 
> For example, how do I parse:
> 'AAA,",,",EEE,FFF,GGG'
> to get:
> ['AAA','BBB,CCC,','EEE','FFF','GGG’]

>>> import csv
>>> next(csv.reader(['AAA,",,",EEE,FFF,GGG']))
['AAA', ',,', 'EEE', 'FFF', 'GGG']

For multiple records: 

list(csv.reader(text.splitlines(True)))

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


RE: string to list

2012-06-14 Thread Shambhu Rajak
This will do you job:

>>> a = 'AAA,",,",EEE,FFF,GGG'
>>> b = []
>>> for x in a.split(','):
... if (x.find("\"") > -1):
... x = x.strip("\"")
... b.append(x)

If you want reduce the lines of code u can go for this option:
b = [x.strip("\"") for x in a.split(',')] 


So Just Cheerz,
-Shambhu

-Original Message-
From: bruce g [mailto:bruceg113...@gmail.com] 
Sent: 14/06/2012 8:00 AM
To: python-list@python.org
Subject: string to list

What is the best way to parse a CSV string to a list?

For example, how do I parse:
'AAA,",,",EEE,FFF,GGG'
to get:
['AAA','BBB,CCC,','EEE','FFF','GGG']

Thanks,
Bruce


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


Re: string to list

2012-06-13 Thread Ian Kelly
On Wed, Jun 13, 2012 at 10:06 PM, Jose H. Martinez
 wrote:
> string.split(',') will give you an array.
>
> Example:
>
> 'AAA,",,",EEE,FFF,GGG '.split(',')
>
> ['AAA', '"', '', '"', 'EEE', 'FFF', 'GGG']

But it incorrectly splits the quoted part.  A proper CSV parser (like
the csv module) should leave that part as a single string, even though
it contains commas.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: string to list

2012-06-13 Thread Jose H. Martinez
string.split(',') will give you an array.

Example:

'AAA,",,",EEE,FFF,GGG '.split(',')

['AAA', '"', '', '"', 'EEE', 'FFF', 'GGG']

On Wed, Jun 13, 2012 at 10:53 PM, Chris Rebert  wrote:

> n Wed, Jun 13, 2012 at 7:29 PM, bruce g  wrote:
> > What is the best way to parse a CSV string to a list?
>
> Use the `csv` module:
> http://docs.python.org/library/csv.html
> http://www.doughellmann.com/PyMOTW/csv/
>
> The `StringIO` module can be used to wrap your string as a file-like
> object for consumption by the `csv` module:
> http://docs.python.org/library/stringio.html
>
> Cheers,
> Chris
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: string to list

2012-06-13 Thread Chris Rebert
n Wed, Jun 13, 2012 at 7:29 PM, bruce g  wrote:
> What is the best way to parse a CSV string to a list?

Use the `csv` module:
http://docs.python.org/library/csv.html
http://www.doughellmann.com/PyMOTW/csv/

The `StringIO` module can be used to wrap your string as a file-like
object for consumption by the `csv` module:
http://docs.python.org/library/stringio.html

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


Re: Converting a string to list for submission to easygui multenterb​ox

2012-05-02 Thread Chris Angelico
On Thu, May 3, 2012 at 3:57 AM, Laurent Pointal  wrote:
> If you have it as a string, you can use eval() (not safe!) on the string to
> retrieve the tuple, then list() on the tuple to get a list.

Are you saying that eval is not safe (which it isn't), or that it has
to be eval() and not safe_eval() to do this job? There's also
ast.literal_eval which ought to be safe for this situation (I think).

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


Re: Converting a string to list for submission to easygui multenterb​ox

2012-05-02 Thread ksals
On May 2, 1:57 pm, Laurent Pointal  wrote:
> ksals wrote:
> > On May 1, 5:29 pm, John Gordon  wrote:
> >> In <3b5f65c4-cd95-4bb4-94f2-0c69cf2b1...@d20g2000vbh.googlegroups.com>
> >> ksals  writes:
>
> >> > The original choice looks like this when I print it:
> >> > print(choice)
> >> > ('ksals', '', 'alsdkfj', '3', '')
> >> > I need to submit these as defaults to a multenterbox. Each entry above
> >> > ksals, "", "alsdkfj', 3 , '' need to fill the five fields in the box.
> >> > I tried your suggestion so you must be right it is a tuple of 5
> >> > strings.  But I need them to work in an instruction like
> >> > fieldValues =3D eg.multenterbox(msg1,title, fieldNames, choice)
> >> > fieldNames has 5 fields.
>
> >> If you just need to convert a tuple to a list, that's easy.  Call the
> >> built-in function list() and pass the tuple as an intializer:
>
> >> >>> choice = ('ksals', '', 'alsdkfj', '3', '')
> >> >>> print choice
>
> >> ('ksals', '', 'alsdkfj', '3', '')>>> choice_list = list(choice)
> >> >>> print choice_list
>
> >> ['ksals', '', 'alsdkfj', '3', '']
>
> >> --
> >> John Gordon                   A is for Amy, who fell down the stairs
> >> gor...@panix.com              B is for Basil, assaulted by bears
> >> -- Edward Gorey, "The Gashlycrumb Tinies"
>
> > This is a small excert to show you what I get
>
> > for choice in easygui.multchoicebox(msg1, title,qstack):
> >             if choice[0] == None:
> >                 print ("No entries made")
> >                 break
>
> >             print("CHOICE IS: ",choice)    .         CHOICE IS:
> > ('', 'ksals', '', '', '')
> >             c=list(choice)
> >             print("C IS: ",c)              .      C IS:  ['(',
> > "'", "'", ',', ' ', "'", 'k', 's', 'a', 'l', 's', "'", ',', ' ', "'",
> > "'", ',', ' ', "'", "'", ',', ' ', "'", "'",
> > ')']
>
> Looks like you have your tuple expression
>         ('ksals', '', 'alsdkfj', '3', '')
> not as a tuple, but as a string. Do you convert it somewhere ?
>
> If you have it as a string, you can use eval() (not safe!) on the string to
> retrieve the tuple, then list() on the tuple to get a list.
>
> --
> Laurent POINTAL - laurent.poin...@laposte.net
> 3 allée des Orangers - 91940 Les Ulis - France
> Tél. 01 69 29 06 59
>
>

Thank you and everyone that helped me.  I did finally figure it out
this morning. I am now converting for my use. I just didn't understand
what I was looking at
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Converting a string to list for submission to easygui multenterb​ox

2012-05-02 Thread Laurent Pointal
ksals wrote:

> On May 1, 5:29 pm, John Gordon  wrote:
>> In <3b5f65c4-cd95-4bb4-94f2-0c69cf2b1...@d20g2000vbh.googlegroups.com>
>> ksals  writes:
>>
>> > The original choice looks like this when I print it:
>> > print(choice)
>> > ('ksals', '', 'alsdkfj', '3', '')
>> > I need to submit these as defaults to a multenterbox. Each entry above
>> > ksals, "", "alsdkfj', 3 , '' need to fill the five fields in the box.
>> > I tried your suggestion so you must be right it is a tuple of 5
>> > strings.  But I need them to work in an instruction like
>> > fieldValues =3D eg.multenterbox(msg1,title, fieldNames, choice)
>> > fieldNames has 5 fields.
>>
>> If you just need to convert a tuple to a list, that's easy.  Call the
>> built-in function list() and pass the tuple as an intializer:
>>
>> >>> choice = ('ksals', '', 'alsdkfj', '3', '')
>> >>> print choice
>>
>> ('ksals', '', 'alsdkfj', '3', '')>>> choice_list = list(choice)
>> >>> print choice_list
>>
>> ['ksals', '', 'alsdkfj', '3', '']
>>
>> --
>> John Gordon   A is for Amy, who fell down the stairs
>> gor...@panix.com  B is for Basil, assaulted by bears
>> -- Edward Gorey, "The Gashlycrumb Tinies"
>>
>>
> This is a small excert to show you what I get
> 
> for choice in easygui.multchoicebox(msg1, title,qstack):
> if choice[0] == None:
> print ("No entries made")
> break
> 
> 
> print("CHOICE IS: ",choice). CHOICE IS:
> ('', 'ksals', '', '', '')
> c=list(choice)
> print("C IS: ",c)  .  C IS:  ['(',
> "'", "'", ',', ' ', "'", 'k', 's', 'a', 'l', 's', "'", ',', ' ', "'",
> "'", ',', ' ', "'", "'", ',', ' ', "'", "'",
> ')']

Looks like you have your tuple expression
('ksals', '', 'alsdkfj', '3', '')
not as a tuple, but as a string. Do you convert it somewhere ? 

If you have it as a string, you can use eval() (not safe!) on the string to 
retrieve the tuple, then list() on the tuple to get a list.


-- 
Laurent POINTAL - laurent.poin...@laposte.net
3 allée des Orangers - 91940 Les Ulis - France
Tél. 01 69 29 06 59

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


Re: Converting a string to list for submission to easygui multenterb​ox

2012-05-01 Thread Cameron Simpson
The tutorial suggests multchoicebox returns an interable of chosen
items, in fact probably a seqeunce. So...

On 01May2012 14:50, ksals  wrote:
| This is a small excert to show you what I get
|
| for choice in easygui.multchoicebox(msg1, title,qstack):
| if choice[0] == None:
| print ("No entries made")
| break

This is the wrong test. "choice" is 

| print("CHOICE IS: ",choice). CHOICE IS:
| ('', 'ksals', '', '', '')

Does this really happen? This code associated with the for loop?

I'd expect easygui.multchoicebox to return a list (or tuple) and
"choice" to be a single string from the list. Did your print() call
happen after the quoted "for" loop or after a statement like this?

  choice = easygui.multchoicebox(msg1, title, qstack)

| c=list(choice)
| print("C IS: ",c)  .  C IS:  ['(',
| "'", "'", ',', ' ', "'", 'k', 's', 'a', 'l', 's', "'", ',', ' ', "'",
| "'", ',', ' ', "'", "'", ',', ' ', "'", "'",
| ')']

Ok, that really does look like "choice" is a string. I'm really very
surprised.

What does this do?

  choices = easygui.multchoicebox(msg1, title, qstack)
  print("type(choices) =", type(choices))
  print("choices =", repr(choices))

Cheers,
-- 
Cameron Simpson  DoD#743
http://www.cskk.ezoshosting.com/cs/

If you lie to the compiler, it will get its revenge.- Henry Spencer
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Converting a string to list for submission to easygui multenterb​ox

2012-05-01 Thread Cameron Simpson
Disclaimer: I have never used esygui.

On 01May2012 21:29, John Gordon  wrote:
| In <3b5f65c4-cd95-4bb4-94f2-0c69cf2b1...@d20g2000vbh.googlegroups.com> ksals 
 writes:
| > The original choice looks like this when I print it:
| 
| > print(choice)
| > ('ksals', '', 'alsdkfj', '3', '')

That's just print() printing a tuple.

| > I need to submit these as defaults to a multenterbox. Each entry above
| > ksals, "", "alsdkfj', 3 , '' need to fill the five fields in the box.
| > I tried your suggestion so you must be right it is a tuple of 5
| > strings.  But I need them to work in an instruction like
| > fieldValues =3D eg.multenterbox(msg1,title, fieldNames, choice)
| > fieldNames has 5 fields.
| 
| If you just need to convert a tuple to a list, that's easy. Call the
| built-in function list() and pass the tuple as an intializer:

Supposedly he should not need to. From:

  http://www.ferg.org/easygui/tutorial.html#contents_item_10.2

the sentence: "The choices are specified in a sequence (a tuple or a
list)."

I do not believe that ksals needs to change anything.

Cheers,
-- 
Cameron Simpson  DoD#743
http://www.cskk.ezoshosting.com/cs/

Uh, this is only temporary...unless it works.   - Red Green
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Converting a string to list for submission to easygui multenterb​ox

2012-05-01 Thread ksals
On May 1, 5:29 pm, John Gordon  wrote:
> In <3b5f65c4-cd95-4bb4-94f2-0c69cf2b1...@d20g2000vbh.googlegroups.com> ksals 
>  writes:
>
> > The original choice looks like this when I print it:
> > print(choice)
> > ('ksals', '', 'alsdkfj', '3', '')
> > I need to submit these as defaults to a multenterbox. Each entry above
> > ksals, "", "alsdkfj', 3 , '' need to fill the five fields in the box.
> > I tried your suggestion so you must be right it is a tuple of 5
> > strings.  But I need them to work in an instruction like
> > fieldValues =3D eg.multenterbox(msg1,title, fieldNames, choice)
> > fieldNames has 5 fields.
>
> If you just need to convert a tuple to a list, that's easy.  Call the
> built-in function list() and pass the tuple as an intializer:
>
> >>> choice = ('ksals', '', 'alsdkfj', '3', '')
> >>> print choice
>
> ('ksals', '', 'alsdkfj', '3', '')>>> choice_list = list(choice)
> >>> print choice_list
>
> ['ksals', '', 'alsdkfj', '3', '']
>
> --
> John Gordon                   A is for Amy, who fell down the stairs
> gor...@panix.com              B is for Basil, assaulted by bears
>                                 -- Edward Gorey, "The Gashlycrumb Tinies"
>
>
This is a small excert to show you what I get

for choice in easygui.multchoicebox(msg1, title,qstack):
if choice[0] == None:
print ("No entries made")
break


print("CHOICE IS: ",choice). CHOICE IS:
('', 'ksals', '', '', '')
c=list(choice)
print("C IS: ",c)  .  C IS:  ['(',
"'", "'", ',', ' ', "'", 'k', 's', 'a', 'l', 's', "'", ',', ' ', "'",
"'", ',', ' ', "'", "'", ',', ' ', "'", "'",
')']
-- 
http://mail.python.org/mailman/listinfo/python-list


RE: Converting a string to list for submission to easygui multenterb​ox

2012-05-01 Thread Prasad, Ramit
> > That looks like a tuple which contains five strings.  

> The original choice looks like this when I print it:
> 
> print(choice)
> ('ksals', '', 'alsdkfj', '3', '')

Based on the print statement, choice is a tuple or a string.
try doing `print(type(choice))`. On the assumption it is a 
tuple and not a string you can convert it by doing:

choice = list(choice)

If it actually is a string I would do:
choice = list( ast.literal_eval( choice ) )


Ramit


Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology
712 Main Street | Houston, TX 77002
work phone: 713 - 216 - 5423

--


This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Re: Converting a string to list for submission to easygui multenterb​ox

2012-05-01 Thread John Gordon
In <3b5f65c4-cd95-4bb4-94f2-0c69cf2b1...@d20g2000vbh.googlegroups.com> ksals 
 writes:

> The original choice looks like this when I print it:

> print(choice)
> ('ksals', '', 'alsdkfj', '3', '')

> I need to submit these as defaults to a multenterbox. Each entry above
> ksals, "", "alsdkfj', 3 , '' need to fill the five fields in the box.
> I tried your suggestion so you must be right it is a tuple of 5
> strings.  But I need them to work in an instruction like
> fieldValues =3D eg.multenterbox(msg1,title, fieldNames, choice)
> fieldNames has 5 fields.

If you just need to convert a tuple to a list, that's easy.  Call the
built-in function list() and pass the tuple as an intializer:

>>> choice = ('ksals', '', 'alsdkfj', '3', '')
>>> print choice
('ksals', '', 'alsdkfj', '3', '')
>>> choice_list = list(choice)
>>> print choice_list
['ksals', '', 'alsdkfj', '3', '']

-- 
John Gordon   A is for Amy, who fell down the stairs
gor...@panix.com  B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"

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


Re: Converting a string to list for submission to easygui multenterb​ox

2012-05-01 Thread ksals
On May 1, 4:26 pm, John Gordon  wrote:
> In <316efebe-7f54-4054-96b1-51c7bb7b7...@f5g2000vbt.googlegroups.com> ksals 
>  writes:
>
> > Please help a newbe.  I have a string returned from an esygui
> > multchoicebox that looks like
> >   this:  ('ksals', '', 'alsdkfj', '3', '') I need to convert this to
> >   this:  ['ksals', '', 'alsdkfj', '3', '']
>
> That looks like a tuple which contains five strings.  But you said it's
> a string, so I'll believe you.
>
> >>> x = "('ksals', '', 'alsdkfj', '3', '')"
> >>> print x
>
> ('ksals', '', 'alsdkfj', '3', '')>>> y = "[%s]" % x[1:-1]
> >>> print y
>
> ['ksals', '', 'alsdkfj', '3', '']
>
> --
> John Gordon                   A is for Amy, who fell down the stairs
> gor...@panix.com              B is for Basil, assaulted by bears
>                                 -- Edward Gorey, "The Gashlycrumb Tinies"
>
>

The original choice looks like this when I print it:

print(choice)
('ksals', '', 'alsdkfj', '3', '')

I need to submit these as defaults to a multenterbox. Each entry above
ksals, "", "alsdkfj', 3 , '' need to fill the five fields in the box.
I tried your suggestion so you must be right it is a tuple of 5
strings.  But I need them to work in an instruction like
fieldValues = eg.multenterbox(msg1,title, fieldNames, choice)
fieldNames has 5 fields.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Converting a string to list for submission to easygui multenterb​ox

2012-05-01 Thread Pedro Kroger
Have you tried to use the function list?:

foo = (1,2,3)
list(foo)

Cheers,

Pedro

--
http://pedrokroger.net



On May 1, 2012, at 5:18 PM, ksals wrote:

> Please help a newbe.  I have a string returned from an esygui
> multchoicebox that looks like
>  this:  ('ksals', '', 'alsdkfj', '3', '') I need to convert this to
>  this:  ['ksals', '', 'alsdkfj', '3', '']
> 
> This is so I can submit this to a multenterbox with 5 fields
> 
> -- 
> http://mail.python.org/mailman/listinfo/python-list

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


Re: Converting a string to list for submission to easygui multenterb​ox

2012-05-01 Thread John Gordon
In <316efebe-7f54-4054-96b1-51c7bb7b7...@f5g2000vbt.googlegroups.com> ksals 
 writes:

> Please help a newbe.  I have a string returned from an esygui
> multchoicebox that looks like
>   this:  ('ksals', '', 'alsdkfj', '3', '') I need to convert this to
>   this:  ['ksals', '', 'alsdkfj', '3', '']

That looks like a tuple which contains five strings.  But you said it's
a string, so I'll believe you.

>>> x = "('ksals', '', 'alsdkfj', '3', '')"
>>> print x
('ksals', '', 'alsdkfj', '3', '')
>>> y = "[%s]" % x[1:-1]
>>> print y
['ksals', '', 'alsdkfj', '3', '']

-- 
John Gordon   A is for Amy, who fell down the stairs
gor...@panix.com  B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"

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


Converting a string to list for submission to easygui multenterb​ox

2012-05-01 Thread ksals
Please help a newbe.  I have a string returned from an esygui
multchoicebox that looks like
  this:  ('ksals', '', 'alsdkfj', '3', '') I need to convert this to
  this:  ['ksals', '', 'alsdkfj', '3', '']

This is so I can submit this to a multenterbox with 5 fields

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


Converting a string to list for submission to easygui multenterbox

2012-05-01 Thread ksals
Please help a newbe.  I have a string returned from an esygui
multchoicebox that looks like
  this:  ('ksals', '', 'alsdkfj', '3', '') I need to convert this to
  this:  ['ksals', '', 'alsdkfj', '3', '']

This is so I can submit this to a multchoicebox with 5 fields
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: string to list when the contents is a list

2010-02-18 Thread Rhodri James

On Thu, 18 Feb 2010 14:52:29 -, nn  wrote:


Wes James wrote:

I have been trying to create a list form a string.  The string will be
a list (this is the contents will look like a list).  i.e. "[]" or
"['a','b']"

The "[]" is simple since I can just check if value == "[]" then return  
[]


But with "['a','b']" I have tried and get:

a="['a','b']"

b=a[1:-1].split(',')

returns

[ " 'a' "," 'b' " ]

when I want it to return ['a','b'].

How can I do this?

thx,

-wes



I am surprised nobody gave you the simple answer yet that may even
work for your situation:

b=a[2:-2].split("','")


Because it's really *very* not robust.  "Harmless" whitespace defeats it  
for starters, and that's one of the most likely things to vary between  
example data and reality.  If you trust your data to be well-formed enough  
for this to work, you might as well use eval() instead.  If you don't,  
parsing is the only sensible answer.


--
Rhodri James *-* Wildebeeste Herder to the Masses
--
http://mail.python.org/mailman/listinfo/python-list


Re: string to list when the contents is a list

2010-02-18 Thread Aahz
In article ,
Wes James   wrote:
>
>try:
>if value=3D=3D'[]' or value=3D=3D'':
>   value=3D[]
>else:
>   no_brackets =3D value[1:-1] # s.strip(' \t[]')
>   c =3D csv.reader([no_brackets], quotechar=3D"'")
>   value=3Dc.next()
>return (value, None)
>except:
>return (value, self.error_message)

Two important points:

* Don't use bare except: clauses

* Put less code in the try: clause to make it easier to track down
problems
-- 
Aahz (a...@pythoncraft.com)   <*> http://www.pythoncraft.com/

"At Resolver we've found it useful to short-circuit any doubt and just
refer to comments in code as 'lies'. :-)"
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: string to list when the contents is a list

2010-02-18 Thread Benjamin Kaplan
On Thu, Feb 18, 2010 at 2:56 PM, Wes James  wrote:
>
> I get an error (when I take the "try" out):
>
> AttributeError: 'function' object has no attribute 'reader'
>

 You have a function called "csv" that's defined after the import csv
statement is executed. That function has no attribute 'reader", so you
get the error. By the way, don't use a bare except- it's bad form
because it hides any other problems you have.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: string to list when the contents is a list

2010-02-18 Thread Tim Chase

import csv

class IS_LIST():
def __init__(self, format='', error_message='must be a list!'):
self.format = format
self.error_message = error_message
def __call__(self, value):
try:
if value=='[]' or value=='':
value=[]
else:
no_brackets = value[1:-1] # s.strip(' \t[]')
c = csv.reader([no_brackets], quotechar="'")
value=c.next()
return (value, None)
except:
return (value, self.error_message)
def formatter(self, value):
return value

I get an error (when I take the "try" out):

AttributeError: 'function' object has no attribute 'reader'


A couple ideas occur to me:

1) you haven't copy/pasted the exact (or entirety of the) code, 
and something you're doing is shadowing the "csv" module


2) are you using Python 2.x or 3.x?  I don't know if the csv 
module has changed in 3.x but it should work in 2.x


The first thing to check would be to pull up a raw python prompt 
and see if your csv module has the expected reader:


  >>> import csv
  >>> csv.reader
  

If not, something likely changed in 3.x and you'd have to inspect 
the docs to see what happened to the reader.


If you get the above evidence of an existing reader, then you're 
likely shadowing it.


-tkc


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


Re: string to list when the contents is a list

2010-02-18 Thread Wes James
On Thu, Feb 18, 2010 at 12:32 PM, Wes James  wrote:
> On Thu, Feb 18, 2010 at 8:18 AM, Tim Chase
>  wrote:
>> Wes James wrote:
> 
>
>>
>> Just to add to the list of solutions I've seen, letting the built-in csv
>> module do the heavy lifting:
>>
>>  >>> s = "['a','b']"
>>  >>> import csv
>>  >>> no_brackets = s[1:-1] # s.strip(' \t[]')
>>  >>> c = csv.reader([no_brackets], quotechar="'")
>>  >>> c.next()
>>  ['a', 'b']


Hmm.  When I put csv.reader in a class:

import csv

class IS_LIST():
def __init__(self, format='', error_message='must be a list!'):
self.format = format
self.error_message = error_message
def __call__(self, value):
try:
if value=='[]' or value=='':
value=[]
else:
no_brackets = value[1:-1] # s.strip(' \t[]')
c = csv.reader([no_brackets], quotechar="'")
value=c.next()
return (value, None)
except:
return (value, self.error_message)
def formatter(self, value):
return value

I get an error (when I take the "try" out):

AttributeError: 'function' object has no attribute 'reader'

Why?

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


Re: string to list when the contents is a list

2010-02-18 Thread Wes James
On Thu, Feb 18, 2010 at 8:18 AM, Tim Chase
 wrote:
> Wes James wrote:


>
> Just to add to the list of solutions I've seen, letting the built-in csv
> module do the heavy lifting:
>
>  >>> s = "['a','b']"
>  >>> import csv
>  >>> no_brackets = s[1:-1] # s.strip(' \t[]')
>  >>> c = csv.reader([no_brackets], quotechar="'")
>  >>> c.next()
>  ['a', 'b']
>
> This also gives you a bit of control regarding how escaping is done, and
> other knobs & dials to twiddle if you need. Additionally, if you have more
> than one string to process coming from an iterable source (such as a file),
> you can just pass that iterator to csv.reader() instead of concocting a
> one-element list.

Thx,  I think this will work for what I want.

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


Re: string to list when the contents is a list

2010-02-18 Thread Tim Chase

Wes James wrote:

I have been trying to create a list form a string.  The string will be
a list (this is the contents will look like a list).  i.e. "[]" or
"['a','b']"

The "[]" is simple since I can just check if value == "[]" then return []

But with "['a','b']" I have tried and get:

a="['a','b']"

b=a[1:-1].split(',')

returns

[ " 'a' "," 'b' " ]

when I want it to return ['a','b'].


Just to add to the list of solutions I've seen, letting the 
built-in csv module do the heavy lifting:


  >>> s = "['a','b']"
  >>> import csv
  >>> no_brackets = s[1:-1] # s.strip(' \t[]')
  >>> c = csv.reader([no_brackets], quotechar="'")
  >>> c.next()
  ['a', 'b']

This also gives you a bit of control regarding how escaping is 
done, and other knobs & dials to twiddle if you need. 
Additionally, if you have more than one string to process coming 
from an iterable source (such as a file), you can just pass that 
iterator to csv.reader() instead of concocting a one-element list.


-tkc


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


Re: string to list when the contents is a list

2010-02-18 Thread nn


Wes James wrote:
> I have been trying to create a list form a string.  The string will be
> a list (this is the contents will look like a list).  i.e. "[]" or
> "['a','b']"
>
> The "[]" is simple since I can just check if value == "[]" then return []
>
> But with "['a','b']" I have tried and get:
>
> a="['a','b']"
>
> b=a[1:-1].split(',')
>
> returns
>
> [ " 'a' "," 'b' " ]
>
> when I want it to return ['a','b'].
>
> How can I do this?
>
> thx,
>
> -wes


I am surprised nobody gave you the simple answer yet that may even
work for your situation:

b=a[2:-2].split("','")
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: string to list when the contents is a list

2010-02-17 Thread Ben Finney
Wes James  writes:

> I have been trying to create a list form a string.  The string will be
> a list (this is the contents will look like a list).  i.e. "[]" or
> "['a','b']"

Pulling back to ask about the larger problem: Are you trying to create
Python data structures from a serialised representation?

There are several well-implemented solutions, including the standard
library modules ‘pickle’ and ‘json’. Do you have control over the choice
of serialisation format?

-- 
 \“I went to court for a parking ticket; I pleaded insanity. I |
  `\   said ‘Your Honour, who in their right mind parks in the passing |
_o__)   lane?’” —Steven Wright |
Ben Finney
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: string to list when the contents is a list

2010-02-17 Thread Matt McCredie
Wes James  gmail.com> writes:

> 
> I have been trying to create a list form a string.  The string will be
> a list (this is the contents will look like a list).  i.e. "[]" or
> "['a','b']"
> 
> The "[]" is simple since I can just check if value == "[]" then return []
> 
> But with "['a','b']" I have tried and get:
> 
> a="['a','b']"
> 
> b=a[1:-1].split(',')
> 
> returns
> 
> [ " 'a' "," 'b' " ]
> 
> when I want it to return ['a','b'].
> 
> How can I do this?


eval will work, but has a safety issue. It also has the issue of evaluating any 
and everything that a user might pass in. 

If you are using python 2.6 check out ast.literal_eval. It uses python's built 
in ast parser to generate an AST and then traverses it to generate a python 
object. Unlike eval though, it will raise an exception if anything other than a 
literal is represented in the string. I have used the same function in python 
2.5 (copied from 2.6) and it works just fine.

Here is a version modified from the code in python 2.6 that should only parse 
lists of strings:

from _ast import List, Str, PyCF_ONLY_AST

def parse(expr, filename='', mode='exec'):
"""
Parse an expression into an AST node.
Equivalent to compile(expr, filename, mode, PyCF_ONLY_AST).
"""
return compile(expr, filename, mode, PyCF_ONLY_AST)


def list_eval(text):
"""
Safely evaluate an expression node or a string containing a Python
expression.  The string or node provided may only consist of the following
Python literal structures: strings, numbers, tuples, lists, dicts, booleans,
and None.
"""

node = parse(text, mode='eval').body
if not isinstance(node, List):
raise ValueError('malformed string')
def _convert(node):
if isinstance(node, Str):
return node.s
raise ValueError('malformed string')

return list(map(_convert, node.elts))




Matt McCredie

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


Re: string to list when the contents is a list

2010-02-17 Thread Steven D'Aprano
On Thu, 18 Feb 2010 00:13:05 +, Rhodri James wrote:

> On Wed, 17 Feb 2010 23:48:38 -, Wes James 
> wrote:
> 
>> I have been trying to create a list form a string.  The string will be
>> a list (this is the contents will look like a list).  i.e. "[]" or
>> "['a','b']"
> 
> If your string is trusted (i.e. goes nowhere near a user), just eval()
> it.

Or use something like YAML or JSON to parse it.

Fredrik Lundh has a simple_eval function which should be safe to use:

http://effbot.org/zone/simple-iterator-parser.htm


But it's fairly simple to parse a simple list like this. Here's a quick 
and dirty version:


def string_to_list(s):
s = s.strip()
if not s.startswith('[') and s.endswith(']'):
raise ValueError
s = s[1:-1].strip()
items = [item.strip() for item in s.split(',')]
for i, item in enumerate(items):
items[i] = dequote(item)
return items


def dequote(s):
for delimiter in ('"""', "'''", '"', "'"):
if s.startswith(delimiter) and s.endswith(delimiter):
n = len(delimiter)
return s[n:-n]
raise ValueError



>>> s = "['a','b']"
>>> print s
['a','b']
>>> string_to_list(s)
['a', 'b']
>>> x = string_to_list(s)
>>> type(x)

>>> x
['a', 'b']



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


Re: string to list when the contents is a list

2010-02-17 Thread Rhodri James

On Wed, 17 Feb 2010 23:48:38 -, Wes James  wrote:


I have been trying to create a list form a string.  The string will be
a list (this is the contents will look like a list).  i.e. "[]" or
"['a','b']"


If your string is trusted (i.e. goes nowhere near a user), just eval() it.

--
Rhodri James *-* Wildebeeste Herder to the Masses
--
http://mail.python.org/mailman/listinfo/python-list


Re: string to list when the contents is a list

2010-02-17 Thread Vlastimil Brom
2010/2/18 Wes James :
> I have been trying to create a list form a string.  The string will be
> a list (this is the contents will look like a list).  i.e. "[]" or
> "['a','b']"
>
> The "[]" is simple since I can just check if value == "[]" then return []
>
> But with "['a','b']" I have tried and get:
>
> a="['a','b']"
>
> b=a[1:-1].split(',')
>
> returns
>
> [ " 'a' "," 'b' " ]
>
> when I want it to return ['a','b'].
>
> How can I do this?
>
> thx,
>
> -wes
> --
> http://mail.python.org/mailman/listinfo/python-list
>

The potentially problematic exec or eval options left aside,
if you really need to do this, you might consider pyparsing; check the example
http://pyparsing.wikispaces.com/file/view/parsePythonValue.py

If you know, the input string will always have this exact format
(single quoted comma separated one-character strings between square
brackets), you might use regular expressions to some extent, e.g.

print re.findall(r"(?<=')\w(?=')", "['a','b','c','b','A']")
['a', 'b', 'c', 'b', 'A']

hth,
  vbr
-- 
http://mail.python.org/mailman/listinfo/python-list


string to list when the contents is a list

2010-02-17 Thread Wes James
I have been trying to create a list form a string.  The string will be
a list (this is the contents will look like a list).  i.e. "[]" or
"['a','b']"

The "[]" is simple since I can just check if value == "[]" then return []

But with "['a','b']" I have tried and get:

a="['a','b']"

b=a[1:-1].split(',')

returns

[ " 'a' "," 'b' " ]

when I want it to return ['a','b'].

How can I do this?

thx,

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


Re: Quick compare string to list

2009-09-30 Thread Steven D'Aprano
On Wed, 30 Sep 2009 11:36:03 -0700, Scooter wrote:

> I'm reading in a text file, and for each line in the file, I'm looking
> for the existence of phrases from a list. The list contains approx. 120
> items currently but will most likely grow. This procedure itself is not
> the main function of my program and only grew out of the need to
> reformat certain phrases I'm finding in a file before re-outputting it.
> But as I suspected, this searching of the lists slows the whole process
> way way down. Was looking for ideas of a better way to do this.
> 
> I basically have
> 
> mylist=[]
> ...
> code that reads in the flat file into string 'flatfileString' ...
> for listitem in mylist:
> if flatfileString.count(listitem):
> ...whatever...I found it.


For starters, why are you bothering to count occurrences of the string if 
you only need a There/Not There answer? That's wasteful... it means the 
code has to walk the entire length of the flatfileString every single 
time. Now, string.count() is likely to be fast because it's written in C, 
but it's not instantaneous. Better is:


for listitem in mylist:
if listitem in flatfileString:
process()


That should show a small improvement, but you can probably do better. 
Here's two more simple approaches worth trying, all untested:

# Use a regex.
r = re.compile('|'.join(mylist))  # item 0 or item 1 or ... 
if r.search(flatfileString):
process()


# Use a loop, re-writing it as a list comprehension for speed.
if any([item in flatfileString for item in mylist]):
process()


# As above, but a generator expression instead.
if any(item in flatfileString for item in mylist):
process()



You will probably find that which approach is faster depends on how many 
items are in mylist.

If none of these approaches are fast enough, you may need to look at a 
more complicated approach, such as Bearophile's suggestion.



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


Re: Quick compare string to list

2009-09-30 Thread Bearophile
Scooter:
> I'm reading in a text file, and for each line in the file, I'm looking
> for the existence of phrases from a list. The list contains approx.
> 120 items currently but will most likely grow. This procedure itself
> is not the main function of my program and only grew out of the need
> to reformat certain phrases I'm finding in a file before re-outputting
> it. But as I suspected, this searching of the lists slows the whole
> process way way down. Was looking for ideas of a better way to do
> this.

Know your basic computer science :-)
http://en.wikipedia.org/wiki/Aho-Corasick_algorithm

There are probably C implementations that can be used from Python,
like:
http://hkn.eecs.berkeley.edu/~dyoo/python/ahocorasick/

Bye,
bearophile
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Quick compare string to list

2009-09-30 Thread Emile van Sebille

On 9/30/2009 11:36 AM Scooter said...

I'm reading in a text file, and for each line in the file, I'm looking
for the existence of phrases from a list. The list contains approx.
120 items currently but will most likely grow. This procedure itself
is not the main function of my program and only grew out of the need
to reformat certain phrases I'm finding in a file before re-outputting
it. But as I suspected, this searching of the lists slows the whole
process way way down. Was looking for ideas of a better way to do
this.

I basically have

mylist=[]
...
code that reads in the flat file into string 'flatfileString'
...
for listitem in mylist:
if flatfileString.count(listitem):
...whatever...I found it.



Whatever you do next to reformat those certain phrases will require a 
second scan which doubles the time involved, and as you don't save the 
count anyway, if mylist were exchange couplets you could use replace 
directly.  Something like:


mylist = [('Chevy','Chevrolet'),
  ('GM','General Motors'),
  (... etc... )
 ]

for wrong,right in mylist:
flatfileString=flatfileString.replace(wrong,right)


Flavor to taste,

Emile

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


Re: Quick compare string to list

2009-09-30 Thread Terry Reedy

Scooter wrote:

I'm reading in a text file, and for each line in the file, I'm looking
for the existence of phrases from a list. The list contains approx.
120 items currently but will most likely grow. This procedure itself
is not the main function of my program and only grew out of the need
to reformat certain phrases I'm finding in a file before re-outputting
it. But as I suspected, this searching of the lists slows the whole
process way way down. Was looking for ideas of a better way to do
this.

I basically have

mylist=[]
...
code that reads in the flat file into string 'flatfileString'
...
for listitem in mylist:
if flatfileString.count(listitem):
...whatever...I found it.


I would try:

turn mylist into my_re and compile
for line in file:
  while search line for first occurence of any phase returns yes:
  process
  reduce line to remainder of line after phrase found
  # assuming no overlaps

tjr

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


Quick compare string to list

2009-09-30 Thread Scooter
I'm reading in a text file, and for each line in the file, I'm looking
for the existence of phrases from a list. The list contains approx.
120 items currently but will most likely grow. This procedure itself
is not the main function of my program and only grew out of the need
to reformat certain phrases I'm finding in a file before re-outputting
it. But as I suspected, this searching of the lists slows the whole
process way way down. Was looking for ideas of a better way to do
this.

I basically have

mylist=[]
...
code that reads in the flat file into string 'flatfileString'
...
for listitem in mylist:
if flatfileString.count(listitem):
...whatever...I found it.

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


Re: String to List Question

2009-07-02 Thread Rhodri James
On Thu, 02 Jul 2009 23:05:46 +0100, Hanna Michelsen   
wrote:



Hi,

I am brand new to python and I love it, but I've been having some trouble
with a file parser that I've been working on. It contains lines that  
start

with a name and then continue with names, nicknames and phone numbers of
people associated with that name. I need to create a list of the names of
people associated with each singular person (the first name in each  
line).
Each name/phone number is separated by a tab but if someone doesn't have  
a

nickname there are two tabs between their name and number.

I've been trying to figure out how to test for two tabs, skip over these
people and move onto the next name but I just can't figure out how that  
will

work in python.


You might find the csv module in the standard library does a lot of the
hard work for you: http://docs.python.org/library/csv.html

You can define yourself a reader that splits the input on tabs, and
then see how long the rows it returns are.  Something like this
(untested):

import csv

for row in csv.reader(open("phone_numbers.txt", "rb"), delimiter='\t'):
if len(row) > 1:
# Do your stuff

--
Rhodri James *-* Wildebeest Herder to the Masses
--
http://mail.python.org/mailman/listinfo/python-list


Re: String to List Question

2009-07-02 Thread Philip Semanchuk


On Jul 2, 2009, at 6:05 PM, Hanna Michelsen wrote:


Hi,

I am brand new to python and I love it, but I've been having some  
trouble
with a file parser that I've been working on. It contains lines that  
start
with a name and then continue with names, nicknames and phone  
numbers of
people associated with that name. I need to create a list of the  
names of
people associated with each singular person (the first name in each  
line).
Each name/phone number is separated by a tab but if someone doesn't  
have a

nickname there are two tabs between their name and number.

I've been trying to figure out how to test for two tabs, skip over  
these
people and move onto the next name but I just can't figure out how  
that will

work in python.

Any help would be greatly appreciated!


Hi Hanna,
Are you familiar with a string's split() function? It sounds like just  
what you need.


http://docs.python.org/library/stdtypes.html#str.split

HTH
Philip

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


String to List Question

2009-07-02 Thread Hanna Michelsen
Hi,

I am brand new to python and I love it, but I've been having some trouble
with a file parser that I've been working on. It contains lines that start
with a name and then continue with names, nicknames and phone numbers of
people associated with that name. I need to create a list of the names of
people associated with each singular person (the first name in each line).
Each name/phone number is separated by a tab but if someone doesn't have a
nickname there are two tabs between their name and number.

I've been trying to figure out how to test for two tabs, skip over these
people and move onto the next name but I just can't figure out how that will
work in python.

Any help would be greatly appreciated!

Thanks,
Hanna
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: string to list conversion

2009-02-19 Thread Srinivas
John,

Try the following code .. hope this helps and solves your problem . I have run 
in the interactive mode
>>> s=''
>>> a=[s,'12']
>>> print a
['', '12']


regards
Srinivas


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


Re: string to list conversion

2009-02-19 Thread MRAB

John Forse wrote:
I need to convert an input string say '' to a list of the form 
['' ,]. If I use list(stringname), I get ['x','x','x','x'] ; 
list.join() is an error;  and str.join() won't use lists. I do need the 
comma after the string. Is there a simple solution?



Have you tried [stringname], eg ['' ,]? :-)

Why do you need the comma? Python permits it but it isn't necessary:

>>> ['' ,]
['']
--
http://mail.python.org/mailman/listinfo/python-list


Re: string to list conversion

2009-02-19 Thread Steve Holden
John Forse wrote:
> I need to convert an input string say '' to a list of the form
> ['' ,]. If I use list(stringname), I get ['x','x','x','x'] ;
> list.join() is an error;  and str.join() won't use lists. I do need the
> comma after the string. Is there a simple solution?

Suppose your input string is s. Just say

s = [s]

Bingo, s is now a list containing the input string as its only element.
But I suspect that's not what you mean ... because you say "I do need
the comma after the string". Do you mean that you want to produce a
string containing "['', ]"?

You might try

s = "['%s', ]" % s

But that's potentially going to give you problems if s contains either
an apostrophe or a quote mark. It depends how you plan to use it. So
what is it you want, exactly, and (if it's not asking too much) why?

regards
 Steve
-- 
Steve Holden+1 571 484 6266   +1 800 494 3119
Holden Web LLC  http://www.holdenweb.com/

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


string to list conversion

2009-02-19 Thread John Forse
I need to convert an input string say '' to a list of the form  
['' ,]. If I use list(stringname), I get ['x','x','x','x'] ;  
list.join() is an error;  and str.join() won't use lists. I do need  
the comma after the string. Is there a simple solution?

Regards
John



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


Re: String To List

2008-03-17 Thread castironpi
On Mar 17, 10:26 am, Jeff Schwab <[EMAIL PROTECTED]> wrote:
> Girish wrote:
> > I have a string a = "['xyz', 'abc']".. I would like to convert it to a
> > list with elements 'xyz' and 'abc'. Is there any simple solution for
> > this??
>
> Do you want:
>
> (1) Specifically to vivify lists formatted as in your example?  If so, why?
>
> (2) To save and restore arbitrary python objects?
>
> (3) To define some kind of configuration file format that you can read
> from Python?

Bar says: Announce your intentions, then contents.  (Form, then
contents.)  == List of two strings.

How does that go into code?

>>> list([str,str])
[, ]
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: String To List

2008-03-17 Thread Jeff Schwab
Girish wrote:
> I have a string a = "['xyz', 'abc']".. I would like to convert it to a
> list with elements 'xyz' and 'abc'. Is there any simple solution for
> this??

Do you want:

(1) Specifically to vivify lists formatted as in your example?  If so, why?

(2) To save and restore arbitrary python objects?

(3) To define some kind of configuration file format that you can read 
from Python?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: String To List

2008-03-17 Thread castironpi
> > > > I have a string a = "['xyz', 'abc']".. I would like to convert it to a
> > > > list with elements 'xyz' and 'abc'. Is there any simple solution for
> > > > this??
> > > > Thanks for the help...
>
> > > eval(a) will do the job, but you have to be very careful about using
> > > that function.  An alternative is
>
> > > [s.strip('\'"') for s in a.strip('[]').split(', ')]
>
> > This will fall over if xyz or abc include any of the characters your
> > stripping/splitting on (e.g if xyz is actually "To be or not to be,
> > that is the question").  Unless you can guarantee they won't, you'll
> > need to write (or rather use) a parser that understands the syntax.
>
> > Iain
>
> Thinking about this some more; could the string module not use a
> simple tokenizer method?  I know that relentlessly adding features to
> built-ins is a bad idea, so I'm not sure if this falls within
> batteries-included, or is actually just adding bulk.  On the one hand,
> it's not difficult to write a simple state-based token parser
> yourself, but on the other it is also quite easy to include a pile of
> bugs when you do.  By simple I mean something like:
>
> def tokenize(string, delim, closing_delim=None, escape_char=None)
>
> which would return a list (or a generator) of all the parts of the
> string enclosed by delim (or which begin with delim and end with
> closing_delim if closing_delim is set), ignoring any delimiters which
> have been escaped by escape_char.   Throw an exception if the string
> is malformed? (odd number of delimiters, or opening/closing delims
> don't match)
>
> In the OP's case, he could get what he want's with a simple:   l =
> a.tokenize("'")

Slippery slope, though, to nested delimiters, and XML after that.
Where does shlex get us?  Do we want to parse "['xyz', 'abc',
['def','ghi']]" any special way?  Are there security concerns past a
really low complexity level, such as recursion overflows?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: String To List

2008-03-17 Thread Tom Stambaugh
It's too bad your inner data items are delimited with an apostrophe (') 
instead a double-quote ("). If they were double-quote, you could do 
something as simple as:

Given:
a = '["xyz", "abc"]'

import simplejson
answer = simplejson.loads(a)

There may be an incantation to simplejson that allows you to use a different 
delimiter. You might be able to provide your own decoder, using the "cls=" 
argument (but I don't think that lets you change the delimiter string). 
Failing that, and depending on your regex/Python prowess, you might be able 
to change the "decoder.py" file within simplejson to do what you want.

As others have observed, a lot depends on the input data. If it really is as 
simple as your example, then the following may do the trick:

a = "['xyz', 'abc']"

answer = map(lambda each: each.strip()[1:-1], a[1:-1].split(','))

This at least has no "eval", and so you need not fear applying it to unknown 
data (it will just break).

The answer that works best for you may perhaps be somewhere in the middle.

"Girish" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
>I have a string a = "['xyz', 'abc']".. I would like to convert it to a
> list with elements 'xyz' and 'abc'. Is there any simple solution for
> this??
> Thanks for the help...
> -- 
> http://mail.python.org/mailman/listinfo/python-list
> 



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


Re: String To List

2008-03-17 Thread Iain King
On Mar 17, 9:27 am, Iain King <[EMAIL PROTECTED]> wrote:
> On Mar 17, 6:56 am, Dan Bishop <[EMAIL PROTECTED]> wrote:
>
> > On Mar 17, 1:15 am, Girish <[EMAIL PROTECTED]> wrote:
>
> > > I have a string a = "['xyz', 'abc']".. I would like to convert it to a
> > > list with elements 'xyz' and 'abc'. Is there any simple solution for
> > > this??
> > > Thanks for the help...
>
> > eval(a) will do the job, but you have to be very careful about using
> > that function.  An alternative is
>
> > [s.strip('\'"') for s in a.strip('[]').split(', ')]
>
> This will fall over if xyz or abc include any of the characters your
> stripping/splitting on (e.g if xyz is actually "To be or not to be,
> that is the question").  Unless you can guarantee they won't, you'll
> need to write (or rather use) a parser that understands the syntax.
>
> Iain


Thinking about this some more; could the string module not use a
simple tokenizer method?  I know that relentlessly adding features to
built-ins is a bad idea, so I'm not sure if this falls within
batteries-included, or is actually just adding bulk.  On the one hand,
it's not difficult to write a simple state-based token parser
yourself, but on the other it is also quite easy to include a pile of
bugs when you do.  By simple I mean something like:

def tokenize(string, delim, closing_delim=None, escape_char=None)

which would return a list (or a generator) of all the parts of the
string enclosed by delim (or which begin with delim and end with
closing_delim if closing_delim is set), ignoring any delimiters which
have been escaped by escape_char.   Throw an exception if the string
is malformed? (odd number of delimiters, or opening/closing delims
don't match)

In the OP's case, he could get what he want's with a simple:   l =
a.tokenize("'")

The point of this ramble not being that this is a how to solve the
OP's question, but wondering if it would be a good inclusion to the
language in general.  Or there's actually a module which already does
it that I couldn't find and I'm an idiot...

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


Re: String To List

2008-03-17 Thread George Sakkis
On Mar 17, 3:22 am, Paul Rubin  wrote:
> Girish <[EMAIL PROTECTED]> writes:
> > I have a string a = "['xyz', 'abc']".. I would like to convert it to a
> > list with elements 'xyz' and 'abc'. Is there any simple solution for
> > this??
> > Thanks for the help...
>
> Be careful about using eval, if the string came from a potentially
> hostile source.  Maybe what you really want is JSON, which has
> python-like syntax but a bunch of safe parsers.

Or take a look at a restricted safe eval variant  (e.g.
http://groups.google.com/group/comp.lang.python/browse_frm/thread/262d479569b1712e)

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


Re: String To List

2008-03-17 Thread Iain King
On Mar 17, 6:56 am, Dan Bishop <[EMAIL PROTECTED]> wrote:
> On Mar 17, 1:15 am, Girish <[EMAIL PROTECTED]> wrote:
>
> > I have a string a = "['xyz', 'abc']".. I would like to convert it to a
> > list with elements 'xyz' and 'abc'. Is there any simple solution for
> > this??
> > Thanks for the help...
>
> eval(a) will do the job, but you have to be very careful about using
> that function.  An alternative is
>
> [s.strip('\'"') for s in a.strip('[]').split(', ')]

This will fall over if xyz or abc include any of the characters your
stripping/splitting on (e.g if xyz is actually "To be or not to be,
that is the question").  Unless you can guarantee they won't, you'll
need to write (or rather use) a parser that understands the syntax.

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


Re: String To List

2008-03-17 Thread Paul Rubin
Girish <[EMAIL PROTECTED]> writes:
> I have a string a = "['xyz', 'abc']".. I would like to convert it to a
> list with elements 'xyz' and 'abc'. Is there any simple solution for
> this??
> Thanks for the help...

Be careful about using eval, if the string came from a potentially
hostile source.  Maybe what you really want is JSON, which has
python-like syntax but a bunch of safe parsers.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: String To List

2008-03-17 Thread Dan Bishop
On Mar 17, 1:15 am, Girish <[EMAIL PROTECTED]> wrote:
> I have a string a = "['xyz', 'abc']".. I would like to convert it to a
> list with elements 'xyz' and 'abc'. Is there any simple solution for
> this??
> Thanks for the help...

eval(a) will do the job, but you have to be very careful about using
that function.  An alternative is

[s.strip('\'"') for s in a.strip('[]').split(', ')]
-- 
http://mail.python.org/mailman/listinfo/python-list


String To List

2008-03-16 Thread Girish
I have a string a = "['xyz', 'abc']".. I would like to convert it to a
list with elements 'xyz' and 'abc'. Is there any simple solution for
this??
Thanks for the help...
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Convert String to list of chars

2007-01-27 Thread Vlad Dogaru
On Jan 27, 9:18 am, Neil Cerutti <[EMAIL PROTECTED]> wrote:
> On 2007-01-27, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
>
> > How can I convert a string to a char list?
> > for example
>
> > "hello" --> ['h','e','l','l','o']
>
> > I have been searching but I can't find my answers
> list("hello")

Wow, I've been using [c for c in 'hello'] for as long as I can remember
needing it. Thanks!

Vlad

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


Re: Convert String to list of chars

2007-01-26 Thread Neil Cerutti
On 2007-01-27, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
> How can I convert a string to a char list?
> for example
>
> "hello" --> ['h','e','l','l','o']
>
> I have been searching but I can't find my answers

list("hello")
-- 
http://mail.python.org/mailman/listinfo/python-list


Convert String to list of chars

2007-01-26 Thread juanefren
How can I convert a string to a char list?
for example

"hello" --> ['h','e','l','l','o']

I have been searching but I can't find my answers

thanks

-- 
Juan Efrén Castillo Encinas
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: string to list of numbers conversion

2006-11-10 Thread Tim Williams
On 5 Nov 2006 04:34:32 -0800, [EMAIL PROTECTED]
<[EMAIL PROTECTED]> wrote:
> Hi,
>   I have a string '((1,2), (3,4))' and I want to convert this into a
> python tuple of numbers. But I do not want to use eval() because I do
> not want to execute any code in that string and limit it to list of
> numbers.
>   Is there any alternative way?
>

?? I want to convert this into a python tuple of numbers ??

Do you want a python tuple with those numbers ie (1,2, 3,4),  or a
direct evaluation giving a tuple of tuples with those numbers, ie
((1,2), (3,4))

If the former then:

>>> a, l = '((1,2), (3,4), (-5,-6),(12,-13), (a,b), (0.1,0.2))', []
>>> for c in a.split(','):
... try:
... c = c.replace('(','').replace(')','')
... if '.' in c: l.append(float(c))
... else:   l.append(int(c))
... except: pass
...
>>> tuple(l)
(1, 2, 3, 4, -5, -6, 12, -13, 0.10001, 0.20001)
>>>

Its not so good with floats, but if you are only expecting integers you can use.

>>> a, l = '((1,2), (3,4), (-5,-6),(12,-13), (a,b), (0.1,0.2))', []
>>> for c in a.split(','):
... try: l.append(int(c.replace('(','').replace(')','')))
... except: pass
...
>>> tuple(l)
(1, 2, 3, 4, -5, -6, 12, -13)
>>>

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


Re: string to list of numbers conversion

2006-11-08 Thread henning
[EMAIL PROTECTED] skrev:

> Hi,
>   I have a string '((1,2), (3,4))' and I want to convert this into a
> python tuple of numbers.

I think your question is deeper and more natural than is clear from the
many recepies given so far in this thread, so I'll take on another
point of view,

>From a language design perspective, there is no reason why not the
parsing capacity of the Python interpreter would be accessible in a
modular fashion to the user/programmer. E.g used like this:

I an imaginable Python, define you expect for an answer. In this case:
(1)
# import junctions, types from maybefuture:-)
string = ((1,2), (3,4))
type a = tuple a | int
myTuple = eval(string, goal=a)


Obviously, if you expect _only_ the given form, then this might be
better:

(2)
# import types from maybefuture:-)
type a = ((int,int),(int,int))
myTuple = eval(string, goal=a)

Note the use of a "a|b" in line 2 (I think Perl 6 is among the few
programming languages giving a reasonable semantics to junctions so
far).

Version 2 above sholud not be a big addition to Python conceptually.
Motivation:
It is easy to think clearly about.
It makes it easier to use eval safely and makes code more readable.

This is a topic of interest to me, so feel free to post either on list
or directly to me.

Thanks/Henning

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


Re: string to list of numbers conversion

2006-11-06 Thread Fredrik Lundh
[EMAIL PROTECTED] wrote:

>   I have a string '((1,2), (3,4))' and I want to convert this into a
> python tuple of numbers. But I do not want to use eval() because I do
> not want to execute any code in that string and limit it to list of
> numbers.

here's yet another approach:

http://online.effbot.org/2005_11_01_archive.htm#simple-parser-1

also see:

http://online.effbot.org/2005_11_01_archive.htm#simple-parser-3



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


Re: string to list of numbers conversion

2006-11-06 Thread Paul McGuire
"[EMAIL PROTECTED]" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
> Hi,
>  I have a string '((1,2), (3,4))' and I want to convert this into a
> python tuple of numbers. But I do not want to use eval() because I do
> not want to execute any code in that string and limit it to list of
> numbers.
>  Is there any alternative way?
>
> Thanks.
> Suresh
>

Pyparsing comes with an example that parses strings representing lists. 
Here's that example, converted to parsing only tuples of numbers.  Note that 
this does not presume that tuples are only pairs, but can be any number of 
numeric values, nested to any depth, and with arbitrary whitespace, etc. 
This grammar also includes converters by type, so that ints come out as 
ints, and floats as floats.  (This grammar doesn't handle empty tuples, but 
it does handle tuples that include an extra ',' after the last tuple 
element.)

-- Paul
Download pyparsing at http://sourceforge.net/projects/pyparsing/ .


from pyparsing import *

integer = (Word(nums)|Word('-+',nums)).setName("integer")
real = Combine(integer + "." + Optional(Word(nums))).setName("real")
tupleStr = Forward().setName("tuple")
tupleItem = real | integer | tupleStr
tupleStr << ( Suppress("(") + delimitedList(tupleItem) +
   Optional(Suppress(",")) + Suppress(")") )

# add parse actions to do conversion during parsing
integer.setParseAction( lambda toks: int(toks[0]) )
real.setParseAction( lambda toks: float(toks[0]) )
tupleStr.setParseAction( lambda toks: tuple(toks) )

s = '((1,2), (3,4), (-5,9.2),)'
print tupleStr.parseString(s)[0]

Gives:
((1, 2), (3, 4), (-5, 9.1993))


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


Re: string to list of numbers conversion

2006-11-06 Thread Frederic Rentsch
[EMAIL PROTECTED] wrote:
> Hi,
>   I have a string '((1,2), (3,4))' and I want to convert this into a
> python tuple of numbers. But I do not want to use eval() because I do
> not want to execute any code in that string and limit it to list of
> numbers.
>   Is there any alternative way?
>
> Thanks.
> Suresh
>
>   
s = '((1,2), (3,4))'
separators = re.compile ('\(\s*\(|\)\s*,\s*\(|\)\s*\)')
tuple ([(float (n[0]), float (n[1])) for n in [pair.split (',') for pair 
in separators.split (s) if pair]])
((1.0, 2.0), (3.0, 4.0))

Frederic


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


Re: string to list of numbers conversion

2006-11-05 Thread Peter Otten
[EMAIL PROTECTED] wrote:

> This recipe fails when negative numbers are used.
> 
> safe_eval('(12, -12)')
> *** Unsafe_Source_Error: Line 1.  Unsupported source construct:
> compiler.ast.UnarySub
> 
> But, I think it could  be easily fixed for somebody who understands the
> script. 

I think that somebody could be you.

> Can somebody help. 

Start with

class SafeEval(object):
# ...
def visitUnarySub(self, node, **kw):
return -node.expr.value

and then add some error handling.

Peter

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


Re: string to list of numbers conversion

2006-11-05 Thread bearophileHUGS

[EMAIL PROTECTED] wrote:
> Hi,
>   I have a string '((1,2), (3,4))' and I want to convert this into a
> python tuple of numbers. But I do not want to use eval() because I do
> not want to execute any code in that string and limit it to list of
> numbers.
>   Is there any alternative way?

This is a possibile solution, no input errors are taken into account:

>>> s = '((1,2), (3,4), (-5,9.2))'
>>> from string import maketrans
>>> tab = maketrans("(), ", " "*4)
>>> s.translate(tab)
'  1 23 4-5 9.2  '
>>> l = s.translate(tab).split()
>>> l
['1', '2', '3', '4', '-5', '9.2']
>>> l2 = map(float, l)
>>> l2
[1.0, 2.0, 3.0, 4.0, -5.0, 9.1993]
>>> # This is partition(l2, 2)
>>> [l2[i:i+2] for i in xrange(0, len(l2), 2)]
[[1.0, 2.0], [3.0, 4.0], [-5.0, 9.1993]]

Bye,
bearophile

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


Re: string to list of numbers conversion

2006-11-05 Thread [EMAIL PROTECTED]
Peter, Thanks.

This recipe fails when negative numbers are used.

safe_eval('(12, -12)')
*** Unsafe_Source_Error: Line 1.  Unsupported source construct:
compiler.ast.UnarySub

But, I think it could  be easily fixed for somebody who understands the
script. Can somebody help.

Thanks.
Suresh
Peter Otten wrote:
> [EMAIL PROTECTED] wrote:
>
> >   I have a string '((1,2), (3,4))' and I want to convert this into a
> > python tuple of numbers. But I do not want to use eval() because I do
> > not want to execute any code in that string and limit it to list of
> > numbers.
> >   Is there any alternative way?
>
> http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/364469
> 
> Peter

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


Re: string to list of numbers conversion

2006-11-05 Thread Peter Otten
[EMAIL PROTECTED] wrote:

>   I have a string '((1,2), (3,4))' and I want to convert this into a
> python tuple of numbers. But I do not want to use eval() because I do
> not want to execute any code in that string and limit it to list of
> numbers.
>   Is there any alternative way?

http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/364469

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


Re: string to list of numbers conversion

2006-11-05 Thread Gerard Flanagan

[EMAIL PROTECTED] wrote:
> Hi,
>   I have a string '((1,2), (3,4))' and I want to convert this into a
> python tuple of numbers. But I do not want to use eval() because I do
> not want to execute any code in that string and limit it to list of
> numbers.
>   Is there any alternative way?


Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MSC v.1310 32 bit
(Intel)] on win
32
Type "help", "copyright", "credits" or "license" for more information.
>>> s = '((1,2), (3,4))'
>>> s = filter(lambda char: char not in ')(', s)
>>> s
'1,2, 3,4'
>>> s = s.split(',')
>>> s
['1', '2', ' 3', '4']
>>> s = map(float, s)
>>> s
[1.0, 2.0, 3.0, 4.0]
>>> t1 = s[::2]
>>> t1
[1.0, 3.0]
>>> t2 = s[1::2]
>>> t2
[2.0, 4.0]
>>> zip(t1, t2)
[(1.0, 2.0), (3.0, 4.0)]
>>>

Gerard

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


string to list of numbers conversion

2006-11-05 Thread [EMAIL PROTECTED]
Hi,
  I have a string '((1,2), (3,4))' and I want to convert this into a
python tuple of numbers. But I do not want to use eval() because I do
not want to execute any code in that string and limit it to list of
numbers.
  Is there any alternative way?

Thanks.
Suresh

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


Re: how to convert string to list or tuple

2005-06-02 Thread Duncan Booth
Ruud de Jong wrote:

> Steven Bethard schreef:
>> But unless the person eval-ing your code *only* writes immaculate
>> code I can see that you can probably screw them. ;)  I wonder why 
>> __subclasses__ isn't a restricted attribute...  Is it ever used for 
>> something that isn't evil? ;)
>> 
>> STeVe
> 
> Completely off topic, but I just cannot resist showing off.
> Some time ago I used __subclasses__ in a way that is not evil. I
> think. 
> 
> The details are described in the following thread:
> http://groups.google.nl/group/comp.lang.python/browse_thread/thread/5c1
> ccb986c66cdc1/ 
> 
> A summary: I used __subclasses__ to apply the Chain-of-Responsibility
> pattern to object creation. The code would appear to instantiate
> an object of the root of a class hierarchy, but the actual object
> that was created would be an instance of a subclass.
> 
> So to get back to your question: yes, there are non-evil
> uses for __subclasses__. Weird perhaps, but non-evil.
> Non-standard, sure . Too clever for my own good, very likely.

I've done almost exactly the same thing. The base class uses __subclasses__ 
to find the best matching subclass based on the factory parameters. In my 
case I was retrieving files from the web, so I had a base Handler class and 
created HtmlHandler, ImageHandler &c.

class Handler(object):
'''Class to process files'''
__map = {}

@classmethod
def _resolveClass(klass, isdir, name):
map = Handler.__map
if not map:
for c in klass.__subclasses__():
for ext in c.Extensions:
map['.'+ext.lower()] = c

if isdir:
klass = FolderHandler
else:
ext = os.path.splitext(name)[1].lower()
if ext not in map:
map[ext] = DefaultHandler

klass = map[ext]
return klass(name)

@classmethod
def fromPathname(klass, name, path, uri, db):
isdir = os.path.isdir(os.path.join(path, name))
obj = klass._resolveClass(isdir, name)
obj._initialize(name, path, uri, db)
return obj

@classmethod
def fromUrl(klass, uri, text, db=None):
   ... and so on ...

and then subclasses such as:

class ImageHandler(Handler):
Extensions = ('jpg', 'jpeg', 'gif', 'png')
type = 'Image'

class DefaultHandler(Handler):
Extensions = ('',)
type = 'Ignored'

This also contains the only code I think I've written with a class 
definition in a for loop:

# General categories
EXTENSIONS = {
'js': 'javascript',
'php': 'php',
'doc': 'Word Document',
'xls': 'Spreadsheet',
'ppt': 'Powerpoint',
'css': 'Stylesheet',
'swf': 'Flash',
'pdf': 'File',
'rtf': 'File',
'zip': 'File',
}

Classes = []
for ext in EXTENSIONS:
class GeneralHandler(Handler):
Extensions = (ext,)
type = EXTENSIONS[ext]

Classes.append(GeneralHandler)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how to convert string to list or tuple

2005-06-01 Thread Ruud de Jong
Steven Bethard schreef:
> But unless the person eval-ing your code *only* writes immaculate code I 
> can see that you can probably screw them. ;)  I wonder why 
> __subclasses__ isn't a restricted attribute...  Is it ever used for 
> something that isn't evil? ;)
> 
> STeVe

Completely off topic, but I just cannot resist showing off.
Some time ago I used __subclasses__ in a way that is not evil. I think.

The details are described in the following thread:
http://groups.google.nl/group/comp.lang.python/browse_thread/thread/5c1ccb986c66cdc1/

A summary: I used __subclasses__ to apply the Chain-of-Responsibility
pattern to object creation. The code would appear to instantiate
an object of the root of a class hierarchy, but the actual object
that was created would be an instance of a subclass.

So to get back to your question: yes, there are non-evil
uses for __subclasses__. Weird perhaps, but non-evil.
Non-standard, sure . Too clever for my own good, very likely.

Regards,

Ruud

-- 
Ruud de Jong

'@'.join('.'.join(s) for s in (['ruud','de','jong'],['tiscali','nl']))
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how to convert string to list or tuple

2005-06-01 Thread Steven Bethard
Duncan Booth wrote:
> Steven Bethard wrote:
> 
> 
>>Interestingly, I don't seem to be able to create a file object as a 
>>class attribute in restricted mode:
>>
>>py> class C(object):
>>... def __init__(self):
>>... self.f = file('temp.txt', 'w')
>>...
>>py> eval('''[ cls for cls in
>>{}.__class__.__bases__[0].__subclasses__() if cls.__name__ ==
>>'C'][0]().f.write("stuff")''', dict(__builtins__=None)) Traceback
>>(most recent call last): 
>>   File "", line 1, in ?
>>   File "", line 0, in ?
>>AttributeError: 'C' object has no attribute 'f'
>>py> eval('''[ cls for cls in
>>{}.__class__.__bases__[0].__subclasses__() if cls.__name__ ==
>>'C'][0]().__dict__''', dict(__builtins__=None)) {}
> 
> Weird. I copied and paste your class and eval exactly (apart from deleting 
> the ... prompts) and it worked exactly as expected: writing 'stuff' to 
> temp.txt. (Python 2.4)

So, I played around with this a little bit.  If I start up a new 
interpreter and type it in like above, I get the behavior you do.  What 
I had actually done (abbreviated) was:

py> class C(object):
... pass
...
py> class C(object):
... def __init__(self):
... self.f = file('temp.txt', 'w')
...
py> eval('''[ cls for cls in {}.__class__.__bases__[0].__subclasses__() 
if cls.__name__ == 'C'][0]().f.write("stuff")''', dict(__builtins__=None))
Traceback (most recent call last):
   File "", line 1, in ?
   File "", line 0, in ?
AttributeError: 'C' object has no attribute 'f'

And the problem with this is that both __main__.C objects are now 
subclasses of object:

py> eval('''[ cls for cls in {}.__class__.__bases__[0].__subclasses__() 
if cls.__name__ == 'C']''', dict(__builtins__=None))
[, ]

So I was getting the wrong __main__.C object.  Sorry for the confusion!

Now, even using this technique, *your* code can't call the file constructor:

py> class C(object):
... def __init__(self):
... self.file = file
...
py> eval('''[ cls for cls in {}.__class__.__bases__[0].__subclasses__() 
if cls.__name__ == 'C'][-1]().file("temp.txt", "w")''', 
dict(__builtins__=None))
Traceback (most recent call last):
   File "", line 1, in ?
   File "", line 0, in ?
IOError: file() constructor not accessible in restricted mode

But unless the person eval-ing your code *only* writes immaculate code I 
can see that you can probably screw them. ;)  I wonder why 
__subclasses__ isn't a restricted attribute...  Is it ever used for 
something that isn't evil? ;)

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


Re: how to convert string to list or tuple

2005-06-01 Thread Fuzzyman
flyaflya wrote:
> a = "(1,2,3)"
> I want convert a to tuple:(1,2,3),but tuple(a) return ('(', '1', ',',
> '2', ',', '3', ')') not (1,2,3)

Probably a bit late... but there's always listquote - It's part of the
pythonutils module.

http://www.voidspace.org.uk/python/pythonutils.html

It will turn strings to lists, including nested lists.

Best Regards,

Fuzzy
http://www.voidspace.org.uk/python

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


Re: how to convert string to list or tuple

2005-06-01 Thread Duncan Booth
Steven Bethard wrote:

> Interestingly, I don't seem to be able to create a file object as a 
> class attribute in restricted mode:
> 
> py> class C(object):
> ... def __init__(self):
> ... self.f = file('temp.txt', 'w')
> ...
> py> eval('''[ cls for cls in
> {}.__class__.__bases__[0].__subclasses__() if cls.__name__ ==
> 'C'][0]().f.write("stuff")''', dict(__builtins__=None)) Traceback
> (most recent call last): 
>File "", line 1, in ?
>File "", line 0, in ?
> AttributeError: 'C' object has no attribute 'f'
> py> eval('''[ cls for cls in
> {}.__class__.__bases__[0].__subclasses__() if cls.__name__ ==
> 'C'][0]().__dict__''', dict(__builtins__=None)) {}
> 

Weird. I copied and paste your class and eval exactly (apart from deleting 
the ... prompts) and it worked exactly as expected: writing 'stuff' to 
temp.txt. (Python 2.4)

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


Re: how to convert string to list or tuple

2005-05-31 Thread Steven Bethard
Duncan Booth wrote:
> e.g. Assuming that the MyDatabase class does something nasty to a file:
> 
class MyDatabase(object):
> 
> def __init__(self, filename):
> self.filename = filename
> def initialise(self):
> print "Splat %s" % self.filename
> 
eval('''[ cls for cls in {}.__class__.__bases__[0].__subclasses__()
> 
> if 'MyDatabase' in `cls` 
> ][0]('importantfile').initialise()''', dict(__builtins__=None))
> Splat importantfile

Interestingly, I don't seem to be able to create a file object as a 
class attribute in restricted mode:

py> class C(object):
... def __init__(self):
... self.f = file('temp.txt', 'w')
...
py> eval('''[ cls for cls in {}.__class__.__bases__[0].__subclasses__() 
if cls.__name__ == 'C'][0]().f.write("stuff")''', dict(__builtins__=None))
Traceback (most recent call last):
   File "", line 1, in ?
   File "", line 0, in ?
AttributeError: 'C' object has no attribute 'f'
py> eval('''[ cls for cls in {}.__class__.__bases__[0].__subclasses__() 
if cls.__name__ == 'C'][0]().__dict__''', dict(__builtins__=None))
{}

I don't get an error for calling the file constructor, but the f 
attribute is never set AFAICT.

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


Re: how to convert string to list or tuple

2005-05-31 Thread Duncan Booth
Steven Bethard wrote:

> Duncan Booth wrote:
>> any new style class you have defined and call any of its methods with
>> whatever arguments I wish.
> 
> Any new style class that I've defined?  Or just any one I pass in as 
> part of dict(__builtins__=None, ...)?  If the former, could you 
> elaborate?  If the latter, then yes, I can see the problem.  However
> for the case where all you pass in is dict(__builtins__=None), is
> there still a risk?  Note that in the OP's case, all that is necessary
> is constant parsing, so no names need to be available.
> 
Any new style class you have defined is accessible through 
object.__subclasses__(), and as I showed object itself is always accessible 
through {}.__class__.__bases__[0].

I'm assuming that the source code for your program is available. That means 
I can find the name of an interesting class which has a method that does 
something destructive, and call it.

e.g. Assuming that the MyDatabase class does something nasty to a file:

>>> class MyDatabase(object):
def __init__(self, filename):
self.filename = filename
def initialise(self):
print "Splat %s" % self.filename


>>> eval('''[ cls for cls in {}.__class__.__bases__[0].__subclasses__()
if 'MyDatabase' in `cls` 
][0]('importantfile').initialise()''', dict(__builtins__=None))
Splat importantfile


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


Re: how to convert string to list or tuple

2005-05-30 Thread Steven Bethard
Duncan Booth wrote:
> Steven Bethard wrote:
> 
>>But you can try it at home if you set __builtins__ to something other 
>>than the default:
>>
>>py> eval("""__import__("os").system('echo "hello"')""", 
>>dict(__builtins__=None))
>>Traceback (most recent call last):
>>   File "", line 1, in ?
>>   File "", line 0, in ?
>>NameError: name '__import__' is not defined
>>
[snip]
>>
>>I know there have been security holes in this technique before, but I 
>>looked at the archives, and all the old ones I found have been
>>patched. 
>>  (Or at least I wasn't able to reproduce them.)
> 
> I guess you are referring to things like this not working when you use eval 
> with an empty __builtins__:
> 
> eval('''[ cls for cls in {}.__class__.__bases__[0].__subclasses__()
>if '_Printer' in `cls` 
> ][0]._Printer__setup.func_globals['__builtins__']['__import__']''',
>   dict(__builtins__=None))
> 
> That gets blocked because func_globals is a 'restricted attribute', so I 
> can't get directly at __import__ that way

Among other things, yes, that's one of the big ones.  func_globals is 
inaccessible.  Also, IIRC the file constructor is inaccessible.

> but what I can do is to access 
> any new style class you have defined and call any of its methods with 
> whatever arguments I wish.

Any new style class that I've defined?  Or just any one I pass in as 
part of dict(__builtins__=None, ...)?  If the former, could you 
elaborate?  If the latter, then yes, I can see the problem.  However for 
the case where all you pass in is dict(__builtins__=None), is there 
still a risk?  Note that in the OP's case, all that is necessary is 
constant parsing, so no names need to be available.

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


Re: how to convert string to list or tuple

2005-05-30 Thread Duncan Booth
Steven Bethard wrote:

>> Have you tried giving it the string '__import__("os").system("rm -rf
>> *")'? [Don't try that at home children!]
> 
> But you can try it at home if you set __builtins__ to something other 
> than the default:
> 
> py> eval("""__import__("os").system('echo "hello"')""", 
> dict(__builtins__=None))
> Traceback (most recent call last):
>File "", line 1, in ?
>File "", line 0, in ?
> NameError: name '__import__' is not defined
> 
> If you're just doing work with constants, the lack of access to any 
> builtins is ok:
> 
> py> eval("(1,2,3)", dict(__builtins__=None))
> (1, 2, 3)
> 
> I know there have been security holes in this technique before, but I 
> looked at the archives, and all the old ones I found have been
> patched. 
>   (Or at least I wasn't able to reproduce them.)
> 
I guess you are referring to things like this not working when you use eval 
with an empty __builtins__:

eval('''[ cls for cls in {}.__class__.__bases__[0].__subclasses__()
   if '_Printer' in `cls` 
][0]._Printer__setup.func_globals['__builtins__']['__import__']''',
  dict(__builtins__=None))

That gets blocked because func_globals is a 'restricted attribute', so I 
can't get directly at __import__ that way, but what I can do is to access 
any new style class you have defined and call any of its methods with 
whatever arguments I wish.

Even with the big holes patched you are going to find it pretty hard to 
write a safe program that uses eval on untrusted strings. The only way to 
go is to filter the AST (or possibly the bytecode).
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how to convert string to list or tuple

2005-05-29 Thread Steven Bethard
Duncan Booth wrote:
> Dan Bishop wrote:
>> Or if you do use eval, don't give it access to any names.
[snip]
>> os.system("rm -rf *")
>> Traceback (most recent call last):
>>   File "", line 1, in ?
>>   File "", line 0, in ?
>> NameError: name 'os' is not defined
> 
> Have you tried giving it the string '__import__("os").system("rm -rf *")'?
> [Don't try that at home children!]

But you can try it at home if you set __builtins__ to something other 
than the default:

py> eval("""__import__("os").system('echo "hello"')""", 
dict(__builtins__=None))
Traceback (most recent call last):
   File "", line 1, in ?
   File "", line 0, in ?
NameError: name '__import__' is not defined

If you're just doing work with constants, the lack of access to any 
builtins is ok:

py> eval("(1,2,3)", dict(__builtins__=None))
(1, 2, 3)

I know there have been security holes in this technique before, but I 
looked at the archives, and all the old ones I found have been patched. 
  (Or at least I wasn't able to reproduce them.)

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


Re: how to convert string to list or tuple

2005-05-29 Thread John Roth

"Duncan Booth" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
> Dan Bishop wrote:
>
>> Simon Brunning wrote:
>>> [...]
>>
>> Or if you do use eval, don't give it access to any names.
>>
>>> [...]
>> os.system("rm -rf *")
>> Traceback (most recent call last):
>>   File "", line 1, in ?
>>   File "", line 0, in ?
>> NameError: name 'os' is not defined
>>
> Have you tried giving it the string '__import__("os").system("rm -rf *")'?
> [Don't try that at home children!]
>
> Even if you take steps to avoid that working by hiding the builtins, there
> are still too many ways to do nasty things with eval for it ever to be
> safe.

There was a posting here Nov 5, 2003 by Huaiyu Zhu at IBM Almaden
that shows how to do eval type stuff safely. The basic notion is to use the
compiler and then check the ast to see if the result fits the straitjacket 
you
want to put it into. Pass / Fail; trying to fix it up if it's "close" is 
usually a
real bad idea.

He gives an example, and there's a much more extensive set of working
code in the taBase.py module of PyFit that handles lists, tuples and
dicts which contain arbitrary literals including complex and arbitrarily 
nested
lists, tuples and dicts.

--- code snippet starts here 

def _safeEval(self, s):
"""
Evaluate strings that only contain the following structures:
const,  tuple,  list,   dict
Taken from c.l.py newsgroup posting Nov 5, 2003 by Huaiyu Zhu at IBM 
Almaden
"""
#print "in _safeEval. input: '%s'" % s
node1 = compiler.parse(s)

# !!! special case of attempting to compile a lone string
if node1.doc is not None and len(node1.node.nodes) == 0:
#print "in _safeEval. string: '%s' found as docstring" % 
node1.doc
return node1.doc

#print "in _safeEval. nodes: '%s'" % (node1,)
stmts = node1.node.nodes
assert len(stmts) == 1
node = compiler.parse(s).node.nodes[0]
assert node.__class__ == compiler.ast.Discard
nodes = node.getChildNodes()
assert len(nodes) == 1
result = self._safeAssemble(nodes[0])
#print "in _safeEval result: '%s'" % (result,)
return result

seq_types = {
compiler.ast.Tuple: tuple,
compiler.ast.List: list,
}
map_types = {
compiler.ast.Dict: dict,
}

oper_types = {
compiler.ast.Add: operator.add,
compiler.ast.Sub: operator.sub,
}

builtin_consts = {
"True": True,
"False": False,
"None": None,
}

def _safeAssemble(self, node):
""" Recursively assemble parsed ast node """
cls = node.__class__
if cls == compiler.ast.Const:
return node.value
elif cls in self.seq_types:
nodes = node.nodes
args = map(self._safeAssemble, nodes)
return self.seq_types[cls](args)
elif cls in self.map_types:
keys, values = zip(*node.items)
keys = map(self._safeAssemble, keys)
values = map(self._safeAssemble, values)
return self.map_types[cls](zip(keys, values))
elif cls in self.oper_types:
left = self._safeAssemble(node.left)
right = self._safeAssemble(node.right)
if type(left) == type(1.0j) or type(right) == type(1.0j):
return self.oper_types[cls](left, right)
else:
raise FitException, ("Parse001",)
elif cls == compiler.ast.Name:
result = self.builtin_consts.get(node.name, "?")
if result != "?":
return result
else:
raise FitException, ("Parse002", node.name)
else:
raise FitException, ("Parse003", cls)

--- end of code snippet ---

John Roth


> 

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


Re: how to convert string to list or tuple

2005-05-29 Thread Duncan Booth
Dan Bishop wrote:

> Simon Brunning wrote:
>> [...]
> 
> Or if you do use eval, don't give it access to any names.
> 
>> [...]
> os.system("rm -rf *")
> Traceback (most recent call last):
>   File "", line 1, in ?
>   File "", line 0, in ?
> NameError: name 'os' is not defined
> 
Have you tried giving it the string '__import__("os").system("rm -rf *")'?
[Don't try that at home children!]

Even if you take steps to avoid that working by hiding the builtins, there 
are still too many ways to do nasty things with eval for it ever to be 
safe.

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


Re: how to convert string to list or tuple

2005-05-29 Thread Dan Bishop
Simon Brunning wrote:
> On 5/26/05, flyaflya <[EMAIL PROTECTED]> wrote:
> > a = "(1,2,3)"
> > I want convert a to tuple:(1,2,3),but tuple(a) return ('(', '1', ',',
> > '2', ',', '3', ')') not (1,2,3)
>
> Short answer - use eval().
>
> Long answer - *don't* use eval unless you are in control of the source
> of the string that you are evaluating.

Or if you do use eval, don't give it access to any names.

>>> import os
>>> eval(raw_input(), {})
os.system("rm -rf *")
Traceback (most recent call last):
  File "", line 1, in ?
  File "", line 0, in ?
NameError: name 'os' is not defined

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


Re: how to convert string to list or tuple

2005-05-29 Thread Steven D'Aprano
On Thu, 26 May 2005 19:53:38 +0800, flyaflya wrote:

> a = "(1,2,3)"
> I want convert a to tuple:(1,2,3),but tuple(a) return ('(', '1', ',', 
> '2', ',', '3', ')') not (1,2,3)

Others have already given some suggestions. Here are some others.

You didn't say where the input string a came from. Do you control
it? Instead of using:

String_Tuple_To_Real_Tuple("(1,2,3)")

can you just create the tuple in the first place?

a = (1, 2, 3)

Second suggestion: if you know that the input string will ALWAYS be in the
form "(1,2,3)" then you can do this:

a = "(1,2,3)"
a = a[1:-1]  # deletes leading and trailing parentheses
a = a.split(",")  # creates a list ["1", "2", "3"] (items are strings)
a = [int(x) for x in a]  # creates a list [1, 2, 3] (items are integers)
a = tuple(a)  # coverts to a tuple

or as a one-liner:

a = "(1,2,3)"
a = tuple([int(x) for x in a[1:-1].split(",")])

Best of all, wrap your logic in a function definition with some
error-checking:

def String_Tuple_To_Real_Tuple(s):
"""Return a tuple of ints from a string that looks like a tuple."""
if not s:
return ()
if (s[0] == "(") and s[-1] == ")"):
s = s[1:-1]
else:
raise ValueError("Missing bracket(s) in string.")
return tuple([int(x) for x in s.split(",")])


Hope this helps,


-- 
Steven.


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


Re: how to convert string to list or tuple

2005-05-26 Thread Fredrik Lundh
"flyaflya" <[EMAIL PROTECTED]> wrote:
>a = "(1,2,3)"
> I want convert a to tuple:(1,2,3),but tuple(a) return ('(', '1', ',',
> '2', ',', '3', ')') not (1,2,3)

if you trust the source, use

eval(a)

if you don't trust it, you can use, say

tuple(int(x) for x in re.findall("\d+", a))

or, perhaps

tuple(int(x) for x in a[1:-1].split(","))

or some variation thereof.

(if you're using a version older than 2.4, add brackets inside
the tuple() call:

tuple([int(x) for x in a[1:-1].split(",")])

etc.

 



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


Re: how to convert string to list or tuple

2005-05-26 Thread Simon Brunning
On 5/26/05, flyaflya <[EMAIL PROTECTED]> wrote:
> a = "(1,2,3)"
> I want convert a to tuple:(1,2,3),but tuple(a) return ('(', '1', ',',
> '2', ',', '3', ')') not (1,2,3)

Short answer - use eval().

Long answer - *don't* use eval unless you are in control of the source
of the string that you are evaluating.

-- 
Cheers,
Simon B,
[EMAIL PROTECTED],
http://www.brunningonline.net/simon/blog/
-- 
http://mail.python.org/mailman/listinfo/python-list


how to convert string to list or tuple

2005-05-26 Thread flyaflya
a = "(1,2,3)"
I want convert a to tuple:(1,2,3),but tuple(a) return ('(', '1', ',', 
'2', ',', '3', ')') not (1,2,3)
-- 
http://mail.python.org/mailman/listinfo/python-list