<commit_before># -*- coding: utf-8 -*-
"""
Helper functions used in views.
"""

import csv
import xml.etree.ElementTree as etree
from json import dumps
from functools import wraps
from datetime import datetime

from flask import Response

from presence_analyzer.main import app

import logging
log = logging.getLogger(__name__)  # pylint: disable-msg=C0103


def jsonify(function):
    """
    Creates a response with the JSON representation of wrapped function result.
    """
    @wraps(function)
    def inner(*args, **kwargs):
        return Response(dumps(function(*args, **kwargs)),
                        mimetype='application/json')
    return inner


def get_data():
    """
    Extracts presence data from CSV file and groups it by user_id.

    It creates structure like this:
    data = {
        'user_id': {
            datetime.date(2013, 10, 1): {
                'start': datetime.time(9, 0, 0),
                'end': datetime.time(17, 30, 0),
            },
            datetime.date(2013, 10, 2): {
                'start': datetime.time(8, 30, 0),
                'end': datetime.time(16, 45, 0),
            },
        }
    }
    """
    usage_id = get_details()
    data = {}
    err = []

    with open(app.config['DATA_CSV'], 'r') as csvfile:
        presence_reader = csv.reader(csvfile, delimiter=',')
        for i, row in enumerate(presence_reader):<commit_msg>bug #1649 added cache decorator<commit_after># -*- coding: utf-8 -*-
"""
Helper functions used in views.
"""

import csv
import Queue
import xml.etree.ElementTree as etree
from json import dumps
from functools import wraps
from datetime import datetime, timedelta

from flask import Response

from presence_analyzer.main import app

import logging
log = logging.getLogger(__name__)  # pylint: disable-msg=C0103


def jsonify(function):
    """
    Creates a response with the JSON representation of wrapped function result.
    """
    @wraps(function)
    def inner(*args, **kwargs):
        return Response(dumps(function(*args, **kwargs)),
                        mimetype='application/json')
    return inner


_cache = [datetime.now().time(), {}]


def cache(sec):
    """
    Save to cash for given period of time function result.
    """
    def decorator(function):
        def wrapper(*args, **kwargs):
            now = datetime.now()
            if _cache[0] > now.time():
                return _cache[1]
            _cache[0] = (now + timedelta(seconds=sec)).time()
            _cache[1] = function(*args, **kwargs)
            return _cache[1]
        return wrapper
    return decorator


@cache(600)
def get_data():
    """
    Extracts presence data from CSV file and groups it by user_id.

    It creates structure like this:
    data = {
        'user_id': {
            datetime.date(2013, 10, 1): {
                'start': datetime.time(9, 0, 0),
                'end': datetime.time(17, 30, 0),
            },
            datetime.date(2013, 10, 2): {
                'start': datetime.time(8, 30, 0),
                'end': datetime.time(16, 45, 0),
            },
        }
    }
    """
    usage_id = get_details()
    data = {}
    err = []

    with open(app.config['DATA_CSV'], 'r') as csvfile:
        presence_reader = csv.reader(csvfile, delimiter=',')
        for i, row in enumerate(presence_reader):
{'fraction_non_alphanumeric': 0.12473700030057108, 'fraction_numerical': 0.025247971145175834, 'mean_word_length': 2.2755905511811023, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '6250564', 'n_tokens_mistral': 1061, 'n_tokens_neox': 973, 'n_words': 289}