Giter Club home page Giter Club logo

count-lines-of-code's Introduction

version license: mit

count-lines-of-code

Problem addressed

Counts of lines of code generated are used or abused for many purposes. There is no standard way of making these counts.

Solution

Use one-liner Python functions for counting lines of code in an external file of code. The code has no external dependencies. The code can be in any programming language. The examples below use a file written in Elisp.

The last example is the solution that you want most of the time. The bash function at the bottom can be available everywhere and all of the time.

Count the number of non-blank lines

Python3 one-liner to count number of non-blank lines follows. This code will count the number of non-blank lines:

print(sum(1 for line in open("init.el",'r') if line.strip()))

Source

Count the number of commented lines

Python3 one-liner to count the number of lines that are commented out follows. The semicolon is used to comment out lines in Emacs Lisp. This function will miss commented lines that are indented, so this function could be improved.

print(sum(1 for line in open('init.el','r') if (line[0] ==  ';') ) )

Count the number of lines with uncommented code

Python3 one-liner to count the number of non-blank lines that are not commented out. This code overlooks block comments and indented comment lines. There is room for improvement.

This is the code that you want to use most of the time:

print(sum(1 for line in open('init.el','r') if ((line.strip()) and (line[0] != ';') ) ) )

More convenient bash function for the last example

You could make this into a bash function that is callable from anywhere on any kind of code. Beware that you may have to escape the comment character if it is part of the Bash syntax. Examples of usage include:

cntloc test.py \#
cntloc init.el \;
cntloc .bashFunctions \#
cntloc rhoxyz.f \*
cntloc rhoxyz.f95 \!
cntloc ancientCode.f C
cntloc learnjuia.jl \#
cntloc my_model.stan  \/
cntloc testme.c \/
cntloc test.cjl \;
cntloc test.R \#

This function can be stored in a .bashFunctions file that is sourced when you open a new bash or zsh shell.

cntloc()
{
echo "Count lines of code in almost any plain text file. Will not handle block comments correctly."
echo "You may have to escape the comment character if it is part of the bash syntax."
if [ $# -lt 2 ]; then
  echo 1>&2 "$0: not enough arguments"
  s='Supply the filename and the first comment character (e.g.,  #  / % ; C * !)'
  echo "${s//;\; //#\# ///\/ //%\%  //!\!}"
  t='Usage: cntloc init.el \;'
  echo "${t//;\; //#\# ///\/ //%\%  //!\!}"
  return 2
elif [ $# -gt 2 ]; then
  echo 1>&2 "$0: too many arguments"
  s='Supply the filename and the first comment character (e.g.,  #  / % ; C  * !)'
  echo "${s//;\; //#\# ///\/ //%\%}"
  t='Usage: cntloc init.el \;'
  echo "${t//;\; //#\# ///\/ //%\%}"
fi
/opt/local/bin/python3.11  -c "print(sum(1 for line in open('$1','r') if ( (line.strip()) and (line[0] != '$2') ) ))"
}

Limitations

  • Skips comments in blocks (e.g., triple quote blocks in Python).
  • Skips comments on indented lines.

Limitations

  • Skips blocked comments in special comment environments (e.g., Python triple quoted blocks, HTML comment blocks, LaTeX comment blocks, comment blocks in C++).
  • Skip comments on indented lines

Update History

Version Changes Date
Version 0.2 Fixed typos in README.md 2024 April 10

Sources of funding

  • NIH: R01 CA242845
  • NIH: R01 AI088011
  • NIH: P30 CA225520 (PI: R. Mannel)
  • NIH P20GM103640 and P30GM145423 (PI: A. West)

count-lines-of-code's People

Contributors

mooerslab avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.