<commit_before>from __future__ import division
import csv

from django.core.management.base import BaseCommand, CommandError
from django.core.exceptions import ObjectDoesNotExist
from core.models import *

class Command(BaseCommand):
    help = "Computes statistics for grades"

    def handle(self, *args, **options):
        compute_stats(options['verbosity'])

def compute_stats(verbosity, term=None):
    """Computes statistics for subjects, courses, and sections. If
    TERM is None (default), we look at all sections, and otherwise
    we only update the sections for that term.
    Verbosity ranges from
        0: no output to console
        1: prints subject once completed
        2: prints course and subject once completed
        3: prints % of sections processed after each section
    """
    if term is None:
        total_sections = Section.objects.count()
    else:
        total_sections = Section.objects.filter(term=term).count()
    processed_sections = 0
    for subject in Subject.objects.all():
        for course in subject.course_set.all():
            if term is None:
                sec_set = course.section_set.all()
            else:
                sec_set = course.section_set.filter(term=term)
            for section in sec_set:
                section.compute_stats()
                processed_sections += 1
                if verbosity >= 3:
                    print "compute_stats: {0} of {1} sections processed ({2:.2f}%)".format(
                          processed_sections, total_sections,
                          100 * (processed_sections / total_sections))
            course.compute_stats()
            if verbosity >= 2:
                print "compute stats: processed course {}".format(course)
        subject.compute_stats()
        if verbosity >= 1:
            print "compute_stats: processed subject {}".format(subject)<commit_msg>fix bug with stats not saving<commit_after>from __future__ import division
import csv

from django.core.management.base import BaseCommand, CommandError
from django.core.exceptions import ObjectDoesNotExist
from core.models import *

class Command(BaseCommand):
    help = "Computes statistics for grades"

    def handle(self, *args, **options):
        compute_stats(options['verbosity'])

def compute_stats(verbosity, term=None):
    """Computes statistics for subjects, courses, and sections. If
    TERM is None (default), we look at all sections, and otherwise
    we only update the sections for that term.
    Verbosity ranges from
        0: no output to console
        1: prints subject once completed
        2: prints course and subject once completed
        3: prints % of sections processed after each section
    """
    if term is None:
        total_sections = Section.objects.count()
    else:
        total_sections = Section.objects.filter(term=term).count()
    processed_sections = 0
    for subject in Subject.objects.all():
        for course in subject.course_set.all():
            if term is None:
                sec_set = course.section_set.all()
            else:
                sec_set = course.section_set.filter(term=term)
            for section in sec_set:
                section.compute_stats()
                section.save()
                processed_sections += 1
                if verbosity >= 3:
                    print "compute_stats: {0} of {1} sections processed ({2:.2f}%)".format(
                          processed_sections, total_sections,
                          100 * (processed_sections / total_sections))
            course.compute_stats()
            course.save()
            if verbosity >= 2:
                print "compute stats: processed course {}".format(course)
        subject.compute_stats()
        subject.save()
        if verbosity >= 1:
            print "compute_stats: processed subject {}".format(subject)
{'fraction_non_alphanumeric': 0.08234383066597832, 'fraction_numerical': 0.008260196179659268, 'mean_word_length': 2.267284991568297, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6885072', 'n_tokens_mistral': 991, 'n_tokens_neox': 950, 'n_words': 323}