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


Groups > comp.lang.python > #41430 > unrolled thread

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

Started byPeng Yu <pengyu.ut@gmail.com>
First post2013-03-18 09:47 -0500
Last post2013-03-18 09:47 -0500
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

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

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

FromPeng Yu <pengyu.ut@gmail.com>
Date2013-03-18 09:47 -0500
SubjectRe: How to automatically get the indent level from code?
Message-ID<mailman.3456.1363618031.2939.python-list@python.org>
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

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web