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

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <rosuav@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.005
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'subject:Python': 0.06; 'diff': 0.07; 'subject:code': 0.07; 'ugly': 0.07; 'spaces': 0.09; 'bug': 0.12; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'mixture': 0.16; 'subject: \n ': 0.16; 'subject:between': 0.16; 'tab': 0.16; 'tabs': 0.16; 'wrote:': 0.18; 'wed,': 0.18; 'code,': 0.22; '31,': 0.24; 'header:In-Reply-To:1': 0.27; 'point': 0.28; 'fixed': 0.29; 'message-id:@mail.gmail.com': 0.30; 'work.': 0.31; 'assumes': 0.31; 'indentation': 0.31; 'subject:that': 0.31; 'run': 0.32; "can't": 0.35; 'but': 0.35; 'received:google.com': 0.35; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'sure': 0.39; 'to:addr:python.org': 0.39; 'how': 0.40; 'easy': 0.60; 'pick': 0.64; 'eight': 0.74; 'jul': 0.74; '2013': 0.98
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=ku2G2R8+IMjVyJ9yv9sHSaszWjOhBM0+b72dCFvFdWc=; b=V8AddN7aYx7FjE1vno9M5u110SjLOL1XgqLUrmBgVEgdHEPXgETi6MpNvrlOkr/NkM uhD8IRGMCRvmKUsR/Gb/iSbngwkKLkHppSotVw5OLJIokROVa10CP85Qw2VaN03uEGfj JzDMs+0CD9bxE5HsVTLkGmrWm0MkH9Krjx8g5ypYsUbdUMRhKgzF6p3xkG29FMu48B+G mD9QpF7wGroZdIjDTj+3+ofAjajGO1RWdv1wwGxMc+i/qer8uEMM3haICKbSb5uLxw74 PUD79kLrd3vvAVEvT31+eF7IeZlni9J95ldrizkHieBWJLFqJZIlYPl9CDE+lLJeqx6P YEsw==
MIME-Version 1.0
X-Received by 10.58.187.4 with SMTP id fo4mr28927432vec.55.1375280634490; Wed, 31 Jul 2013 07:23:54 -0700 (PDT)
In-Reply-To <ktb5a5$pqk$1@dont-email.me>
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 Wed, 31 Jul 2013 15:23:54 +0100
Subject Re: Script that converts between indentation and curly braces in Python code
From Chris Angelico <rosuav@gmail.com>
To python-list@python.org
Content-Type text/plain; charset=ISO-8859-1
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.30.1375280999.1251.python-list@python.org> (permalink)
Lines 17
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1375280999 news.xs4all.nl 15921 [2001:888:2000:d::a6]:59550
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:51663

Show key headers only | 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