<commit_before>from flask.ext.script import Command, Option

from flask import current_app
from sqlalchemy.orm import joinedload
from skylines.model import db, Flight
from skylines.lib.xcsoar_ import analyse_flight
from skylines.worker import tasks
from datetime import datetime

class Analyze(Command):
    """ (Re)analyze flights """

    option_list = (
        Option('--force', action='store_true',
               help='re-analyse all flights, not just the scheduled ones'),
        Option('ids', metavar='ID', nargs='*', type=int,
               help='Any number of flight IDs.'),
    )

    def run(self, force, ids):
        if force:
            # invalidate all results
            db.session.query(Flight).update({'needs_analysis': True})

        q = db.session.query(Flight)
        q = q.options(joinedload(Flight.igc_file))
        if ids:
            self.apply_and_commit(self.do, q.filter(Flight.id.in_(ids)))
        else:
            self.incremental(self.do, q.filter(Flight.needs_analysis == True))

    def do(self, flight):
        print flight.id
        return analyse_flight(flight)

    def apply_and_commit(self, func, q):<commit_msg>commands.flights.analysis: make flake happy<commit_after>from flask.ext.script import Command, Option

from flask import current_app
from sqlalchemy.orm import joinedload
from skylines.model import db, Flight
from skylines.lib.xcsoar_ import analyse_flight
from skylines.worker import tasks
from datetime import datetime


class Analyze(Command):
    """ (Re)analyze flights """

    option_list = (
        Option('--force', action='store_true',
               help='re-analyse all flights, not just the scheduled ones'),
        Option('ids', metavar='ID', nargs='*', type=int,
               help='Any number of flight IDs.'),
    )

    def run(self, force, ids):
        if force:
            # invalidate all results
            db.session.query(Flight).update({'needs_analysis': True})

        q = db.session.query(Flight)
        q = q.options(joinedload(Flight.igc_file))
        if ids:
            self.apply_and_commit(self.do, q.filter(Flight.id.in_(ids)))
        else:
            self.incremental(self.do, q.filter(Flight.needs_analysis == True))

    def do(self, flight):
        print flight.id
        return analyse_flight(flight)

    def apply_and_commit(self, func, q):
{'fraction_non_alphanumeric': 0.11327959270258804, 'fraction_numerical': 0.0, 'mean_word_length': 2.821717990275527, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '4995816', 'n_tokens_mistral': 732, 'n_tokens_neox': 694, 'n_words': 178}