Script that converts between indentation and curly braces in Python code

Rotwang sg552 at hotmail.co.uk
Wed Jul 31 10:07:05 EDT 2013


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?



More information about the Python-list mailing list