Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #106799 > unrolled thread
| Started by | Fillmore <fillmore_remove@hotmail.com> |
|---|---|
| First post | 2016-04-10 18:51 -0400 |
| Last post | 2016-04-11 17:50 +1000 |
| Articles | 8 on this page of 68 — 20 participants |
Back to article view | Back to comp.lang.python
Most probably a stupid question, but I still want to ask Fillmore <fillmore_remove@hotmail.com> - 2016-04-10 18:51 -0400
Re: Most probably a stupid question, but I still want to ask Chris Angelico <rosuav@gmail.com> - 2016-04-11 08:58 +1000
Re: Most probably a stupid question, but I still want to ask Ben Finney <ben+python@benfinney.id.au> - 2016-04-11 09:04 +1000
Re: Most probably a stupid question, but I still want to ask Stephen Hansen <me+python@ixokai.io> - 2016-04-10 16:30 -0700
Re: Most probably a stupid question, but I still want to ask Fillmore <fillmore_remove@hotmail.com> - 2016-04-10 20:17 -0400
Re: Most probably a stupid question, but I still want to ask Stephen Hansen <me+python@ixokai.io> - 2016-04-10 17:32 -0700
Re: Most probably a stupid question, but I still want to ask Terry Reedy <tjreedy@udel.edu> - 2016-04-10 21:45 -0400
Re: Most probably a stupid question, but I still want to ask Marko Rauhamaa <marko@pacujo.net> - 2016-04-11 08:41 +0300
one-element tuples [Was: Most probably a stupid question, but I still want to ask] Fillmore <fillmore_remove@hotmail.com> - 2016-04-10 20:13 -0400
Re: one-element tuples [Was: Most probably a stupid question, but I still want to ask] Stephen Hansen <me+python@ixokai.io> - 2016-04-10 17:19 -0700
Re: one-element tuples [Was: Most probably a stupid question, but I still want to ask] Stephen Hansen <me+python@ixokai.io> - 2016-04-10 17:18 -0700
Re: one-element tuples [Was: Most probably a stupid question, but I still want to ask] Chris Angelico <rosuav@gmail.com> - 2016-04-11 10:20 +1000
Re: one-element tuples [Was: Most probably a stupid question, but I still want to ask] Fillmore <fillmore_remove@hotmail.com> - 2016-04-10 20:22 -0400
Re: one-element tuples [Was: Most probably a stupid question, but I still want to ask] Stephen Hansen <me+python@ixokai.io> - 2016-04-10 17:28 -0700
Re: one-element tuples Ben Finney <ben+python@benfinney.id.au> - 2016-04-11 10:31 +1000
Re: one-element tuples Fillmore <fillmore_remove@hotmail.com> - 2016-04-10 20:48 -0400
Re: one-element tuples Ben Finney <ben+python@benfinney.id.au> - 2016-04-11 10:56 +1000
Re: one-element tuples Grant Edwards <invalid@invalid.invalid> - 2016-04-11 14:10 +0000
Re: one-element tuples Fillmore <fillmore_remove@hotmail.com> - 2016-04-11 10:11 -0400
Re: one-element tuples Grant Edwards <invalid@invalid.invalid> - 2016-04-11 14:26 +0000
Re: one-element tuples Ned Batchelder <ned@nedbatchelder.com> - 2016-04-10 18:00 -0700
Re: one-element tuples Stephen Hansen <me+python@ixokai.io> - 2016-04-10 18:07 -0700
Re: one-element tuples "Martin A. Brown" <martin@linux-ip.net> - 2016-04-10 18:08 -0700
Re: one-element tuples Fillmore <fillmore_remove@hotmail.com> - 2016-04-10 23:19 -0400
Re: one-element tuples Jussi Piitulainen <jussi.piitulainen@helsinki.fi> - 2016-04-11 09:57 +0300
Re: one-element tuples Larry Hudson <orgnut@yahoo.com> - 2016-04-11 23:01 -0700
Re: one-element tuples Ben Finney <ben+python@benfinney.id.au> - 2016-04-11 11:36 +1000
Re: one-element tuples Fillmore <fillmore_remove@hotmail.com> - 2016-04-10 22:57 -0400
Re: one-element tuples Ben Finney <ben+python@benfinney.id.au> - 2016-04-11 14:10 +1000
Re: one-element tuples Fillmore <fillmore_remove@hotmail.com> - 2016-04-11 00:43 -0400
Re: one-element tuples Stephen Hansen <me+python@ixokai.io> - 2016-04-10 21:54 -0700
Re: one-element tuples Ben Finney <ben+python@benfinney.id.au> - 2016-04-11 15:40 +1000
Re: one-element tuples Rustom Mody <rustompmody@gmail.com> - 2016-04-10 22:07 -0700
Re: one-element tuples BartC <bc@freeuk.com> - 2016-04-11 12:15 +0100
Re: one-element tuples Marko Rauhamaa <marko@pacujo.net> - 2016-04-11 15:12 +0300
Re: one-element tuples Grant Edwards <invalid@invalid.invalid> - 2016-04-11 14:12 +0000
Re: one-element tuples [Was: Most probably a stupid question, but I still want to ask] Ben Finney <ben+python@benfinney.id.au> - 2016-04-11 10:30 +1000
Re: one-element tuples [Was: Most probably a stupid question, but I still want to ask] MRAB <python@mrabarnett.plus.com> - 2016-04-11 01:33 +0100
Re: one-element tuples [Was: Most probably a stupid question, but I still want to ask] Dan Sommers <dan@tombstonezero.net> - 2016-04-11 02:22 +0000
Re: one-element tuples [Was: Most probably a stupid question, but I still want to ask] Chris Angelico <rosuav@gmail.com> - 2016-04-11 12:34 +1000
Re: one-element tuples [Was: Most probably a stupid question, but I still want to ask] Chris Angelico <rosuav@gmail.com> - 2016-04-11 10:38 +1000
Parens do create a tuple (was: one-element tuples [Was: Most probably a stupid question, but I still want to ask]) Ben Finney <ben+python@benfinney.id.au> - 2016-04-11 10:45 +1000
Re: Parens do create a tuple (was: one-element tuples [Was: Most probably a stupid question, but I still want to ask]) Chris Angelico <rosuav@gmail.com> - 2016-04-11 10:50 +1000
Re: Parens do create a tuple Ben Finney <ben+python@benfinney.id.au> - 2016-04-11 10:57 +1000
Re: Parens do create a tuple Chris Angelico <rosuav@gmail.com> - 2016-04-11 11:04 +1000
Re: Parens do create a tuple (was: one-element tuples [Was: Most probably a stupid question, but I still want to ask]) Stephen Hansen <me@ixokai.io> - 2016-04-10 18:03 -0700
Re: Parens do create a tuple (was: one-element tuples [Was: Most probably a stupid question, but I still want to ask]) Tim Chase <python.list@tim.thechases.com> - 2016-04-10 19:52 -0500
Re: Parens do create a tuple Ben Finney <ben+python@benfinney.id.au> - 2016-04-11 11:41 +1000
Re: Parens do create a tuple Steven D'Aprano <steve@pearwood.info> - 2016-04-11 12:32 +1000
Re: Parens do create a tuple Random832 <random832@fastmail.com> - 2016-04-10 22:51 -0400
Re: Parens do create a tuple Steven D'Aprano <steve@pearwood.info> - 2016-04-11 14:08 +1000
Re: Parens do create a tuple Random832 <random832@fastmail.com> - 2016-04-11 01:27 -0400
Re: Parens do create a tuple Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2016-04-11 18:01 +1000
Re: Parens do create a tuple Random832 <random832@fastmail.com> - 2016-04-11 09:42 -0400
Re: Parens do create a tuple Chris Angelico <rosuav@gmail.com> - 2016-04-11 13:02 +1000
Re: Parens do create a tuple Ben Finney <ben+python@benfinney.id.au> - 2016-04-11 14:08 +1000
Re: Parens do create a tuple Chris Angelico <rosuav@gmail.com> - 2016-04-11 11:51 +1000
Re: Parens do create a tuple Steven D'Aprano <steve@pearwood.info> - 2016-04-11 12:57 +1000
Re: one-element tuples [Was: Most probably a stupid question, but I still want to ask] Tim Chase <python.list@tim.thechases.com> - 2016-04-10 19:46 -0500
Re: Most probably a stupid question, but I still want to ask Steven D'Aprano <steve@pearwood.info> - 2016-04-11 11:50 +1000
Re: Most probably a stupid question, but I still want to ask Fillmore <fillmore_remove@hotmail.com> - 2016-04-10 22:48 -0400
Re: Most probably a stupid question, but I still want to ask Steven D'Aprano <steve@pearwood.info> - 2016-04-11 13:54 +1000
Re: Most probably a stupid question, but I still want to ask Fillmore <fillmore_remove@hotmail.com> - 2016-04-11 00:03 -0400
Re: Most probably a stupid question, but I still want to ask Stephen Hansen <me+python@ixokai.io> - 2016-04-10 21:46 -0700
Re: Most probably a stupid question, but I still want to ask Rustom Mody <rustompmody@gmail.com> - 2016-04-10 22:18 -0700
Re: Most probably a stupid question, but I still want to ask Stephen Hansen <me+python@ixokai.io> - 2016-04-10 22:42 -0700
Re: Most probably a stupid question, but I still want to ask Rustom Mody <rustompmody@gmail.com> - 2016-04-10 23:57 -0700
Re: Most probably a stupid question, but I still want to ask Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2016-04-11 17:50 +1000
Page 4 of 4 — ← Prev page 1 2 3 [4]
| From | Fillmore <fillmore_remove@hotmail.com> |
|---|---|
| Date | 2016-04-10 22:48 -0400 |
| Message-ID | <nef3a3$15nk$2@gioia.aioe.org> |
| In reply to | #106832 |
funny, but it seems to me that you are taking it personally... thank god i even apologized in advance for what was most probably a stupid question.. On 04/10/2016 09:50 PM, Steven D'Aprano wrote: > > Fillmore, you should feel very pleased with yourself. All the tens of > thousands of Python programmers, and millions of lines of code written in > the language, but nobody until now was able to see what you alone had the > intelligence and clarity of thought to spot. Well done! > > >
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve@pearwood.info> |
|---|---|
| Date | 2016-04-11 13:54 +1000 |
| Message-ID | <570b2000$0$22142$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #106838 |
On Mon, 11 Apr 2016 12:48 pm, Fillmore wrote: > > funny, but it seems to me that you are taking it personally... thank god i > even apologized in advance for what was most probably a stupid question.. I hope you did get a laugh out of it, because it wasn't meant to be nasty. But it was meant to get you to think about statements about betraying principles and other inflammatory remarks. -- Steven
[toc] | [prev] | [next] | [standalone]
| From | Fillmore <fillmore_remove@hotmail.com> |
|---|---|
| Date | 2016-04-11 00:03 -0400 |
| Message-ID | <nef7lq$1ati$1@gioia.aioe.org> |
| In reply to | #106847 |
On 04/10/2016 11:54 PM, Steven D'Aprano wrote: > On Mon, 11 Apr 2016 12:48 pm, Fillmore wrote: > >> >> funny, but it seems to me that you are taking it personally... thank god i >> even apologized in advance for what was most probably a stupid question.. > > I hope you did get a laugh out of it, because it wasn't meant to be nasty. > But it was meant to get you to think about statements about betraying > principles and other inflammatory remarks. I did have a laugh. I don't think I talked about betraying principles. I just mentioned that in my newbie mind, I experienced what I perceived as a discontinuity. My limited understanding of what builds tuples and the (almost always to be avoided) use of eval() were the origin of my perplexities. I'll make sure I approach the temple of pythonistas bare-footed and with greater humility next time
[toc] | [prev] | [next] | [standalone]
| From | Stephen Hansen <me+python@ixokai.io> |
|---|---|
| Date | 2016-04-10 21:46 -0700 |
| Message-ID | <mailman.18.1460350021.15650.python-list@python.org> |
| In reply to | #106848 |
On Sun, Apr 10, 2016, at 09:03 PM, Fillmore wrote: > and the (almost always to be avoided) use of eval() FWIW, there's ast.literal_eval which is safe and there's no reason to avoid it. You'll still have to deal with the fact that a single string on a line will return a string while multiples will return a tuple, but how you're doing that (checking the type of the result) is fine. --Stephen m e @ i x o k a i . i o
[toc] | [prev] | [next] | [standalone]
| From | Rustom Mody <rustompmody@gmail.com> |
|---|---|
| Date | 2016-04-10 22:18 -0700 |
| Message-ID | <45c1ee67-16c5-45b3-bf3c-f33efb42d3c3@googlegroups.com> |
| In reply to | #106853 |
On Monday, April 11, 2016 at 10:17:13 AM UTC+5:30, Stephen Hansen wrote:
> On Sun, Apr 10, 2016, at 09:03 PM, Fillmore wrote:
> > and the (almost always to be avoided) use of eval()
>
> FWIW, there's ast.literal_eval which is safe and there's no reason to
> avoid it.
Its error reporting is clunky:
>>> from ast import literal_eval as le
>>> le("(1)")
1
>>> le("(1,)")
(1,)
>>> le("1")
1
>>> le("{1:x}")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/ast.py", line 80, in literal_eval
return _convert(node_or_string)
File "/usr/lib/python2.7/ast.py", line 63, in _convert
in zip(node.keys, node.values))
File "/usr/lib/python2.7/ast.py", line 62, in <genexpr>
return dict((_convert(k), _convert(v)) for k, v
File "/usr/lib/python2.7/ast.py", line 79, in _convert
raise ValueError('malformed string')
ValueError: malformed string
> You'll still have to deal with the fact that a single string
> on a line will return a string while multiples will return a tuple, but
> how you're doing that (checking the type of the result) is fine.
Yes...
0 and 1 length tuples are special cases even if we argue till we are blue in
the face
To Fillmore:
Go via lists and the special cases can be obviated:
>>> def br(s): return "[" + s + "]"
>>> s0=""
>>> s1="1"
>>> s2="1,2"
>>> le(br(s0))
[]
>>> le(br(s1))
[1]
>>> le(br(s2))
[1,2]
Then tuplify (if you so wish)
>>> tuple(le(br(s0)))
()
>>> tuple(le(br(s1)))
(1,)
>>> tuple(le(br(s2)))
(1, 2)
[toc] | [prev] | [next] | [standalone]
| From | Stephen Hansen <me+python@ixokai.io> |
|---|---|
| Date | 2016-04-10 22:42 -0700 |
| Message-ID | <mailman.23.1460353347.15650.python-list@python.org> |
| In reply to | #106857 |
On Sun, Apr 10, 2016, at 10:18 PM, Rustom Mody wrote:
> On Monday, April 11, 2016 at 10:17:13 AM UTC+5:30, Stephen Hansen wrote:
> > On Sun, Apr 10, 2016, at 09:03 PM, Fillmore wrote:
> > > and the (almost always to be avoided) use of eval()
> >
> > FWIW, there's ast.literal_eval which is safe and there's no reason to
> > avoid it.
>
> Its error reporting is clunky:
>
> >>> from ast import literal_eval as le
> >>> le("(1)")
> 1
> >>> le("(1,)")
> (1,)
> >>> le("1")
> 1
> >>> le("{1:x}")
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> File "/usr/lib/python2.7/ast.py", line 80, in literal_eval
> return _convert(node_or_string)
> File "/usr/lib/python2.7/ast.py", line 63, in _convert
> in zip(node.keys, node.values))
> File "/usr/lib/python2.7/ast.py", line 62, in <genexpr>
> return dict((_convert(k), _convert(v)) for k, v
> File "/usr/lib/python2.7/ast.py", line 79, in _convert
> raise ValueError('malformed string')
> ValueError: malformed string
So? Worst case scenario, someone puts invalid data into the file and it
throws an exception. Could it be more specific? Maybe, but I don't see
why it needs to be. If your input isn't reliably formatted, catch
ValueError and log it and fix (either what wrote it or change how you
read it).
--S
[toc] | [prev] | [next] | [standalone]
| From | Rustom Mody <rustompmody@gmail.com> |
|---|---|
| Date | 2016-04-10 23:57 -0700 |
| Message-ID | <ced2b390-f14d-408c-93d3-9d92b75b048e@googlegroups.com> |
| In reply to | #106861 |
On Monday, April 11, 2016 at 11:12:39 AM UTC+5:30, Stephen Hansen wrote:
> On Sun, Apr 10, 2016, at 10:18 PM, Rustom Mody wrote:
> > On Monday, April 11, 2016 at 10:17:13 AM UTC+5:30, Stephen Hansen wrote:
> > > On Sun, Apr 10, 2016, at 09:03 PM, Fillmore wrote:
> > > > and the (almost always to be avoided) use of eval()
> > >
> > > FWIW, there's ast.literal_eval which is safe and there's no reason to
> > > avoid it.
> >
> > Its error reporting is clunky:
> >
> > >>> from ast import literal_eval as le
> > >>> le("(1)")
> > 1
> > >>> le("(1,)")
> > (1,)
> > >>> le("1")
> > 1
> > >>> le("{1:x}")
> > Traceback (most recent call last):
> > File "<stdin>", line 1, in <module>
> > File "/usr/lib/python2.7/ast.py", line 80, in literal_eval
> > return _convert(node_or_string)
> > File "/usr/lib/python2.7/ast.py", line 63, in _convert
> > in zip(node.keys, node.values))
> > File "/usr/lib/python2.7/ast.py", line 62, in <genexpr>
> > return dict((_convert(k), _convert(v)) for k, v
> > File "/usr/lib/python2.7/ast.py", line 79, in _convert
> > raise ValueError('malformed string')
> > ValueError: malformed string
>
> So? Worst case scenario, someone puts invalid data into the file and it
> throws an exception. Could it be more specific? Maybe, but I don't see
> why it needs to be. If your input isn't reliably formatted, catch
> ValueError and log it and fix (either what wrote it or change how you
> read it).
Sorry... Didnt mean "dont use!"
Just "Heres a flipside to consider"
Also that the OP's question is not a classic "Avoid eval like the plague"
but "Many nooks and crannies in the syntax" issue
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2016-04-11 17:50 +1000 |
| Message-ID | <570b5751$0$1589$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #106848 |
On Monday 11 April 2016 14:03, Fillmore wrote: > I'll make sure I approach the temple of pythonistas bare-footed and with > greater humility next time Don't forget to rip your clothes into rags and heap ashes on your head too. -- Steve
[toc] | [prev] | [standalone]
Page 4 of 4 — ← Prev page 1 2 3 [4]
Back to top | Article view | comp.lang.python
csiph-web