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


Groups > comp.lang.python > #41349

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 <mshroyer@awaredigital.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; '__name__': 0.07; 'column': 0.07; 'subject:code': 0.07; 'subject:How': 0.09; '#print': 0.09; 'ast': 0.09; 'subject:skip:a 10': 0.09; 'def': 0.10; "'__main__':": 0.16; 'code?': 0.16; 'for,': 0.16; 'indent': 0.16; 'lineno': 0.16; 'node.lineno': 0.16; 'received:10.3.3': 0.16; 'skip:[ 60': 0.16; 'textual': 0.16; 'true:': 0.16; 'message-----': 0.17; 'pfxlen:0': 0.17; 'skip:{ 20': 0.17; 'code.': 0.20; 'python?': 0.20; 'to:name:python-list@python.org': 0.20; 'import': 0.21; 'logical': 0.22; 'received:10.3': 0.22; 'to:2**1': 0.23; 'second': 0.24; 'header:In-Reply-To:1': 0.25; 'realize': 0.27; 'tree': 0.27; 'inspect': 0.29; 'node': 0.29; 'url:mailman': 0.29; 'function': 0.30; 'asking': 0.32; 'url:python': 0.32; 'skip:- 10': 0.32; 'print': 0.32; 'url:listinfo': 0.32; 'hopefully': 0.33; 'to:addr:python-list': 0.33; 'hi,': 0.33; 'subject:?': 0.35; 'subject:': 0.36; 'but': 0.36; 'url:org': 0.36; 'level.': 0.36; 'email addr:python.org': 0.36; 'charset:us-ascii': 0.36; 'possible': 0.37; 'level': 0.37; 'sent:': 0.37; 'rather': 0.37; 'subject:: ': 0.38; 'mark': 0.38; 'from:': 0.38; 'received:10': 0.38; 'to:addr:python.org': 0.39; 'url:mail': 0.40; 'first': 0.61; 'email name:python-list': 0.62; 'skip:n 10': 0.63; 'within': 0.64; 'subject:get': 0.81; '2013': 0.84; 'start.': 0.84
From Mark Shroyer <mshroyer@awaredigital.com>
To Peng Yu <pengyu.ut@gmail.com>, "python-list@python.org" <python-list@python.org>
Date Sun, 17 Mar 2013 02:23:31 -0400
Subject RE: How to automatically get the indent level from code?
Thread-Topic How to automatically get the indent level from code?
Thread-Index Ac4izAopL5HSNyrZSneCzrmTtE1pjwACopKA
References <CABrM6wkCzrMg7_fU9eY15CuNgB3SVzH3+odD06+DtBpO99+Jng@mail.gmail.com>
In-Reply-To <CABrM6wkCzrMg7_fU9eY15CuNgB3SVzH3+odD06+DtBpO99+Jng@mail.gmail.com>
Accept-Language en-US
Content-Language en-US
X-MS-Has-Attach
X-MS-TNEF-Correlator
acceptlanguage en-US
Content-Type text/plain; charset="us-ascii"
Content-Transfer-Encoding quoted-printable
MIME-Version 1.0
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.3384.1363501486.2939.python-list@python.org> (permalink)
Lines 54
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1363501486 news.xs4all.nl 6958 [2001:888:2000:d::a6]:48047
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:41349

Show key headers only | View raw


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.

Mark

-----Original Message-----
From: Python-list [mailto:python-list-bounces+mshroyer=awaredigital.com@python.org] On Behalf Of Peng Yu
Sent: Sunday, March 17, 2013 12:53 AM
To: python-list@python.org
Subject: How to automatically get the indent level from code?

Hi,

I want to get the indent level within the code. For example, I want to
print 1 within the while loop as the line is indented 1 level. Is it
possible to get it within python?

while 1:
   #print the level of indent, which is 1 here.

-- 
Regards,
Peng
-- 
http://mail.python.org/mailman/listinfo/python-list

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


Thread

RE: How to automatically get the indent level from code? Mark Shroyer <mshroyer@awaredigital.com> - 2013-03-17 02:23 -0400

csiph-web