Start Reading Django Source Code

Sun 05 Sep 2010. in django

I've been using Django for two years. And I always wanted to read the source of such a great framework. I think now is the time. Before start, I wrote a script to see that Django is made of. Here's the result:

.py 842 files(65.6786%) 91427 lines(87.4791%)
.js 27 files(2.1061%) 8722 lines(8.3454%)
.css 8 files(0.6240%) 2242 lines(2.1452%)
.html 83 files(6.4743%) 2122 lines(2.0304%)
2 files(0.1560%) 0 lines(0.0000%)
.vrt 1 files(0.0780%) 0 lines(0.0000%)
.png 7 files(0.5460%) 0 lines(0.0000%)
.mo 121 files(9.4384%) 0 lines(0.0000%)
.po 121 files(9.4384%) 0 lines(0.0000%)
.gif 39 files(3.0421%) 0 lines(0.0000%)
.json 3 files(0.2340%) 0 lines(0.0000%)
.csv 1 files(0.0780%) 0 lines(0.0000%)
.kml 2 files(0.1560%) 0 lines(0.0000%)
.shx 5 files(0.3900%) 0 lines(0.0000%)
.txt 2 files(0.1560%) 0 lines(0.0000%)
.prj 4 files(0.3120%) 0 lines(0.0000%)
.xml 4 files(0.3120%) 0 lines(0.0000%)
.dbf 5 files(0.3900%) 0 lines(0.0000%)
.shp 5 files(0.3900%) 0 lines(0.0000%)

 

The Python script to generate this table.

code_overview.py

 

import sys
import os.path
from collections import defaultdict
from pprint import pprint

ignore_dirs = ['nbproject']
total_files = 0
total_lines = 0
file_results = defaultdict(int)
file_ignores = []
line_results = defaultdict(int)
line_types = ['.py', '.js', '.css', '.html',]
final_results = []

def process_dir(data, dir, files):
    if os.path.basename(dir) in ignore_dirs:
        return
    print "processing %s" % dir
    for file in files:
        path = os.path.join(dir, file)
        if not os.path.isdir(path):
            process_file(path)

def process_file(path):
    global total_files, total_lines
    ext = os.path.splitext(path)[-1]
    file_results[ext] += 1
    total_files += 1
    if ext in line_types:
        line_count = len(open(path, 'r').readlines())
        line_results[ext] += line_count
        total_lines += line_count

def process_results():
    global total_files, total_lines
    for key, file_count in file_results.items():
        line_count = line_results[key]
        result = {
            'ext':key,
            'files':file_count,
            'lines':line_count,
            'file_percent': '%.4f%%' % (float(file_count)/float(total_files) * 100),
            'line_percent': '%.4f%%' % (float(line_count)/float(total_lines) * 100),
            }
        final_results.append(result)

def compare_lines(result1, result2):
    return result2['lines'] - result1['lines']

def sort_results():
    final_results.sort(compare_lines)

def print_results():
    sort_results()
    for result in final_results:
        s = '%(ext)s:\t %(files)s files(%(file_percent)s),\t %(lines)s lines(%(line_percent)s)' % result
        print s

def print_html_results():
    sort_results()
    table = []
    table.append('<table>')
    for result in final_results:
        s = '<tr><td>%(ext)s</td><td>%(files)s files(%(file_percent)s)</td><td>%(lines)s lines(%(line_percent)s)</td></tr>' % result
        table.append(s)
    table.append('</table>')
    print '\n'.join(table)

if __name__ == "__main__":
    if len(sys.argv) < 2:
        print "Please provide the directory you want to process"
        sys.exit()
    os.path.walk(sys.argv[1], process_dir, None)
    process_results()
    if (len(sys.argv)>=3) and (sys.argv[2] == '-html'):
        print_html_results()
    else:
        print_results()

$ python code_overview.py ~/workspace/django/Django-1.2.1/django/ -html

 

 

 

Comment is closed for this post.