Wednesday, October 27, 2010

Recovering from Ctrl+S in Putty

The problem is related to XON/XOFF command that is mapped to Ctrl+S sequence. The terminal doesn't echo the commands you issue, so you need to remember press Ctrl+Q in order to turn flow control ON. There is a way to ignore such behaviour. What you need to do is to change your terminal characteristics.
stty -ixon
Consider add this command to your /etc/profile.d/ixon.sh file.

Thursday, June 24, 2010

Python duplicate code detection with clonedigger

The python tool clonedigger can be used to examine your source code for duplication. It can be installed with easy_install clonedigger. Here is how to run it:
user1@deby:~/devenv/trunk$ ../bin/clonedigger src/
Parsing  src/greatings/helloworld.py ... done
Parsing  src/greatings/__init__.py ... done
Parsing  src/greatings/tests/__init__.py ... done
Parsing  src/greatings/tests/test_helloworld.py ... done
3 sequences
average sequence length: 2.666667
maximum sequence length: 3
Number of statements:  8
Calculating size for each statement... done
Building statement hash... done
Number of different hash values:  5
Building patterns... 6 patterns were discovered
Choosing pattern for each statement... done
Finding similar sequences of statements... 0  sequences were found
Refining candidates... 0 clones were found
Removing dominated clones... 0 clones were removed
Read more about clonedigger here.

Tuesday, June 22, 2010

Python code metrics with pymetrics

The python tool pymetrics can be used to measure your source code complexity. You can install it with easy_install pymetrics. Here is an example (running in virtual environment devenv; source code is located in src):
user1@deby:~/devenv/trunk$ ../bin/pymetrics src/greatings\
/helloworld.py

=== File: src/greatings/helloworld.py ===
Module src/greatings/helloworld.py is missing a module doc string.
Detected at line 1

Basic Metrics for module src/greatings/helloworld.py
----------------------------------------------------
          1    maxBlockDepth
          4    numBlocks
        189    numCharacters
          1    numDocStrings
          1    numFcnDocStrings
          2    numFunctions
          7    numKeywords
         16    numLines
         73    numTokens

         50.00 %FunctionsHavingDocStrings

Functions DocString present(+) or missing(-)
--------------------------------------------
- main
+ say

McCabe Complexity Metric for file src/greatings/helloworld.py
--------------------------------------------------------------
          2    __main__
          1    main
          1    say

COCOMO 2's SLOC Metric for src/greatings/helloworld.py
-------------------------------------------------------
          8    src/greatings/helloworld.py

*** Processed 1 module in run ***
Here is a script that let you generate report for all your files in src directory:
#!/bin/sh

working_dir=/tmp/$USER/pymetrics
mkdir -p $working_dir

find src/ -name \*.py > $working_dir/files.txt
../bin/pymetrics --nosql --nocsv -f $working_dir/files.txt
Here is a guideline for understanding cyclomatic complexity number:
  • 1 - 15: simple code, minimum risk, can be easily covered by tests
  • 15 - 30: complicated code, consider refactoring before writing tests
  • 30 - 50+: complex code, refactor now, almost impossible to write a good tests
Read more about software complexity here and here.

How to count source lines of code in Linux

You can use debian package sloccount for SLOC:
deby:~# apt-get install sloccount
  ...
user1@deby:~/devenv/trunk$ sloccount src/ | grep ^python
python:          31 (100.00%)
Read more about sloccount here.

Monday, June 21, 2010

Python static code analysis with pyflakes

Python tool pyflakes is focused on identifying common errors quickly without executing Python code. It can be installed with easy_install pyflakes. Since the tool doesn't import your code, you just specify a folder with source code:
user1@deby:~/devenv/trunk$ ../bin/pyflakes src/
Using pyflakes with IDE like PyDev make good sense since you see errors while you are typing.