How to use "while" within the command in -c option of python?

Joshua Landau joshua.landau.ws at gmail.com
Sat Oct 13 18:00:09 EDT 2012


On 13 October 2012 22:39, Chris Angelico <rosuav at gmail.com> wrote:

> On Sun, Oct 14, 2012 at 6:06 AM, Joshua Landau
> <joshua.landau.ws at gmail.com> wrote:
> > The fact that your proposal can't allow "a=[]\nfor x in range(10):
> > a.append(x**a[-2])\nprint(a)" makes it somewhat an incomplete suggestion,
> > and code like:
> >
> >> while True: while True: break; break
> >
> > is just confusing.
>
> Agreed. However, I don't think there's going to be _any_ perfect
> solution for delimiting blocks, short of "from __future__ import
> braces".


I don't know. I don't personally think of it as much of a problem, as the
only time this will ever really cause trouble is:
a) You can't enter newlines easily
b) It's a hassle to keep track of indentation

These only really apply to a few things.


>  > I don't want to sound closed, but the options I'm really open to are:
> >
> > a) It does a limited set of things that make a those things nicer, á la
> "@"
> > b) It does almost everything, minus some carefully-chosen things deemed
> to
> > quirky, á la current newlines (which don't allow "if a: if b: pass")
> > c) It does everything that would be possible
> >
> > Your example falls nicely between a and b, which I do not find
> particularly
> > helpful. Mine attempts a by only applying to "python -c", but would be c
> if
> > it didn't. I find the syntax to horrible for general code, which is why I
> > didn't suggest that.
> >
>
> Your idea is an extension to the -c parameter that would technically
> work, but be fairly hard to manage in anything other than the trivial
> case. My idea is an extension to general syntax that would work in all
> cases where you aren't trying to put a statement after the end of what
> would be an indented block. Both have their limitations, but both
> would be - as far as I can see - backward compatible.


Since you seem to want to solve the general case (I was under the
assumption that we were still only talking in the context of the OP) I have
an idea.

There could be a Python module that reads a file like this:

> if a: ${ print(1) $ print(2) $ while b: c() $ if g: ${ pass }$ }$ print(d)

and transforms it to the valid Python:
>
> if a:
>     print(1)
>     print(2)
>     while b: c()
>     if g:
>         pass
> print(d)


That is *also* callable from the command-line like so:

> python -m debrace -c "if a: ${ print(1) $ print(2) $ while b: c() $ if g:
> ${ pass }$ }$ print(d)"


This should solve both *a* and *b* above, and it would make life easier for
scripters. It's also easy to mock up, and from then we only need it in the
stdlib.

~~~~~~~~~~~~~~~~~~~~~~~
"${" means INDENT
"}$" means DEDENT
"$" means NEWLINE (similar to ";")
*"$" has a lower precedence than "${" or "}$"*
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20121013/bf924609/attachment.html>


More information about the Python-list mailing list