Sunday, November 15, 2015

Number of lines of code

I was asked to find out the number of lines of code in our entire module so as to figure out how hard is to maintain it. Here is the source to find out the number of lines in a python project

#!\usr\bin\env python
import os
print 'current working directory : ' + os.getcwd()
total_lines = 0
def number_of_lines_in_file( filename ):
global total_lines
if filename.endswith(".py"):
with open(os.path.realpath(filename), 'r') as file_handle:
lines = sum(1 for _ in file_handle)
print 'no of lines in ' + filename + ' is %d' %lines
total_lines = total_lines + lines
def visit_the_directory( folder ):
for element in os.listdir(folder):
if os.path.isdir(os.path.join(folder,element)):
print element + ' is a directroy'
dir = os.path.join(folder, element)
print '------------------------------'
print 'visiting folder ' + dir
startingTotalLines = total_lines
visit_the_directory(dir)
endingTotalLines = total_lines
LinesInThisModule = endingTotalLines - startingTotalLines
print 'total lines in ths module : %d' %LinesInThisModule
print 'total lines until now: %d' %(total_lines)
print 'exiting folder ' + dir
print '------------------------------'
else:
file = os.path.join(folder, element)
number_of_lines_in_file(file)
visit_the_directory(os.getcwd())
print 'total lines : %d' %(total_lines)

Wednesday, November 4, 2015

Finding out the system is little endian or big endian system

Here first a value 1 is stored in a int variable. Address of that variable is taken and cast as a char address which will work only on the first byte of the word. If a value one is available in the char then its little endian system otherwise its big endian
# include <stdio.h>
# include <conio.h>
void main()
{
int i = 1;
char *c = (char *) &i;
if (*c)
printf("Little endian");
else
printf("Big endian");
getchar();
}
view raw endian.c hosted with ❤ by GitHub