<commit_before>from func_prototypes.util import dictjoin
from functools import partial, wraps
from inspect import getargspec

"""
Adapts args, kwargs into **kwargs only with help of function prototype.
Prototype should be a list of arg names.
"""
def to_kwargs(func, args, kwargs):
  fname = func.__name__
  spec = getargspec(func)
  if spec.varargs is not None or spec.keywords is not None:
    raise TypeError( "Cannot convert arguments for function %s because it uses args or kwargs." % fname)
  prototype = spec.args
  out = kwargs.copy()
  if len(prototype) < len(args):
    raise TypeError("%s takes at most %d arguments (%s given)" % (fname, len(prototype), len(args)))
  for name, value in zip(prototype, args):
    if name in out:
      raise TypeError("%s got multiple values for keyword argument '%s'" % (fname, name))
    out[name] = value
  return out

"""Function which adapts arguments for function foo using convert"""
def adapt(foo, convert, context=None):
  @wraps(foo)
  def wrapped(*args, **kwargs):
    kwargs = to_kwargs(foo, args, kwargs)
    new_kwargs = convert(foo.__name__, kwargs, context)
    return foo(**new_kwargs)
  return wrapped<commit_msg>Upgrade to python3 specification functions.<commit_after>from func_prototypes.util import dictjoin
from functools import partial, wraps
try:
    from inspect import getfullargspec as inspector
except ImportError:
    from inspect import getargspec as inspector # Python 2 fallback.


"""
Adapts args, kwargs into **kwargs only with help of function prototype.
Prototype should be a list of arg names.
"""
def to_kwargs(func, args, kwargs):
  fname = func.__name__
  spec = inspector(func)
  # spec 2 is kerwords for getargspec or varkw for getfullargspec
  if spec.varargs is not None or spec[2] is not None:
    raise TypeError( "Cannot convert arguments for function %s because it uses args or kwargs." % fname)
  prototype = spec.args
  out = kwargs.copy()
  if len(prototype) < len(args):
    raise TypeError("%s takes at most %d arguments (%s given)" % (fname, len(prototype), len(args)))
  for name, value in zip(prototype, args):
    if name in out:
      raise TypeError("%s got multiple values for keyword argument '%s'" % (fname, name))
    out[name] = value
  return out

"""Function which adapts arguments for function foo using convert"""
def adapt(foo, convert, context=None):
  @wraps(foo)
  def wrapped(*args, **kwargs):
    kwargs = to_kwargs(foo, args, kwargs)
    new_kwargs = convert(foo.__name__, kwargs, context)
    return foo(**new_kwargs)
  return wrapped
{'fraction_non_alphanumeric': 0.09980430528375733, 'fraction_numerical': 0.0015655577299412916, 'mean_word_length': 4.336116910229645, 'pattern_counts': {'":': 0, '<': 5, '<?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': '488867', 'n_tokens_mistral': 749, 'n_tokens_neox': 746, 'n_words': 305}