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


Groups > comp.lang.python > #41430

Re: How to automatically get the indent level from code?

Path csiph.com!usenet.pasdenom.info!news.albasani.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <pengyu.ut@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; '16,': 0.03; '__name__': 0.07; 'column': 0.07; 'computed': 0.07; 'subject:code': 0.07; 'subject:How': 0.09; 'python': 0.09; 'ast': 0.09; 'ioerror:': 0.09; 'subject:skip:a 10': 0.09; 'cc:addr:python-list': 0.10; 'def': 0.10; "'__main__':": 0.16; 'for,': 0.16; 'lineno': 0.16; 'node.lineno': 0.16; 'obviously,': 0.16; 'stdin': 0.16; 'textual': 0.16; 'true:': 0.16; 'wrote:': 0.17; 'skip:{ 20': 0.17; 'import': 0.21; 'thanks.': 0.21; '"",': 0.22; 'logical': 0.22; 'cc:2**0': 0.23; 'work.': 0.23; 'second': 0.24; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'extend': 0.26; '(most': 0.27; 'am,': 0.27; 'realize': 0.27; 'skip:# 10': 0.27; 'tree': 0.27; 'message-id:@mail.gmail.com': 0.27; 'run': 0.28; 'cat': 0.29; 'indentation': 0.29; 'inspect': 0.29; 'node': 0.29; 'function': 0.30; 'code': 0.31; 'asking': 0.32; 'file': 0.32; 'print': 0.32; 'hopefully': 0.33; 'traceback': 0.33; 'problem': 0.33; 'received:google.com': 0.34; 'subject:?': 0.35; 'there': 0.35; 'but': 0.36; 'does': 0.37; 'level': 0.37; 'rather': 0.37; 'subject:: ': 0.38; 'mark': 0.38; 'first': 0.61; 'skip:n 10': 0.63; 'subject:get': 0.81; '2013': 0.84; 'directory:': 0.84; 'start.': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:x-received:in-reply-to:references:date:message-id :subject:from:to:cc:content-type; bh=94HW5tZybDnaS68wpxOohtIMc2iIL40LimlEsVaL8Ow=; b=U9TkXuMKwT7l+YNvzf8tWdIYbkM2jzDiKjVCIMyC96DadVOJCIELAEtYoNynJscXEK I2s5gLl2tBOoFO1cXuUG6FvEvRgRDMezGWFN9Hprs5XgnASIQzhXpHaDwhvPEoHGUIk/ IPqMuj81op7ocypTDBoOQJj26kzF3Hgub6qk749diUaX8Wc5FCLT6eVADG6PU8naOHEe 9Dp+MfaWNhaBRaQNCsYpKmo3ubfiQwT+i0Tj+8aeHmcJH4tcNJm/E9/vc7hYap+iZGes Cz1ObMdMI1xJDyD8+ntFyT+KKahcS72D9hutM/OSA8PaUnStnFVJjggywPXa+YvVXw9c GfPw==
MIME-Version 1.0
X-Received by 10.224.181.210 with SMTP id bz18mr20171327qab.68.1363618023018; Mon, 18 Mar 2013 07:47:03 -0700 (PDT)
In-Reply-To <777767EA52AB47418B1C3D05502A87C622B16C283D@awareexchange1>
References <CABrM6wkCzrMg7_fU9eY15CuNgB3SVzH3+odD06+DtBpO99+Jng@mail.gmail.com> <777767EA52AB47418B1C3D05502A87C622B16C283D@awareexchange1>
Date Mon, 18 Mar 2013 09:47:02 -0500
Subject Re: How to automatically get the indent level from code?
From Peng Yu <pengyu.ut@gmail.com>
To Mark Shroyer <mshroyer@awaredigital.com>
Content-Type text/plain; charset=ISO-8859-1
Cc "python-list@python.org" <python-list@python.org>
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.3456.1363618031.2939.python-list@python.org> (permalink)
Lines 68
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1363618031 news.xs4all.nl 6898 [2001:888:2000:d::a6]:33171
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:41430

Show key headers only | View raw


On Sun, Mar 17, 2013 at 1:23 AM, Mark Shroyer <mshroyer@awaredigital.com> wrote:
> I realize this isn't yet precisely what you're asking for, but look at the inspect and ast modules:
>
>     import ast, inspect
>
>     def indent_level():
>         lineno = inspect.currentframe().f_back.f_lineno
>
>         with open(__file__) as source_file:
>             tree = ast.parse(source_file.read(), filename=__file__)
>
>         for node in ast.walk(tree):
>             if hasattr(node, 'lineno') and node.lineno == lineno:
>                 return node.col_offset
>
>     def example_usage():
>         print("first indent_level() = {0}".format(indent_level()))
>         if True:
>             print("second indent_level() = {0}".format(indent_level()))
>
>     if __name__ == '__main__':
>         example_usage()
>
> The indent_level function above returns the textual column offset rather than the logical block level you're asking for, e.g.:
>
>     first indent_level() = 4
>     second indent_level() = 8
>
> But hopefully it's a start.

Thanks. I try to run it from stdin. Obviously, it does not work. The
problem is the stdin can not be read again. Is there a way to extend
the code that indentation can be computed even the code is from stdin?

~/linux/test/python/tricks/indent_level$ python - < main.py
Traceback (most recent call last):
  File "<stdin>", line 23, in <module>
  File "<stdin>", line 16, in example_usage
  File "<stdin>", line 8, in indent_level
IOError: [Errno 2] No such file or directory: '<stdin>'
~/linux/test/python/tricks/indent_level$ cat main.py
#!/usr/bin/env python

import ast, inspect

def indent_level():
  lineno = inspect.currentframe().f_back.f_lineno

  with open(__file__) as source_file:
    tree = ast.parse(source_file.read(), filename=__file__)

  for node in ast.walk(tree):
    if hasattr(node, 'lineno') and node.lineno == lineno:
      return node.col_offset

def example_usage():
  print indent_level()
  #print("first indent_level() = {0}".format(indent_level()))
  if True:
    print indent_level()
    #print("second indent_level() = {0}".format(indent_level()))

if __name__ == '__main__':
  example_usage()

-- 
Regards,
Peng

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


Thread

Re: How to automatically get the indent level from code? Peng Yu <pengyu.ut@gmail.com> - 2013-03-18 09:47 -0500

csiph-web