{"id":22901361,"url":"https://github.com/mooerslab/count-lines-of-code","last_synced_at":"2025-07-14T10:06:56.208Z","repository":{"id":228258241,"uuid":"773486389","full_name":"MooersLab/count-lines-of-code","owner":"MooersLab","description":"Python and bash  functions for counting lines of code","archived":false,"fork":false,"pushed_at":"2024-04-10T11:17:16.000Z","size":24,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-01T05:42:09.481Z","etag":null,"topics":["coding","count-lines-of-code","loc","productivity"],"latest_commit_sha":null,"homepage":"","language":"Shell","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/MooersLab.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2024-03-17T19:39:56.000Z","updated_at":"2024-03-18T11:12:29.000Z","dependencies_parsed_at":"2024-04-10T12:39:38.033Z","dependency_job_id":null,"html_url":"https://github.com/MooersLab/count-lines-of-code","commit_stats":null,"previous_names":["mooerslab/count-lines-of-code"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/MooersLab/count-lines-of-code","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MooersLab%2Fcount-lines-of-code","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MooersLab%2Fcount-lines-of-code/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MooersLab%2Fcount-lines-of-code/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MooersLab%2Fcount-lines-of-code/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/MooersLab","download_url":"https://codeload.github.com/MooersLab/count-lines-of-code/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MooersLab%2Fcount-lines-of-code/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":265277283,"owners_count":23739352,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["coding","count-lines-of-code","loc","productivity"],"created_at":"2024-12-14T01:33:05.741Z","updated_at":"2025-07-14T10:06:56.183Z","avatar_url":"https://github.com/MooersLab.png","language":"Shell","funding_links":[],"categories":[],"sub_categories":[],"readme":"![version](https://img.shields.io/static/v1?label=count-lines-of-code\u0026message=0.2\u0026color=brightcolor)\n[![license: mit](https://img.shields.io/badge/license-mit-blue.svg)](https://opensource.org/licenses/mit)\n\n\n# count-lines-of-code\n\n## Problem addressed\nCounts of lines of code generated are used or abused for many purposes.\nThere is no standard way of making these counts.\n\n## Solution\nUse one-liner Python functions for counting lines of code in an external file of code.\nThe code has no external dependencies.\nThe code can be in any programming language.\nThe examples below use a file written in Elisp.\n\nThe last example is the solution that you want most of the time.\nThe bash function at the bottom can be available everywhere and all of the time.\n\n\n## Count the number of non-blank lines\n\nPython3 one-liner to count number of non-blank lines follows. \nThis code will count the number of non-blank lines:\n\n```python\nprint(sum(1 for line in open(\"init.el\",'r') if line.strip()))\n```\n[Source](https://stackoverflow.com/questions/10673560/count-number-of-lines-in-a-txt-file-with-python-excluding-blank-lines)\n\n## Count the number of commented lines\n\nPython3 one-liner to count the number of lines that are commented out follows.\nThe semicolon is used to comment out lines in Emacs Lisp.\nThis function will miss commented lines that are indented, so this function could be improved.\n\n```python\nprint(sum(1 for line in open('init.el','r') if (line[0] ==  ';') ) )\n```\n\n\n## Count the number of lines with uncommented code\n\nPython3 one-liner to count the number of non-blank lines that are not commented out.\nThis code overlooks block comments and indented  comment lines.\nThere is room for improvement.\n\nThis is the code that you want to use most of the time:\n\n```python\nprint(sum(1 for line in open('init.el','r') if ((line.strip()) and (line[0] != ';') ) ) )\n```\n\n\n##  More convenient bash function for the last example\n\nYou could make this into a bash function that is callable from anywhere on any kind of code.\nBeware that you may have to escape the comment character if it is part of the Bash syntax.\nExamples of usage include:\n\n```bash\ncntloc test.py \\#\ncntloc init.el \\;\ncntloc .bashFunctions \\#\ncntloc rhoxyz.f \\*\ncntloc rhoxyz.f95 \\!\ncntloc ancientCode.f C\ncntloc learnjuia.jl \\#\ncntloc my_model.stan  \\/\ncntloc testme.c \\/\ncntloc test.cjl \\;\ncntloc test.R \\#\n```\n\nThis function can be stored in a `.bashFunctions` file that is sourced when you open a new bash or zsh shell.\n\n```bash\ncntloc()\n{\necho \"Count lines of code in almost any plain text file. Will not handle block comments correctly.\"\necho \"You may have to escape the comment character if it is part of the bash syntax.\"\nif [ $# -lt 2 ]; then\n  echo 1\u003e\u00262 \"$0: not enough arguments\"\n  s='Supply the filename and the first comment character (e.g.,  #  / % ; C * !)'\n  echo \"${s//;\\; //#\\# ///\\/ //%\\%  //!\\!}\"\n  t='Usage: cntloc init.el \\;'\n  echo \"${t//;\\; //#\\# ///\\/ //%\\%  //!\\!}\"\n  return 2\nelif [ $# -gt 2 ]; then\n  echo 1\u003e\u00262 \"$0: too many arguments\"\n  s='Supply the filename and the first comment character (e.g.,  #  / % ; C  * !)'\n  echo \"${s//;\\; //#\\# ///\\/ //%\\%}\"\n  t='Usage: cntloc init.el \\;'\n  echo \"${t//;\\; //#\\# ///\\/ //%\\%}\"\nfi\n/opt/local/bin/python3.11  -c \"print(sum(1 for line in open('$1','r') if ( (line.strip()) and (line[0] != '$2') ) ))\"\n}\n```\n\n##  Limitations\n\n- Skips comments in blocks (e.g., triple quote blocks in  Python).\n- Skips comments on indented lines.\n\n## Limitations\n\n- Skips blocked comments in special comment environments (e.g., Python triple quoted blocks, HTML comment blocks, LaTeX comment blocks, comment blocks in C++).\n- Skip comments on indented lines\n\n\n## Update History\n\n|Version      | Changes                                         | Date            |\n|:-----------:|:-----------------------------------------------:|:---------------:|\n| Version 0.2 |  Fixed typos in README.md                       | 2024 April 10    |\n\n\n## Sources of funding\n\n- NIH: R01 CA242845\n- NIH: R01 AI088011\n- NIH: P30 CA225520 (PI: R. Mannel)\n- NIH P20GM103640 and P30GM145423 (PI: A. West)  \n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmooerslab%2Fcount-lines-of-code","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmooerslab%2Fcount-lines-of-code","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmooerslab%2Fcount-lines-of-code/lists"}