Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #51657 > unrolled thread

Re: Script that converts between indentation and curly braces in Python code

Started byChris Angelico <rosuav@gmail.com>
First post2013-07-31 14:55 +0100
Last post2013-07-31 15:23 +0100
Articles 3 — 2 participants

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Script that converts between indentation and curly braces in Python code Chris Angelico <rosuav@gmail.com> - 2013-07-31 14:55 +0100
    Re: Script that converts between indentation and curly braces in Python code Rotwang <sg552@hotmail.co.uk> - 2013-07-31 15:07 +0100
      Re: Script that converts between indentation and curly braces in Python code Chris Angelico <rosuav@gmail.com> - 2013-07-31 15:23 +0100

#51657 — Re: Script that converts between indentation and curly braces in Python code

FromChris Angelico <rosuav@gmail.com>
Date2013-07-31 14:55 +0100
SubjectRe: Script that converts between indentation and curly braces in Python code
Message-ID<mailman.26.1375278953.1251.python-list@python.org>
On Wed, Jul 31, 2013 at 1:39 PM, Beth McNany <beth.mcnany@gmail.com> wrote:
> ok, ok, if you *really* want it, you could keep track of how many leading
> spaces there are (you are using spaces, right?), and insert an open bracket
> where that number increases and a closing bracket where it decreases.  Of
> course, as with all parsing problems, this is oversimplification... if you
> have multi-line statements you'll need to check for those (if line starts
> with """ or ''', ends with \, or if there's an unclosed bracket or paren...)
> - but that'd be a reasonable place to start if you're only doing short code
> snippets.
>

Since the braced version won't run anyway, how about a translation like this:

def foo():
    print("""Hello,
world!""")
    for i in range(5):
        foo()
    return 42

-->

0-def foo():
4-print("""Hello,
0-world!""")
4-for i in range(5):
8-foo()
4-return 42

That's a simple translation that guarantees safe round-tripping, and
you can probably do it with a one-liner fwiw... let's see...

# Assumes spaces OR tabs but not both
# Can't see an easy way to count leading spaces other than:
# len(s)-len(s.lstrip())
code = '\n'.join("%d-%s"%(len(s)-len(s.lstrip()),s.lstrip()) for s in
code.split('\n'))

# Recreates with spaces, choose tabs for the multiplication if you prefer
code = '\n'.join(' '*int(s.split('-',1)[0])+s.split('-',1)[1] for s in
code.split('\n'))

These would be better done in a couple of lines, but I like doing
one-liners just for fun. :)

ChrisA

[toc] | [next] | [standalone]


#51658

FromRotwang <sg552@hotmail.co.uk>
Date2013-07-31 15:07 +0100
Message-ID<ktb5a5$pqk$1@dont-email.me>
In reply to#51657
On 31/07/2013 14:55, Chris Angelico wrote:
> [...]
>
>
> Since the braced version won't run anyway, how about a translation like this:
>
> def foo():
>      print("""Hello,
> world!""")
>      for i in range(5):
>          foo()
>      return 42
>
> -->
>
> 0-def foo():
> 4-print("""Hello,
> 0-world!""")
> 4-for i in range(5):
> 8-foo()
> 4-return 42
>
> That's a simple translation that guarantees safe round-tripping, and
> you can probably do it with a one-liner fwiw... let's see...
>
> # Assumes spaces OR tabs but not both
> # Can't see an easy way to count leading spaces other than:
> # len(s)-len(s.lstrip())

How about len(s.expandtabs()) - len(s.lstrip()) instead?

[toc] | [prev] | [next] | [standalone]


#51663

FromChris Angelico <rosuav@gmail.com>
Date2013-07-31 15:23 +0100
Message-ID<mailman.30.1375280999.1251.python-list@python.org>
In reply to#51658
On Wed, Jul 31, 2013 at 3:07 PM, Rotwang <sg552@hotmail.co.uk> wrote:
>> # Assumes spaces OR tabs but not both
>> # Can't see an easy way to count leading spaces other than:
>> # len(s)-len(s.lstrip())
>
>
> How about len(s.expandtabs()) - len(s.lstrip()) instead?

Still comes to the same thing. The only diff is that tabs get treated
as eight spaces instead of one (and the bug that a tab elsewhere in
the line will result in indentation, which is fixed by lstripping the
tab-expanded form). It won't perfectly round-trip with a mixture of
tabs and spaces; as it is, you can pick one or the other and run with
it. Anyway, the main point is that indentation will work. Sure you
might have ugly narrow code, but it'll run with one-space indents.

ChrisA

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web