class DataInsights: def __init__(self): self.db = Database('messages') def no_of_responses(self, question_no): return self.db.count_docs('received', {'question_no': question_no}) def choice_distribution(self, question_no): dist = {} aggs = self.db.aggregate('received', 'text', {'question_no': question_no}) for item in aggs: dist[item['_id']] = item['count'] return dist
def parse_file(filepath): total_lines = sum((1 for i in open(filepath, 'rb'))) bar = progressbar.ProgressBar(maxval=total_lines, widgets=[ progressbar.Bar('=', '[', ']'), ' ', progressbar.Percentage() ]) bar.start() with open(filepath, 'r') as file_object: with Database() as db: count = 0 line = file_object.readline() while line: keys, matches = _parse_line(line) if keys.__contains__( 'instruction') and not keys.__contains__('trap'): instruction = matches[keys.index('instruction')].group( 'instruction') trap = 'v' db.save_instruction(instruction, trap, filepath, count) elif keys.__contains__('instruction') and keys.__contains__( 'trap'): instruction = matches[keys.index('instruction')].group( 'instruction') trap = matches[keys.index('trap')].group('trap') db.save_instruction(instruction, trap, filepath, count) count = count + 1 bar.update(count) line = file_object.readline() bar.finish()
def __init__(self): self.db = Database('messages')
def reset_db(): with Database() as db: db.reset()