<commit_before># -*- coding: utf-8 -*-
from __future__ import (
    division, absolute_import, print_function, unicode_literals,
)
from builtins import *                  # noqa
from future.builtins.disabled import *  # noqa

from collections import namedtuple

from magic_constraints.exception import MagicSyntaxError


class Parameter(object):

    def __init__(self, name, type_, **options):
        self.name = name

        raise_on_nontype_object(type_)
        self.type_ = type_

        # key only arguemnt.
        # 1. if nullable, None is accepted.
        self.nullable = options.get('nullable', False)
        # 2. record default value.
        if 'default' in options:
            self.with_default = True
            self.default = options['default']
        else:
            self.with_default = False
            self.default = None
        # 3. user-defined validator on input argument.
        self.validator = options.get(
            'validator', lambda instance: True,
        )

        self._arguments_repr = self._generate_init_arguments_repr(
            name, type_, **options
        )

    def check_argument(self, instance):
        if instance is None:
            if not (self.nullable or issubclass(self.type_, type(None))):
                return False
            else:
                return self.validator(None)
        if not isinstance(instance, self.type_):
            return False
        return self.validator(instance)

    def _generate_init_arguments_repr(self, name, type_, **options):
        # positional arguments.
        prefix = "name={name}, type_={type_}".format(
            name=conditional_repr(name),
            type_=conditional_repr(type_),
        )
        # keyword-only arguments.
        suffix = ', '.join(map(
            lambda p: "{0}={1}".format(p[0], conditional_repr(p[1])),
            sorted(options.items(), key=lambda p: p[0]),
        ))

        # merge.
        if suffix:
            arguemnt_repr = "{0}, {1}".format(prefix, suffix)
        else:
            arguemnt_repr = prefix

        return arguemnt_repr

    def __repr__(self):
        return repr_return(
            'Parameter({})'.format(self._arguments_repr),
        )


def build_parameter_package(parameters):
    raise_on_non_parameters(parameters)

    name_hash = {}
    start_of_defaults = -1

    idx = 0
    for parameter in parameters:

        if parameter.name in name_hash:
            raise MagicSyntaxError(
                'redefinition of parameter',<commit_msg>Refactored Parameter.<commit_after># -*- coding: utf-8 -*-
from __future__ import (
    division, absolute_import, print_function, unicode_literals,
)
from builtins import *                  # noqa
from future.builtins.disabled import *  # noqa

from collections import namedtuple

from magic_constraints.exception import MagicSyntaxError


class BaseType(object):

    def __init__(self, type_, **options):

        raise_on_nontype_object(type_)
        self.type_ = type_

        # key only arguemnt.
        # 1. if nullable, None is accepted.
        self.nullable = options.get('nullable', False)

        # 2. record default value.
        # NOTICE that ReturnType do not support default.
        if 'default' in options:
            self.with_default = True
            self.default = options['default']
        else:
            self.with_default = False
            self.default = None

        # 3. user-defined validator on input argument.
        self.validator = options.get(
            'validator', lambda instance: True,
        )

        # generator serialized string for repr.
        self._arguments_repr = '{prefix}, {suffix}'.format(
            # op 1.
            prefix=self.init_arguments_repr_prefix(type_, **options),
            # common suffix.
            suffix=self._init_arguments_repr_suffix(type_, **options),
        )

    def check_argument(self, instance):
        if instance is None:
            if not (self.nullable or issubclass(self.type_, type(None))):
                return False
            else:
                return self.validator(None)
        if not isinstance(instance, self.type_):
            return False
        return self.validator(instance)

    def _init_arguments_repr_suffix(self, type_, **options):
        # positional arguments.
        prefix = "type_={type_}".format(
            type_=conditional_repr(type_),
        )
        # keyword-only arguments.
        suffix = ', '.join(map(
            lambda p: "{0}={1}".format(p[0], conditional_repr(p[1])),
            sorted(options.items(), key=lambda p: p[0]),
        ))

        # merge.
        if suffix:
            arguemnt_repr = "{0}, {1}".format(prefix, suffix)
        else:
            arguemnt_repr = prefix

        return arguemnt_repr


class Parameter(BaseType):

    def __init__(self, name, type_, **options):
        self.name = name
        super().__init__(type_, **options)

    def init_arguments_repr_prefix(self, type_, **options):
        return 'name={0}'.format(
            conditional_repr(self.name),
        )

    def __repr__(self):
        return repr_return(
            'Parameter({})'.format(self._arguments_repr),
        )


def build_parameter_package(parameters):
    raise_on_non_parameters(parameters)

    name_hash = {}
    start_of_defaults = -1

    idx = 0
    for parameter in parameters:

        if parameter.name in name_hash:
            raise MagicSyntaxError(
                'redefinition of parameter',
{'fraction_non_alphanumeric': 0.1004897514964629, 'fraction_numerical': 0.005078904407763468, 'mean_word_length': 2.0925406618059452, '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': '7634811', 'n_tokens_mistral': 1580, 'n_tokens_neox': 1521, 'n_words': 374}