Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed2.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; '(even': 0.05; 'classes,': 0.05; 'lines,': 0.05; 'main()': 0.07; 'objects,': 0.07; 'subject:code': 0.07; 'python': 0.09; '"if': 0.09; 'counting': 0.09; 'docstrings': 0.09; 'size)': 0.09; 'skip:o 60': 0.09; 'structure,': 0.09; 'subject:number': 0.09; 'gui': 0.11; 'subject:python': 0.11; 'code).': 0.16; 'simplifies': 0.16; 'skip:d 60': 0.16; 'skip:o 70': 0.16; 'subject:program': 0.16; 'top-level': 0.16; 'wrote:': 0.17; 'module,': 0.17; 'projects,': 0.17; 'jan': 0.18; 'code,': 0.18; 'module': 0.19; 'code.': 0.20; 'define': 0.20; 'mostly': 0.20; 'skip:= 10': 0.20; 'to:name :python-list@python.org': 0.20; 'sort': 0.21; 'import': 0.21; 'do.': 0.21; 'not,': 0.21; 'skip:_ 20': 0.22; 'to:2**1': 0.23; 'seems': 0.23; 'idea': 0.24; 'header:In-Reply-To:1': 0.25; 'am,': 0.27; 'possibly': 0.27; 'lines': 0.28; 'actual': 0.28; 'chris': 0.28; 'inspect': 0.29; "people's": 0.29; 'url:mailman': 0.29; "i'm": 0.29; 'function': 0.30; 'code': 0.31; '(and': 0.32; 'url:python': 0.32; '-----': 0.32; 'url:listinfo': 0.32; 'problem': 0.33; 'to:addr:python-list': 0.33; 'skip:d 20': 0.34; 'reply-to:addr:yahoo.com': 0.34; 'process,': 0.35; 'subject:': 0.36; 'but': 0.36; 'url:org': 0.36; 'email addr:python.org': 0.36; 'should': 0.36; 'display': 0.36; 'sent:': 0.37; 'subject:: ': 0.38; 'from:': 0.38; 'object': 0.38; 'skip:o 20': 0.38; 'to:addr:python.org': 0.39; 'takes': 0.39; 'url:mail': 0.40; 'think': 0.40; 'your': 0.60; 'first': 0.61; 'side': 0.61; "you'll": 0.62; 'email name:python-list': 0.62; 'solve': 0.62; 'information': 0.63; 'here': 0.65; 'header:Reply-To:1': 0.68; 'protect': 0.69; '2013': 0.84; 'picture.': 0.84; 'received:98.138.89.166': 0.84 X-Yahoo-Newman-Property: ymail-3 X-Yahoo-Newman-Id: 588973.22051.bm@omp1022.mail.ne1.yahoo.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com; s=s1024; t=1357399067; bh=mGekxcTujSdjBd9lFCMPgkXz+ah5zgllc4DDIx+LpSY=; h=X-YMail-OSG:Received:X-Rocket-MIMEInfo:X-Mailer:References:Message-ID:Date:From:Reply-To:Subject:To:In-Reply-To:MIME-Version:Content-Type:Content-Transfer-Encoding; b=gf00RbAAxMQ/nZGoxsV1vZCovnovg3aitgNzP9aZmTYFs4kVcZjKnsuNMvIWSq4KQxZk0eFzAM4kCPWzu+aaCD8Umc20KrfhoFG742XLWdKya2k+597BouIcWT81cJ0Q7jW531qqZXmHcLDHRRvPnqe1r96dlxV2lbeV2RpR0vc= DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=s1024; d=yahoo.com; h=X-YMail-OSG:Received:X-Rocket-MIMEInfo:X-Mailer:References:Message-ID:Date:From:Reply-To:Subject:To:In-Reply-To:MIME-Version:Content-Type:Content-Transfer-Encoding; b=Ovi94QMp6POu0tlVYjtvuEFEtw2EFEZHoqlJOYa8ICexjWbCOAZeYoxUTfNkNh2iLeZnYfac5gzGW3ePiImA4tmi7jYYslbUMYsPgGwdDHkEPQzRqDFrV0e6Aaw6YXJimbfh+Cf7xZ0G+c/bjg2s/T1kgSagWO11C/kxs8qR1mA=; X-YMail-OSG: 5x5mHmQVM1k7onR9dFVxQRWnOpTcacI8qvmCpArOj7hy4jX J7BA5.BQnqe5wP80QaGooKbSxgszKN4C0tkVS7B.pBQK0w2RHTjPD_HXbTed Guc6B5XbDpCIZuWrQlc3Ob1d45Mnacdas7klpim5CVAWaH8d07C23Jf.hRK6 EGBoDdZI0Pi3R_HvzTLrfdmFb1Zimk7Vmv04tyyysZuVxnm8haAlt5dQ_DcL QbVsjOKScvQYY1C2AnAXqGB.EpTPvAs8nOVLYBJQ_QSQzy8IeP_zXvMxKGCe 6Ox017q9_iLfSRRGWMfL.7m8F6y9r7jGbbAHmZ35kGfRiugH26YiY3ttKADE qrx_.tMcitI2eeaJD.7sp4a1s7o4GK.OSBSkJ2JA4xAt.TCx7ZwCCbkGPGsu M1_R_Tk8L_wN64lGYyespU4rih7myNNnHBMnK4TmZRjFn5t2T2haL8_tejw2 oK0SKEccU5CFUD9ToHjQ9QlkZht1L8PZCKFPB8D2RHmfP_c5WhfUUJnxePiS KHQdtPqwbERkLz98V_A5U3ZhnVcgv X-Rocket-MIMEInfo: 001.001, VGhlIGlkZWEgc3RhcnRlZCBvZmYgYXMgYSB2b2x1bWV0cmljIGluZm9ybWF0aW9uIG9mIG15IHByb2plY3RzLCBidXQgZXZvbHZlZCB0byBhIHNvcnQgb2YgY29kZSBicm93c2VyIHRoYXQgd291bGQgZGlzcGxheSBjbGFzc2VzLCBtZXRob2RzIGFuZCBmdW5jdGlvbnMgaW4gYSB0cmVlLWxpa2Ugc3RydWN0dXJlLCBhbmQgbm93IEkgbW9zdGx5IHdhbnQgdG8gdXNlIGl0IHdpdGggb3RoZXIgcGVvcGxlJ3MgY29kZSBhcyBhIHdheSB0byBoYXZlIHRoZSBiaWcgcGljdHVyZS4gU28gSSB3b3VsZCBzYXkgdGhhdCABMAEBAQE- X-Mailer: YahooMailWebService/0.8.129.483 References: <1357394154.35596.YahooMailNeo@web125506.mail.ne1.yahoo.com> Date: Sat, 5 Jan 2013 07:17:47 -0800 (PST) From: chaouche yacine Subject: Re: Couting the number of lines of code of a python program To: Chris Angelico , "python-list@python.org" In-Reply-To: MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: quoted-printable X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list Reply-To: chaouche yacine 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: 36 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1357399203 news.xs4all.nl 6984 [2001:888:2000:d::a6]:54805 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:36180 The idea started off as a volumetric information of my projects, but evolve= d to a sort of code browser that would display classes, methods and functio= ns in a tree-like structure, and now I mostly want to use it with other peo= ple's code as a way to have the big picture. So I would say that it is used= both for my programs (to get a feeling of their size) as much as it is use= d for others' code.=0A=0A> But counting lines of code is a hairy thing to d= o. Do blank lines,=0A> comments, and multi-line strings count?=0A=0AThe way= I implmented it, they do not, particulary docstrings (even though it seems= that many people think docstrings are actual code). My opinion on docstrin= gs is that they're not instructions. I want to know how many lines of instr= uctions a program has. Comments, blank lines and docstrings are not intruct= ions.=0A=0AHere is my implementation :=0A=0Adefcount_loc(lines):nb_lines = =3D0docstring =3DFalseforline inlines:line =3Dline.strip()ifline =3D=3D""\ = orline.startswith("#")\ ordocstring andnot(line.startswith('"""')orline.sta= rtswith("'''"))\ or(line.startswith("'''")andline.endswith("'''")andlen(lin= e)>3)\ or(line.startswith('"""')andline.endswith('"""')andlen(line)>3):cont= inue# this is either a starting or ending docstringelifline.startswith('"""= ')orline.startswith("'''"):docstring =3Dnotdocstring continueelse:nb_lines = +=3D1returnnb_lines=0A=0A=0A=0A----- Original Message -----=0AFrom: Chris A= ngelico =0ATo: python-list@python.org=0ACc: =0ASent: Satu= rday, January 5, 2013 3:09 PM=0ASubject: Re: Couting the number of lines of= code of a python program=0A=0AOn Sun, Jan 6, 2013 at 12:55 AM, chaouche ya= cine=0A wrote:=0A> The=0A>=A0 problem is that I'm= using the inspect module, because it provides a=0A> nice function inspect.= getsourcelines that takes a python object and=0A> return its number of line= s of code. BUT, it works on live objects, that=0A> means one has to first i= mport the module he wants to process, and this=0A> can have side effects (e= xample : GUI programs).=0A=0AIf you're using this entirely on your own code= , one good way to solve=0Athe problem is to make your code always importabl= e. Protect your=0Atop-level code with "if __name__=3D=3D'__main__':" (and p= ossibly put it=0Ainto a function main() if that simplifies your code counti= ng), and you=0Ashould then be able to import it as-is, and all you'll do is= define ams and =0Abunch of functions/classes.=0A=0ABut counting lines of c= ode is a hairy thing to do. Do blank lines,=0Acomments, and multi-line stri= ngs count?=0A=0AChrisA=0A-- =0Ahttp://mail.python.org/mailman/listinfo/pyth= on-list=0A