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: 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 To: Peng Yu , "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: In-Reply-To: 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 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 =3D inspect.currentframe().f_back.f_lineno with open(__file__) as source_file: tree =3D ast.parse(source_file.read(), filename=3D__file__) for node in ast.walk(tree): if hasattr(node, 'lineno') and node.lineno =3D=3D lineno: return node.col_offset def example_usage(): print("first indent_level() =3D {0}".format(indent_level())) if True: print("second indent_level() =3D {0}".format(indent_level())) if __name__ =3D=3D '__main__': example_usage() The indent_level function above returns the textual column offset rather th= an the logical block level you're asking for, e.g.: first indent_level() =3D 4 second indent_level() =3D 8 But hopefully it's a start. Mark -----Original Message----- From: Python-list [mailto:python-list-bounces+mshroyer=3Dawaredigital.com@p= ython.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. --=20 Regards, Peng --=20 http://mail.python.org/mailman/listinfo/python-list