Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.