<commit_before>r""" Lists with inter-element operations

In certain cases it may be advantegeous to make operations on list elements rather than doing
list extensions.

This sub-module implements a list which allows to make operations with it-self or with scalars.
"""

from functools import wraps
import operator as op

from ._internal import set_module
from ._help import isiterable


__all__ = ['oplist']


def _crt_op(op, only_self=False):
    if only_self:
        def _(self):
            return self.__class__(op(data) for data in self)
    else:
        def _(self, other):
            if isiterable(other):
                n = len(self)
                if n != len(other):
                    raise ValueError(f"{self.__class__.__name__} requires other data to contain same number of elements (or a scalar).")
                return self.__class__(op(self[i], other[i]) for i in range(n))
            return self.__class__(op(data, other) for data in self)
    return _


def _crt_rop(op):
    def _(self, other):
        if isiterable(other):
            n = len(self)
            if n != len(other):
                raise ValueError(f"{self.__class__.__name__} requires other data to contain same number of elements (or a scalar).")
            return self.__class__(op(other[i], self[i]) for i in range(n))
        return self.__class__(op(other, data) for data in self)
    return _


def _crt_iop(op, only_self=False):
    if only_self:
        def _(self):
            for data in self:
                op(data)
    else:
        def _(self, other):
            if isiterable(other):
                n = len(self)
                if n != len(other):
                    raise ValueError(f"{self.__class__.__name__} requires other data to contain same number of elements (or a scalar).")
                for i in range(n):
                    op(self[i], other[i])
            else:
                for data in self:
                    op(data, other)
    return _


@set_module("sisl")
class oplist(list):
    """ list with element-wise operations
<commit_msg>maint: cleaned a bit the operations in oplist<commit_after>r""" Lists with inter-element operations

In certain cases it may be advantegeous to make operations on list elements rather than doing
list extensions.

This sub-module implements a list which allows to make operations with it-self or with scalars.
"""

from functools import wraps
import operator as op

from ._internal import set_module
from ._help import isiterable


__all__ = ['oplist']


def _crt_op(op, only_self=False):
    if only_self:
        def _(self):
            return self.__class__(op(s) for s in self)
    else:
        def _(self, other):
            if isiterable(other):
                if len(self) != len(other):
                    raise ValueError(f"{self.__class__.__name__} requires other data to contain same number of elements (or a scalar).")
                return self.__class__(op(s, o) for s, o in zip(self, other))
            return self.__class__(op(s, other) for s in self)
    return _


def _crt_rop(op):
    def _(self, other):
        if isiterable(other):
            if len(self) != len(other):
                raise ValueError(f"{self.__class__.__name__} requires other data to contain same number of elements (or a scalar).")
            return self.__class__(op(o, s) for s, o in zip(self, other))
        return self.__class__(op(other, s) for s in self)
    return _


def _crt_iop(op, only_self=False):
    if only_self:
        def _(self):
            for data in self:
                op(data)
    else:
        def _(self, other):
            if isiterable(other):
                if len(self) != len(other):
                    raise ValueError(f"{self.__class__.__name__} requires other data to contain same number of elements (or a scalar).")
                for s, o in zip(self, other):
                    op(s, o)
            else:
                for s in self:
                    op(s, other)
    return _


@set_module("sisl")
class oplist(list):
    """ list with element-wise operations

{'fraction_non_alphanumeric': 0.09237536656891496, 'fraction_numerical': 0.0, 'mean_word_length': 2.1363984674329504, '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': '4960456', 'n_tokens_mistral': 1185, 'n_tokens_neox': 1150, 'n_words': 423}