<commit_before>"""Generic utility methods not part of main API.
"""

from functools import wraps
import inspect
import warnings

import pydash as pyd
from ._compat import iteritems


class _NoValue(object):
    """Represents an unset value. Used to differeniate between an explicit
    ``None`` and an unset value.
    """
    pass


#: Singleton object that differeniates between an explicit ``None`` value and
#: an unset value.
NoValue = _NoValue()


def call_callback(callback, *args):
    """Inspect argspec of `callback` function and only pass the supported
    arguments when calling it.
    """
    maxargs = len(args)
    argspec = None

    try:
        argspec = inspect.getargspec(callback)
    except TypeError:
        try:
            argspec = inspect.getargspec(getattr(callback, '__call__', None))
        except TypeError:  # pragma: no cover
            pass
    finally:
        if argspec:
            argcount = len(argspec.args)
        elif isinstance(callback, type):
            # Only pass single argument to type callbacks. This is for things
            # like int(), float(), str(), etc.
            argcount = 1
        else:  # pragma: no cover
            argcount = maxargs

    argstop = (maxargs if argspec and argspec.varargs
               else min([maxargs, argcount]))

    return callback(*args[:argstop])


def itercallback(collection, callback=None, reverse=False):
    """Return iterative callback based on collection type."""
    if isinstance(collection, dict):
        return iterdict_callback(collection, callback, reverse=reverse)
    else:
        return iterlist_callback(collection, callback, reverse=reverse)


def iterlist_callback(obj, callback=None, reverse=False):
    """Return iterative list callback."""
    cbk = pyd.iteratee(callback)<commit_msg>Prioritize argcount for types over argspec. Move varargs check to main logic tree.<commit_after>"""Generic utility methods not part of main API.
"""

from functools import wraps
import inspect
import warnings

import pydash as pyd
from ._compat import iteritems


class _NoValue(object):
    """Represents an unset value. Used to differeniate between an explicit
    ``None`` and an unset value.
    """
    pass


#: Singleton object that differeniates between an explicit ``None`` value and
#: an unset value.
NoValue = _NoValue()


def call_callback(callback, *args):
    """Inspect argspec of `callback` function and only pass the supported
    arguments when calling it.
    """
    maxargs = len(args)
    argspec = None

    try:
        argspec = inspect.getargspec(callback)
    except TypeError:
        try:
            argspec = inspect.getargspec(getattr(callback, '__call__', None))
        except TypeError:  # pragma: no cover
            pass
    finally:
        if isinstance(callback, type):
            # Only pass single argument to type callbacks. This is for things
            # like int(), float(), str(), etc.
            argcount = 1
        elif argspec and argspec.varargs:
            # Callback supports variable arguments.
            argcount = maxargs
        elif argspec:
            # Use inspected arg count.
            argcount = len(argspec.args)
        else:  # pragma: no cover
            argcount = maxargs

    argstop = min([maxargs, argcount])

    return callback(*args[:argstop])


def itercallback(collection, callback=None, reverse=False):
    """Return iterative callback based on collection type."""
    if isinstance(collection, dict):
        return iterdict_callback(collection, callback, reverse=reverse)
    else:
        return iterlist_callback(collection, callback, reverse=reverse)


def iterlist_callback(obj, callback=None, reverse=False):
    """Return iterative list callback."""
    cbk = pyd.iteratee(callback)
{'fraction_non_alphanumeric': 0.0891820580474934, 'fraction_numerical': 0.0005277044854881266, 'mean_word_length': 2.8882051282051284, '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': '1308608', 'n_tokens_mistral': 1025, 'n_tokens_neox': 1002, 'n_words': 359}