<commit_before># -*- coding: utf-8 -*-


# OpenFisca -- A versatile microsimulation software
# By: OpenFisca Team <<EMAIL>>
#
# Copyright (C) 2011, 2012, 2013, 2014 OpenFisca Team
# https://github.com/openfisca
#
# This file is part of OpenFisca.
#
# OpenFisca is free software; you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# OpenFisca is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.


from __future__ import division

import collections
from datetime import date

from biryani1 import strings
import numpy as np

from . import conv
from .enumerations import Enum


def default_frequency_converter(from_ = None, to_ = None):
    """
    Default function to convert Columns between different frequencies
    """
    if (from_ is None) and (to_ is None):
        return lambda x: x

    if (from_ == "year") and (to_ == "trim"):
        return lambda x: x / 4

    if (from_ == "trim") and (to_ == "year"):
        return lambda x: x * 4

    if (from_ == "year") and (to_ == "month"):
        return lambda x: x / 12

    if (from_ == "month") and (to_ == "year"):
        return lambda x: 12 * x


# Base Column


class Column(object):
    _default = 0
    _dtype = float
    _frequency_converter = default_frequency_converter
    end = None
    entity = None
    freq = None
    # json_type = None  # Defined in sub-classes
    label = None
    legislative_input = False
    name = None
    start = None
    val_type = None

    def __init__(self, label = None, default = None, entity= None, start = None, end = None, val_type = None,
            freq = None, legislative_input = True):
        if default is not None and default != self._default:
            self._default = default
        if end is not None:
            self.end = end
        self.entity = entity or 'ind'
        self.freq = freq or 'year'
        if label is not None:
            self.label = label
        if legislative_input:
            self.legislative_input = True
        if start is not None:
            self.start = start
        if val_type is not None and val_type != self.val_type:
            self.val_type = val_type

    def to_json(self):
        self_json = collections.OrderedDict((
            ('@type', self.json_type),
            ))
        if self._default is not None:
            self_json['default'] = self._default
        end = self.end
        if end is not None:
            if isinstance(end, date):
                end = end.isoformat()
            self_json['end'] = end
        if self.entity is not None:
            self_json['entity'] = self.entity
        if self.freq is not None:
            self_json['freq'] = self.freq
        if self.label is not None:
            self_json['label'] = self.label
        if self.name is not None:
            self_json['name'] = self.name
        start = self.start
        if start is not None:
            if isinstance(start, date):
                start = start.isoformat()
            self_json['start'] = start
        if self.val_type is not None:
            self_json['val_type'] = self.val_type
        return self_json


# Level-1 Columns


class BoolCol(Column):<commit_msg>Don't export column freq to JSON when it is 'year'.<commit_after># -*- coding: utf-8 -*-


# OpenFisca -- A versatile microsimulation software
# By: <NAME> <<EMAIL>>
#
# Copyright (C) 2011, 2012, 2013, 2014 OpenFisca Team
# https://github.com/openfisca
#
# This file is part of OpenFisca.
#
# OpenFisca is free software; you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# OpenFisca is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.


from __future__ import division

import collections
from datetime import date

from biryani1 import strings
import numpy as np

from . import conv
from .enumerations import Enum


def default_frequency_converter(from_ = None, to_ = None):
    """
    Default function to convert Columns between different frequencies
    """
    if (from_ is None) and (to_ is None):
        return lambda x: x

    if (from_ == "year") and (to_ == "trim"):
        return lambda x: x / 4

    if (from_ == "trim") and (to_ == "year"):
        return lambda x: x * 4

    if (from_ == "year") and (to_ == "month"):
        return lambda x: x / 12

    if (from_ == "month") and (to_ == "year"):
        return lambda x: 12 * x


# Base Column


class Column(object):
    _default = 0
    _dtype = float
    _frequency_converter = default_frequency_converter
    end = None
    entity = None
    freq = None
    # json_type = None  # Defined in sub-classes
    label = None
    legislative_input = False
    name = None
    start = None
    val_type = None

    def __init__(self, label = None, default = None, entity= None, start = None, end = None, val_type = None,
            freq = None, legislative_input = True):
        if default is not None and default != self._default:
            self._default = default
        if end is not None:
            self.end = end
        self.entity = entity or 'ind'
        self.freq = freq or 'year'
        if label is not None:
            self.label = label
        if legislative_input:
            self.legislative_input = True
        if start is not None:
            self.start = start
        if val_type is not None and val_type != self.val_type:
            self.val_type = val_type

    def to_json(self):
        self_json = collections.OrderedDict((
            ('@type', self.json_type),
            ))
        if self._default is not None:
            self_json['default'] = self._default
        end = self.end
        if end is not None:
            if isinstance(end, date):
                end = end.isoformat()
            self_json['end'] = end
        if self.entity is not None:
            self_json['entity'] = self.entity
        if self.freq != 'year':
            self_json['freq'] = self.freq
        if self.label is not None:
            self_json['label'] = self.label
        if self.name is not None:
            self_json['name'] = self.name
        start = self.start
        if start is not None:
            if isinstance(start, date):
                start = start.isoformat()
            self_json['start'] = start
        if self.val_type is not None:
            self_json['val_type'] = self.val_type
        return self_json


# Level-1 Columns


class BoolCol(Column):
{'fraction_non_alphanumeric': 0.08996728462377317, 'fraction_numerical': 0.007360959651035987, 'mean_word_length': 2.2536585365853656, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 10, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, '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': '5738016', 'n_tokens_mistral': 2165, 'n_tokens_neox': 2079, 'n_words': 815}