Thursday, October 28, 2010

Working with Vim Explore plugin

Vim Explore command let you browse files.

Hiding files and folders: Ctrl + H

Sometimes you would like exclude some files or folders from the list, e.g. pyc files that are results of python compilation, or .svn folders, etc. There is an easy way to get this done. Add the following to your ~/.vimrc file:
let g:netrw_list_hide = '.pyc,.svn,.egg-info'

Back to Explore: Ctrl + 6

When you click on a file in explore view Vim displays its content. Once you finished with your changes to the file there is a way to quickly get back to the explore view. Just press Ctrl+6.

Change current folder: c

When you are in Explore view you can use the following command to create a new file:
:e filename
This command create a file in a folder you started Vim, but sometimes that is not what you want, usually need a new file in a folder you currently browsing. Just press c to make a folder in explore view a current folder for Vim.
Read more about explore here.

Wednesday, October 27, 2010

Vim - Save in Insert Mode

You can use the following combination:
  • Press Ctrl+O
  • Than enter command :w and hit enter
But there is a better way. If you come from Windows there is Ctrl+S combination that saves changes, you can use it in your VIM editor (try issue the following in Vim command mode):
" Use Ctrl+S to save file is edit and command modes
inoremap <c-s> <c-o>:w<cr>
nnoremap <c-s> :w<cr>

Consider add it to your .vimrc file. If you are using Vim in putty console, please have a look here. Read more about mapping keys here.

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.