Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder1.xlned.com!newsfeed.xs4all.nl!newsfeed4.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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'lines,': 0.05; 'main()': 0.07; 'objects,': 0.07; 'subject:code': 0.07; 'python': 0.09; '"if': 0.09; 'counting': 0.09; 'received:mail-vb0-f46.google.com': 0.09; 'subject:number': 0.09; 'gui': 0.11; 'subject:python': 0.11; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'simplifies': 0.16; 'subject:program': 0.16; 'top-level': 0.16; 'wrote:': 0.17; 'module,': 0.17; 'jan': 0.18; 'code,': 0.18; 'received:209.85.212.46': 0.18; 'module': 0.19; 'code.': 0.20; 'define': 0.20; 'import': 0.21; 'do.': 0.21; 'skip:_ 20': 0.22; 'header:In-Reply-To:1': 0.25; 'am,': 0.27; 'possibly': 0.27; 'message-id:@mail.gmail.com': 0.27; 'lines': 0.28; 'received:209.85.212': 0.28; 'inspect': 0.29; "i'm": 0.29; 'function': 0.30; 'code': 0.31; '(and': 0.32; 'problem': 0.33; 'to:addr:python-list': 0.33; 'received:google.com': 0.34; 'process,': 0.35; 'received:209.85': 0.35; 'but': 0.36; 'should': 0.36; 'received:209': 0.37; 'subject:: ': 0.38; 'object': 0.38; 'to:addr:python.org': 0.39; 'takes': 0.39; 'header:Received:5': 0.40; 'your': 0.60; 'first': 0.61; 'side': 0.61; "you'll": 0.62; 'solve': 0.62; 'protect': 0.69; '2013': 0.84 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=GSp5G1G3yJodLTQj/l68AswhgqN2yag7nWIm58dY7D0=; b=iMgq41Dv2rZdvY9j2HqxHnEAUkGbb3qVV7DdBemL0anvrCT/RPNBNkG2lmG52emx1e KeCYnnBZZsj/qwYxLgQV3RPg8awzqvQSL5L0yQF7aFZ0+h7aQim+LmUkdVRa4HhB0aQs LqRWtQmSNSSw/yp9axm41au1Vu5h2AMD5YHBVmGDx8kVlXvEbT3qikwBNY4hr1D4FoD2 YeX02pDLmT7yn8wXRVRwcbCrxD/zkCYBE44DWr5a9GTjCvaWHV9kETtiL+zmaxGXl1dZ gZvw7PMNzOh/JgwbjrgxQXRhuxWO/U640PgrMfrykziUGM5SuEOjW2wPR5QQ0+h5NUuY KvhA== MIME-Version: 1.0 In-Reply-To: <1357394154.35596.YahooMailNeo@web125506.mail.ne1.yahoo.com> References: <1357394154.35596.YahooMailNeo@web125506.mail.ne1.yahoo.com> Date: Sun, 6 Jan 2013 01:09:08 +1100 Subject: Re: Couting the number of lines of code of a python program 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: 20 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1357394951 news.xs4all.nl 6895 [2001:888:2000:d::a6]:58868 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:36172 On Sun, Jan 6, 2013 at 12:55 AM, chaouche yacine wrote: > The > problem is that I'm using the inspect module, because it provides a > nice function inspect.getsourcelines that takes a python object and > return its number of lines of code. BUT, it works on live objects, that > means one has to first import the module he wants to process, and this > can have side effects (example : GUI programs). If you're using this entirely on your own code, one good way to solve the problem is to make your code always importable. Protect your top-level code with "if __name__=='__main__':" (and possibly put it into a function main() if that simplifies your code counting), and you should then be able to import it as-is, and all you'll do is define a bunch of functions/classes. But counting lines of code is a hairy thing to do. Do blank lines, comments, and multi-line strings count? ChrisA