Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!eweka.nl!lightspeed.eweka.nl!194.109.133.87.MISMATCH!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.003 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'insert': 0.05; 'subject:Python': 0.06; '"""': 0.07; 'lines,': 0.07; 'subject:code': 0.07; 'parsing': 0.09; 'spaces': 0.09; 'statements': 0.09; 'def': 0.12; 'translation': 0.12; 'bracket': 0.16; 'foo()': 0.16; 'foo():': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'subject: \n ': 0.16; 'subject:between': 0.16; 'tabs': 0.16; '(you': 0.16; 'wrote:': 0.18; 'wed,': 0.18; 'starts': 0.20; '31,': 0.24; 'this:': 0.26; 'header:In-Reply-To:1': 0.27; 'message-id:@mail.gmail.com': 0.30; 'code': 0.31; 'assumes': 0.31; "skip:' 40": 0.31; 'subject:that': 0.31; 'probably': 0.32; 'run': 0.32; 'open': 0.33; 'could': 0.34; "can't": 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'version': 0.36; 'done': 0.36; 'doing': 0.36; 'skip:4 10': 0.37; 'skip:o 20': 0.38; 'ends': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'track': 0.38; 'short': 0.38; 'to:addr:python.org': 0.39; 'how': 0.40; 'easy': 0.60; 'simple': 0.61; "you're": 0.61; "you'll": 0.62; 'choose': 0.64; 'safe': 0.72; 'jul': 0.74; '*really*': 0.84; 'increases': 0.91; '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=ne+1hMU0ObqGS2mzCVUWv9/G32oK/2pQ7VCmSjT1PBY=; b=h7lF5upAE/94ExGHkgmTQIhhO3YqCMGQQv3++IrCcjcdeDyhW4FCDxsvYF1uc/OKq5 HavgAY28a4nxzCodtRuXRFqDBRLugcbz0cibdNfWyj+oB3nURD4Q35kyuX+VIosxnTNu 6FIKl+kDbOwMz9OnofvnWAkU1SxnIur9hyHUXR591VFRlgSYsT747cxf9U655nVd/CDY 7nzr1iQWJMGtjyk2jYdziJnBpOV5baxLKRZMss/G7/mPwGRXxpa5rgnKNBx7TdR903Si Hu8miURsmuvc7VUk0jvwXsQae1lIzABSdsUvCIcv9y4ABlJtUsqTJHYYYumOShztY0kL VsFA== MIME-Version: 1.0 X-Received: by 10.221.68.71 with SMTP id xx7mr1107321vcb.97.1375278951666; Wed, 31 Jul 2013 06:55:51 -0700 (PDT) In-Reply-To: References: <09DBD544-FA04-48DC-8FEA-1E788FE1C6A6@gmail.com> Date: Wed, 31 Jul 2013 14:55:51 +0100 Subject: Re: Script that converts between indentation and curly braces in Python code From: Chris Angelico 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 46 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1375278953 news.xs4all.nl 15890 [2001:888:2000:d::a6]:44494 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:51657 On Wed, Jul 31, 2013 at 1:39 PM, Beth McNany 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