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


from __future__ import division

import numpy as np

from . import periods
from .tools import empty_clone


class DatedHolder(object):
    """A view of an holder, for a given period"""
    holder = None
    period = None

    def __init__(self, holder, period):
        self.holder = holder
        self.period = period

    @property
    def array(self):
        return self.holder.get_array(self.period)

    @array.deleter
    def array(self):
        self.holder.delete_array(self.period)

    @array.setter
    def array(self, array):
        self.holder.set_array(self.period, array)

    @property
    def column(self):
        return self.holder.column

    @property
    def entity(self):
        return self.holder.entity

    def to_value_json(self, use_label = False):
        transform_dated_value_to_json = self.holder.column.transform_dated_value_to_json
        return [
            transform_dated_value_to_json(cell, use_label = use_label)
            for cell in self.array.tolist()
            ]


class Holder(object):
    _array = None  # Only used when column.is_permanent
    _array_by_period = None  # Only used when not column.is_permanent
    column = None
    entity = None
    formula = None
    formula_output_period_by_requested_period = None

    def __init__(self, column = None, entity = None):
        assert column is not None
        assert self.column is None
        self.column = column
        assert entity is not None
        self.entity = entity

    @property
    def array(self):
        if not self.column.is_permanent:
            return self.get_array(self.entity.simulation.period)
        return self._array

    @array.setter
    def array(self, array):
        simulation = self.entity.simulation
        if not self.column.is_permanent:
            return self.set_array(simulation.period, array)
        if simulation.debug or simulation.trace:
            variable_infos = (self.column.name, None)
            step = simulation.traceback.get(variable_infos)
            if step is None:
                simulation.traceback[variable_infos] = dict(
                    holder = self,
                    )
        self._array = array

    def at_period(self, period):
        return self if self.column.is_permanent else DatedHolder(self, period)

    def calculate(self, period = None, **parameters):
        dated_holder = self.compute(period = period, **parameters)
        return dated_holder.array

    def calculate_output(self, period):
        return self.formula.calculate_output(period)

    def clone(self):
        """Copy the holder just enough to be able to run a new simulation without modifying the original simulation."""
        new = empty_clone(self)
        new_dict = new.__dict__

        for key, value in self.__dict__.iteritems():
            if key in ('_array_by_period',):
                if value is not None:
                    # There is no need to copy the arrays, because the formulas don't modify them.
                    new_dict[key] = value.copy()
            elif key not in ('entity', 'formula'):
                new_dict[key] = value

        new_dict['entity'] = self.entity
        # Caution: formula must be cloned after the entity has been set into new.
        formula = self.formula
        if formula is not None:
            new_dict['formula'] = formula.clone(new)

        return new

    def compute(self, period = None, **parameters):
        """Compute array if needed and/or convert it to requested period and return a dated holder containig it.

        The returned dated holder is always of the requested period and this method never returns None.
        """
        entity = self.entity
        simulation = entity.simulation
        if period is None:
            period = simulation.period
        column = self.column
        accept_other_period = parameters.get('accept_other_period', False)

        # First look for dated_holders covering the whole period (without hole).
        dated_holder = self.at_period(period)
        if dated_holder.array is not None:
            return dated_holder
        assert self._array is None  # self._array should always be None when dated_holder.array is None.

        column_start_instant = periods.instant(column.start)
        column_stop_instant = periods.instant(column.end)
        if (column_start_instant is None or column_start_instant <= period.start) \
                and (column_stop_instant is None or period.start <= column_stop_instant):
            formula_dated_holder = self.formula.compute(period = period, **parameters)
            assert formula_dated_holder is not None
            if not column.is_permanent:
                assert accept_other_period or formula_dated_holder.period == period, \
                    u"Requested period {} differs from {} returned by variable {}".format(period,
                        formula_dated_holder.period, column.name)
            return formula_dated_holder
        array = np.empty(entity.count, dtype = column.dtype)
        array.fill(column.default)
        dated_holder.array = array
        return dated_holder

    def compute_add(self, period = None, **parameters):
        dated_holder = self.at_period(period)
        if dated_holder.array is not None:
            return dated_holder

        array = None
        unit = period.unit
        if unit == u'month':
            remaining_period_months = period.size
        else:<commit_msg>Change assert string to be str, not unicode<commit_after># -*- coding: utf-8 -*-


from __future__ import division

import numpy as np

from . import periods
from .tools import empty_clone


class DatedHolder(object):
    """A view of an holder, for a given period"""
    holder = None
    period = None

    def __init__(self, holder, period):
        self.holder = holder
        self.period = period

    @property
    def array(self):
        return self.holder.get_array(self.period)

    @array.deleter
    def array(self):
        self.holder.delete_array(self.period)

    @array.setter
    def array(self, array):
        self.holder.set_array(self.period, array)

    @property
    def column(self):
        return self.holder.column

    @property
    def entity(self):
        return self.holder.entity

    def to_value_json(self, use_label = False):
        transform_dated_value_to_json = self.holder.column.transform_dated_value_to_json
        return [
            transform_dated_value_to_json(cell, use_label = use_label)
            for cell in self.array.tolist()
            ]


class Holder(object):
    _array = None  # Only used when column.is_permanent
    _array_by_period = None  # Only used when not column.is_permanent
    column = None
    entity = None
    formula = None
    formula_output_period_by_requested_period = None

    def __init__(self, column = None, entity = None):
        assert column is not None
        assert self.column is None
        self.column = column
        assert entity is not None
        self.entity = entity

    @property
    def array(self):
        if not self.column.is_permanent:
            return self.get_array(self.entity.simulation.period)
        return self._array

    @array.setter
    def array(self, array):
        simulation = self.entity.simulation
        if not self.column.is_permanent:
            return self.set_array(simulation.period, array)
        if simulation.debug or simulation.trace:
            variable_infos = (self.column.name, None)
            step = simulation.traceback.get(variable_infos)
            if step is None:
                simulation.traceback[variable_infos] = dict(
                    holder = self,
                    )
        self._array = array

    def at_period(self, period):
        return self if self.column.is_permanent else DatedHolder(self, period)

    def calculate(self, period = None, **parameters):
        dated_holder = self.compute(period = period, **parameters)
        return dated_holder.array

    def calculate_output(self, period):
        return self.formula.calculate_output(period)

    def clone(self):
        """Copy the holder just enough to be able to run a new simulation without modifying the original simulation."""
        new = empty_clone(self)
        new_dict = new.__dict__

        for key, value in self.__dict__.iteritems():
            if key in ('_array_by_period',):
                if value is not None:
                    # There is no need to copy the arrays, because the formulas don't modify them.
                    new_dict[key] = value.copy()
            elif key not in ('entity', 'formula'):
                new_dict[key] = value

        new_dict['entity'] = self.entity
        # Caution: formula must be cloned after the entity has been set into new.
        formula = self.formula
        if formula is not None:
            new_dict['formula'] = formula.clone(new)

        return new

    def compute(self, period = None, **parameters):
        """Compute array if needed and/or convert it to requested period and return a dated holder containig it.

        The returned dated holder is always of the requested period and this method never returns None.
        """
        entity = self.entity
        simulation = entity.simulation
        if period is None:
            period = simulation.period
        column = self.column
        accept_other_period = parameters.get('accept_other_period', False)

        # First look for dated_holders covering the whole period (without hole).
        dated_holder = self.at_period(period)
        if dated_holder.array is not None:
            return dated_holder
        assert self._array is None  # self._array should always be None when dated_holder.array is None.

        column_start_instant = periods.instant(column.start)
        column_stop_instant = periods.instant(column.end)
        if (column_start_instant is None or column_start_instant <= period.start) \
                and (column_stop_instant is None or period.start <= column_stop_instant):
            formula_dated_holder = self.formula.compute(period = period, **parameters)
            assert formula_dated_holder is not None
            if not column.is_permanent:
                assert accept_other_period or formula_dated_holder.period == period, \
                    "Requested period {} differs from {} returned by variable {}".format(period,
                        formula_dated_holder.period, column.name)
            return formula_dated_holder
        array = np.empty(entity.count, dtype = column.dtype)
        array.fill(column.default)
        dated_holder.array = array
        return dated_holder

    def compute_add(self, period = None, **parameters):
        dated_holder = self.at_period(period)
        if dated_holder.array is not None:
            return dated_holder

        array = None
        unit = period.unit
        if unit == u'month':
            remaining_period_months = period.size
        else:
{'fraction_non_alphanumeric': 0.0762137238947654, 'fraction_numerical': 0.0001808154778049001, 'mean_word_length': 2.266981689308919, 'pattern_counts': {'":': 0, '<': 7, '<?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': '2818675', 'n_tokens_mistral': 2993, 'n_tokens_neox': 2889, 'n_words': 900}