<commit_before>from __future__ import division

from datetime import date, timedelta

import tablib

from radar.exporter import queries
from radar.models.results import Observation
from radar.permissions import has_permission_for_patient
from radar.roles import PERMISSION


exporter_map = {}


def register(name):
    def decorator(cls):
        exporter_map[name] = cls
        return cls

    return decorator


def path_getter(path):
    keys = path.split('.')

    def f(value):
        for key in keys:
            if value is None:
                break

            value = getattr(value, key)

        return value

    return f


def none_getter(value):
    return None


def identity_getter(value):
    return value


def query_to_dataset(query, columns):
    data = tablib.Dataset(headers=[c[0] for c in columns])

    for row in query:
        data.append([c[1](row) for c in columns])

    return data


def format_user(user):
    if user.first_name and user.last_name:
        return '%s %s' % (user.first_name, user.last_name)
    else:
        return user.username


def column(name, getter=None):
    if getter is None:
        getter = path_getter(name)
    elif isinstance(getter, basestring):
        getter = path_getter(getter)

    return name, getter


def get_years(months):
    """Get whole number of years."""

    if months is None:
        years = 0
    else:
        years = months // 12

    return years


def get_months(months):
    """Get remaining months."""

    if months is None:
        months = 0
    else:
        months = months % 12

    return 0


def demographics_column_factory(config):
    if config['anonymised']:
        def column(name, getter=None, anonymised_getter=None, patient_getter=None):
            if anonymised_getter is None:
                anonymised_getter = none_getter
            elif isinstance(anonymised_getter, basestring):
                anonymised_getter = path_getter(anonymised_getter)

            return name, anonymised_getter
    elif config['user'] is not None:
        def column(name, getter=None, anonymised_getter=None, patient_getter=None):<commit_msg>Add exporter tests<commit_after>from __future__ import division

from datetime import date, timedelta

import tablib

from radar.exporter import queries
from radar.models.results import Observation
from radar.permissions import has_permission_for_patient
from radar.roles import PERMISSION
from radar.exporter.utils import get_months, get_years, path_getter, identity_getter, none_getter


exporter_map = {}


def register(name):
    def decorator(cls):
        exporter_map[name] = cls
        return cls

    return decorator


def query_to_dataset(query, columns):
    data = tablib.Dataset(headers=[c[0] for c in columns])

    for row in query:
        data.append([c[1](row) for c in columns])

    return data


def format_user(user):
    if user.first_name and user.last_name:
        return '%s %s' % (user.first_name, user.last_name)
    else:
        return user.username


def column(name, getter=None):
    if getter is None:
        getter = path_getter(name)
    elif isinstance(getter, basestring):
        getter = path_getter(getter)

    return name, getter


def demographics_column_factory(config):
    if config['anonymised']:
        def column(name, getter=None, anonymised_getter=None, patient_getter=None):
            if anonymised_getter is None:
                anonymised_getter = none_getter
            elif isinstance(anonymised_getter, basestring):
                anonymised_getter = path_getter(anonymised_getter)

            return name, anonymised_getter
    elif config['user'] is not None:
        def column(name, getter=None, anonymised_getter=None, patient_getter=None):
{'fraction_non_alphanumeric': 0.08044752264251465, 'fraction_numerical': 0.002930207778369739, 'mean_word_length': 2.9484752891692954, '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': '93131', 'n_tokens_mistral': 1184, 'n_tokens_neox': 1127, 'n_words': 319}