code: def create_kwargs(cls, params, **extras):
      """
      Given some class, a `Params` object, and potentially other keyword arguments,
      create a dict of keyword args suitable for passing to the class's constructor.
  
      The function does this by finding the class's constructor, matching the constructor
      arguments to entries in the `params` object, and instantiating values for the parameters
      using the type annotation and possibly a from_params method.
  
      Any values that are provided in the `extras` will just be used as is.
      For instance, you might provide an existing `Vocabulary` this way.
      """
      # Get the signature of the constructor.
      signature = inspect.signature(cls.__init__)
      kwargs = {}
  
      # Iterate over all the constructor parameters and their annotations.
      for name, param in list(signature.parameters.items()):
          # Skip "self". You're not *required* to call the first parameter "self",
          # so in theory this logic is fragile, but if you don't call the self parameter
          # "self" you kind of deserve what happens.
          if name == "self":
              continue
  
          # If the annotation is a compound type like typing.Dict[str, int],
          # it will have an __origin__ field indicating `typing.Dict`
          # and an __args__ field indicating `(str, int)`. We capture both.
          annotation = remove_optional(param.annotation)
          origin = getattr(annotation, "__origin__", None)
          args = getattr(annotation, "__args__", [])
  
          # The parameter is optional if its default value is not the "no default" sentinel.
          default = param.default
          optional = default != _NO_DEFAULT
  
          # Some constructors expect extra non-parameter items, e.g. vocab: Vocabulary.
          # We check the provided `extras` for these and just use them if they exist.
          if name in extras:
              kwargs[name] = extras[name]
  
          # The next case is when the parameter type is itself constructible from_params.
          elif hasattr(annotation, "from_params"):
              if name in params:
                  # Our params have an entry for this, so we use that.
                  subparams = params.pop(name)
  
                  if takes_arg(annotation.from_params, "extras"):
                      # If annotation.params accepts **extras, we need to pass them all along.
                      # For example, `BasicTextFieldEmbedder.from_params` requires a Vocabulary
                      # object, but `TextFieldEmbedder.from_params` does not.
                      subextras = extras
                  else:
                      # Otherwise, only supply the ones that are actual args; any additional ones
                      # will cause a TypeError.
                      subextras = dict(
                          (k, v)
                          for k, v in list(extras.items())
                          if takes_arg(annotation.from_params, k)
                      )
  
                  # In some cases we allow a string instead of a param dict, so
                  # we need to handle that case separately.
                  if isinstance(subparams, unicode):
                      kwargs[name] = annotation.by_name(subparams)()
                  else:
                      print(annotation)
                      kwargs[name] = annotation.from_params(
                          params=subparams, **subextras
                      )
              elif not optional:
                  # Not optional and not supplied, that's an error!
                  raise ConfigurationError(
                      "expected key {name} for {cls.__name__}"
                  )
              else:
                  kwargs[name] = default
  
          # If the parameter type is a Python primitive, just pop it off
          # using the correct casting pop_xyz operation.
          elif annotation == unicode:
              kwargs[name] = (
                  params.pop(name, default)
                  if optional
                  else params.pop(name)
              )
          elif annotation == int:
              kwargs[name] = (
                  params.pop_int(name, default)
                  if optional
                  else params.pop_int(name)
              )
          elif annotation == bool:
              kwargs[name] = (
                  params.pop_bool(name, default)
                  if optional
                  else params.pop_bool(name)
              )
          elif annotation == float:
              kwargs[name] = (
                  params.pop_float(name, default)
                  if optional
                  else params.pop_float(name)
              )
  
          # This is special logic for handling types like Dict[str, TokenIndexer], which it creates by
          # instantiating each value from_params and returning the resulting dict.
          elif (
              origin in (Dict, dict)
              and len(args) == 2
              and hasattr(args[-1], "from_params")
          ):
              value_cls = annotation.__args__[-1]
  
              value_dict = {}
  
              for key, value_params in list(
                  params.pop(name, Params({})).items()
              ):
                  value_dict[key] = value_cls.from_params(
                      params=value_params, **extras
                  )
  
              kwargs[name] = value_dict
  
          else:
              # Pass it on as is and hope for the best.
              if optional:
                  kwargs[name] = params.pop(name, default)
              else:
                  kwargs[name] = params.pop(name)
  
      params.assert_empty(cls.__name__)
      return kwargs
  
file_path: magnitude/pymagnitude/third_party/allennlp/common/from_params.py
imports: from __future__ import absolute_import
  from __future__ import print_function
  import inspect
  import logging
  from allennlp.common.checks import ConfigurationError
  from allennlp.common.params import Params
label_desc: Use image node_id 3 for calling a global function with example usage: create_kwargs(cls, params) and returns: kwargs