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


Groups > comp.lang.python > #51663

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

References <09DBD544-FA04-48DC-8FEA-1E788FE1C6A6@gmail.com> <CAAM7w92xAY1z5yVrv6cdL-cAHHFeg8mW+fSAPKsH9u5QHM842w@mail.gmail.com> <mailman.26.1375278953.1251.python-list@python.org> <ktb5a5$pqk$1@dont-email.me>
Date 2013-07-31 15:23 +0100
Subject Re: Script that converts between indentation and curly braces in Python code
From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.30.1375280999.1251.python-list@python.org> (permalink)

Show all headers | View raw


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

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

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

csiph-web