Skip to content

Instantly share code, notes, and snippets.

@nychng
Created September 1, 2012 07:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nychng/3566351 to your computer and use it in GitHub Desktop.
Save nychng/3566351 to your computer and use it in GitHub Desktop.
Django Project Code Counter!
from django.core.management.base import BaseCommand
from django.conf import settings
import os
class Command(BaseCommand):
"""
This counts the code for each app in the project and displays the
lines of code of unit test written.
"""
help = 'Display Project Code Count and Unit Test Code count'
def handle(self, *args, **options):
file_types = ['views.py', 'forms.py', 'models.py', 'utils.py', 'tests.py']
counter = 0
unit_test_counter = 0
for app in settings.APPS:
for file_type in file_types:
file_name = app + '/' + file_type
if os.path.exists(file_name):
with open(file_name) as f:
for row in f:
counter += 1
if file_type == 'tests.py':
unit_test_counter += 1
print 'Total lines of code in bbox only apps: %s' % counter
print 'Total lines of code in bbox excluding tests: %s' % (counter - unit_test_counter)
print 'Total lines of code in bbox tests: %s' % unit_test_counter
print 'For every 1 line of code written, %s lines of test code is written' \
% (float(counter-unit_test_counter) / float(unit_test_counter))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment