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

import logging

from .. import config

from enum import Enum
from textwrap import TextWrapper
from numbers import Integral, Real, Number
from collections.abc import Set, Sized, Iterable, Mapping


__all__ = ['val2dzn', 'stmt2dzn', 'stmt2enum', 'dict2dzn', 'rebase_array']


_wrapper = None


def _get_wrapper():
    global _wrapper
    if not _wrapper:
        _wrapper = TextWrapper(
            width=int(config.dzn_width), subsequent_indent=' '*4,
            break_long_words=False, break_on_hyphens = False
        )
    return _wrapper


def _is_bool(obj):
    return isinstance(obj, bool)


def _is_enum(obj):
    return isinstance(obj, Enum)


def _is_int(obj):
    return isinstance(obj, Integral)


def _is_float(obj):
    return isinstance(obj, Real)


def _is_value(obj):
    return isinstance(obj, (bool, str, Enum, Number))


def _is_set(obj):
    return isinstance(obj, Set) and all(map(_is_value, obj))


def _is_elem(obj):
    return _is_value(obj) or _is_set(obj)


def _is_list(obj):
    return (
        isinstance(obj, Sized) and isinstance(obj, Iterable) and
        not isinstance(obj, (str, Set, Mapping))
    )


def _is_dict(obj):
    return isinstance(obj, Mapping)


def _is_array_type(obj):
    return _is_list(obj) or _is_dict(obj)


def _list_index_set(obj):
    return 1, len(obj)


def _extremes(s):
    return min(s), max(s)


def _is_int_set(obj):
    return all(map(_is_int, obj))


def _is_contiguous(obj, min_val, max_val):
    return all([v in obj for v in range(min_val, max_val + 1)])


def _index_set(obj):
    if _is_list(obj):
        if len(obj) == 0:
            return ()
        if all(map(_is_elem, obj)):
            return _list_index_set(obj),
        if all(map(_is_array_type, obj)):
            idx_sets = list(map(_index_set, obj))
            # all children index-sets must be identical
            if idx_sets[1:] == idx_sets[:-1]:
                return (_list_index_set(obj),) + idx_sets[0]
    elif _is_dict(obj):
        if len(obj) == 0:
            return ()
        keys = obj.keys()
        if _is_int_set(keys):
            min_val, max_val = _extremes(keys)
            if _is_contiguous(keys, min_val, max_val):
                idx_set = (min_val, max_val),
                if all(map(_is_elem, obj.values())):
                    return idx_set
                if all(map(_is_array_type, obj.values())):
                    idx_sets = list(map(_index_set, obj.values()))
                    # all children index-sets must be identical
                    if idx_sets[1:] == idx_sets[:-1]:
                        return idx_set + idx_sets[0]
    raise ValueError(
        'The input object is not a proper array: {}'.format(repr(obj)), obj
    )


def _flatten_array(arr, lvl):
    if _is_dict(arr):
        arr_it = arr.values()
    else:
        arr_it = arr

    if lvl == 1:
        return arr_it

    flat_arr = []
    for sub_arr in arr_it:
        flat_arr.extend(_flatten_array(sub_arr, lvl - 1))
    return flat_arr


def _dzn_val(val):
    if isinstance(val, bool):
        return 'true' if val else 'false'
    if isinstance(val, Enum):
        return val.name
    return str(val)


def _dzn_set(s):
    if s and _is_int_set(s):
        min_val, max_val = _extremes(s)
        if _is_contiguous(s, min_val, max_val):
            return '{}..{}'.format(min_val, max_val)  # contiguous set
    return '{{{}}}'.format(', '.join(map(_dzn_val, s)))


def _index_set_str(idx_set):
    return ', '.join(['{}..{}'.format(*s) for s in idx_set])


def _dzn_array_nd(arr):
    idx_set = _index_set(arr)
    dim = max([len(idx_set), 1])
    if dim > 6:  # max 6-dimensional array in dzn language
        raise ValueError((
            'The input array has {} dimensions. Minizinc supports arrays of '
            'up to 6 dimensions.'
        ).format(dim), arr)

    if _is_dict(arr):
        arr_it = arr.values()
    else:
        arr_it = arr<commit_msg>dzn/marsh: fix serialization of empty sets<commit_after># -*- coding: utf-8 -*-

import logging

from .. import config

from enum import Enum
from textwrap import TextWrapper
from numbers import Integral, Real, Number
from collections.abc import Set, Sized, Iterable, Mapping


__all__ = ['val2dzn', 'stmt2dzn', 'stmt2enum', 'dict2dzn', 'rebase_array']


_wrapper = None


def _get_wrapper():
    global _wrapper
    if not _wrapper:
        _wrapper = TextWrapper(
            width=int(config.dzn_width), subsequent_indent=' '*4,
            break_long_words=False, break_on_hyphens = False
        )
    return _wrapper


def _is_bool(obj):
    return isinstance(obj, bool)


def _is_enum(obj):
    return isinstance(obj, Enum)


def _is_int(obj):
    return isinstance(obj, Integral)


def _is_float(obj):
    return isinstance(obj, Real)


def _is_value(obj):
    return isinstance(obj, (bool, str, Enum, Number))


def _is_set(obj):
    return isinstance(obj, Set) and all(map(_is_value, obj))


def _is_elem(obj):
    return _is_value(obj) or _is_set(obj)


def _is_list(obj):
    return (
        isinstance(obj, Sized) and isinstance(obj, Iterable) and
        not isinstance(obj, (str, Set, Mapping))
    )


def _is_dict(obj):
    return isinstance(obj, Mapping)


def _is_array_type(obj):
    return _is_list(obj) or _is_dict(obj)


def _list_index_set(obj):
    return 1, len(obj)


def _extremes(s):
    return min(s), max(s)


def _is_int_set(obj):
    return all(map(_is_int, obj))


def _is_contiguous(obj, min_val, max_val):
    return all([v in obj for v in range(min_val, max_val + 1)])


def _index_set(obj):
    if _is_list(obj):
        if len(obj) == 0:
            return ()
        if all(map(_is_elem, obj)):
            return _list_index_set(obj),
        if all(map(_is_array_type, obj)):
            idx_sets = list(map(_index_set, obj))
            # all children index-sets must be identical
            if idx_sets[1:] == idx_sets[:-1]:
                return (_list_index_set(obj),) + idx_sets[0]
    elif _is_dict(obj):
        if len(obj) == 0:
            return ()
        keys = obj.keys()
        if _is_int_set(keys):
            min_val, max_val = _extremes(keys)
            if _is_contiguous(keys, min_val, max_val):
                idx_set = (min_val, max_val),
                if all(map(_is_elem, obj.values())):
                    return idx_set
                if all(map(_is_array_type, obj.values())):
                    idx_sets = list(map(_index_set, obj.values()))
                    # all children index-sets must be identical
                    if idx_sets[1:] == idx_sets[:-1]:
                        return idx_set + idx_sets[0]
    raise ValueError(
        'The input object is not a proper array: {}'.format(repr(obj)), obj
    )


def _flatten_array(arr, lvl):
    if _is_dict(arr):
        arr_it = arr.values()
    else:
        arr_it = arr

    if lvl == 1:
        return arr_it

    flat_arr = []
    for sub_arr in arr_it:
        flat_arr.extend(_flatten_array(sub_arr, lvl - 1))
    return flat_arr


def _dzn_val(val):
    if isinstance(val, bool):
        return 'true' if val else 'false'
    if isinstance(val, Enum):
        return val.name
    if isinstance(val, set) and len(val) == 0:
        return '{}'
    return str(val)


def _dzn_set(s):
    if s and _is_int_set(s):
        min_val, max_val = _extremes(s)
        if _is_contiguous(s, min_val, max_val):
            return '{}..{}'.format(min_val, max_val)  # contiguous set
    return '{{{}}}'.format(', '.join(map(_dzn_val, s)))


def _index_set_str(idx_set):
    return ', '.join(['{}..{}'.format(*s) for s in idx_set])


def _dzn_array_nd(arr):
    idx_set = _index_set(arr)
    dim = max([len(idx_set), 1])
    if dim > 6:  # max 6-dimensional array in dzn language
        raise ValueError((
            'The input array has {} dimensions. Minizinc supports arrays of '
            'up to 6 dimensions.'
        ).format(dim), arr)

    if _is_dict(arr):
        arr_it = arr.values()
    else:
        arr_it = arr
{'fraction_non_alphanumeric': 0.12029887920298879, 'fraction_numerical': 0.00560398505603985, 'mean_word_length': 2.461637931034483, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '1503135', 'n_tokens_mistral': 2828, 'n_tokens_neox': 2732, 'n_words': 709}