<commit_before>from datetime import date
from decimal import Decimal, ROUND_DOWN
from flask import Flask, request, render_template
from flask.ext.restless import APIManager
from sqlalchemy.orm import scoped_session
from scraper import Session
from scraper.models import Department, Course, Section, SectionInstance
from scraper.stats import most_full_filters, biggest_filters

app = Flask(__name__)
app.debug = True

# initialize API
session = scoped_session(Session)
manager = APIManager(app, session=session)
manager.create_api(Department)
manager.create_api(Course)
manager.create_api(Section)
manager.create_api(SectionInstance)

@app.route('/')
def index():
    return render_template('index.html')

@app.route('/mostfull/', methods=['GET', 'POST'])
def mostfull():
    error = None
    if request.method == 'POST':
        try:
            year = int(request.form['year'])
            month = int(request.form['month'])
            day = int(request.form['day'])
            d = date(year, month, day)
        except:
            d = date.today()
        try:
            if request.form['dept'] != 'all':
                print request.form['dept']
                dept = session.query(Department).\
                               filter(Department.abbreviation == request.form['dept']).\
                               first()
            else:
                dept = None
        except:
            dept = None
        full = most_full_filters(session, day=d, dept=dept, fmt='LEC')
    else:
        full = most_full_filters(session, fmt='LEC')
    depts = session.query(Department.abbreviation, Department.name).all()
    ranked = []
    for i in range(len(full)):
        pair = full[i]
        percent = Decimal(pair[1]).quantize(Decimal('.01'),
                                            rounding=ROUND_DOWN)
        if i > 0 and percent == ranked[i - 1][2]:
            rank = ranked[i - 1][0]
        else:
            rank = i + 1
        triplet = (rank, pair[0], percent)
        ranked.append(triplet)
    return render_template('mostfull.html', ranked=ranked, depts=depts)

@app.route('/biggest/', methods=['GET', 'POST'])
def biggest():
    error = None
    if request.method == 'POST':
        try:
            if request.form['dept'] != 'all':
                print request.form['dept']
                dept = session.query(Department).\
                               filter(Department.abbreviation == request.form['dept']).\
                               first()
            else:
                dept = None
        except:
            dept = None
        big = biggest_filters(session, dept=dept)
    else:
        big = biggest_filters(session)
    depts = session.query(Department.abbreviation, Department.name).all()<commit_msg>Fix Python 2.6 bug with Decimal.<commit_after>from datetime import date
from decimal import Decimal, ROUND_DOWN
from flask import Flask, request, render_template
from flask.ext.restless import APIManager
from sqlalchemy.orm import scoped_session
from scraper import Session
from scraper.models import Department, Course, Section, SectionInstance
from scraper.stats import most_full_filters, biggest_filters

app = Flask(__name__)
app.debug = True

# initialize API
session = scoped_session(Session)
manager = APIManager(app, session=session)
manager.create_api(Department)
manager.create_api(Course)
manager.create_api(Section)
manager.create_api(SectionInstance)

@app.route('/')
def index():
    return render_template('index.html')

@app.route('/mostfull/', methods=['GET', 'POST'])
def mostfull():
    error = None
    if request.method == 'POST':
        try:
            year = int(request.form['year'])
            month = int(request.form['month'])
            day = int(request.form['day'])
            d = date(year, month, day)
        except:
            d = date.today()
        try:
            if request.form['dept'] != 'all':
                print request.form['dept']
                dept = session.query(Department).\
                               filter(Department.abbreviation == request.form['dept']).\
                               first()
            else:
                dept = None
        except:
            dept = None
        full = most_full_filters(session, day=d, dept=dept, fmt='LEC')
    else:
        full = most_full_filters(session, fmt='LEC')
    depts = session.query(Department.abbreviation, Department.name).all()
    ranked = []
    for i in range(len(full)):
        pair = full[i]
        percent = Decimal(str(pair[1])).quantize(Decimal('.01'),
                                                 rounding=ROUND_DOWN)
        if i > 0 and percent == ranked[i - 1][2]:
            rank = ranked[i - 1][0]
        else:
            rank = i + 1
        triplet = (rank, pair[0], percent)
        ranked.append(triplet)
    return render_template('mostfull.html', ranked=ranked, depts=depts)

@app.route('/biggest/', methods=['GET', 'POST'])
def biggest():
    error = None
    if request.method == 'POST':
        try:
            if request.form['dept'] != 'all':
                print request.form['dept']
                dept = session.query(Department).\
                               filter(Department.abbreviation == request.form['dept']).\
                               first()
            else:
                dept = None
        except:
            dept = None
        big = biggest_filters(session, dept=dept)
    else:
        big = biggest_filters(session)
    depts = session.query(Department.abbreviation, Department.name).all()
{'fraction_non_alphanumeric': 0.12182375202739232, 'fraction_numerical': 0.003964678320418093, 'mean_word_length': 2.1302876480541455, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2432098', 'n_tokens_mistral': 1597, 'n_tokens_neox': 1547, 'n_words': 366}