示例#1
0
def cast_bound_arguments_to_real(bound_arguments: BoundArguments,
                                 signature: Signature):
    """Cast the requested parameters to simple types.

    Args:
        bound_arguments: Incoming parameters.
        signature: Signature of method.
    """
    # loop all arguments
    for key, value in bound_arguments.arguments.items():
        # get type of parameter
        annotation = signature.parameters[key].annotation

        # special cases
        if value is None or annotation == Parameter.empty or annotation == Any:
            # if value is None or no annotation is given, just copy it
            bound_arguments.arguments[key] = value
        elif annotation == Enum:
            # cast to enum
            bound_arguments.arguments[
                key] = value if annotation == Parameter.empty else annotation(
                    value)
        elif annotation == str:
            # unescape strings
            bound_arguments.arguments[key] = xml.sax.saxutils.unescape(value)
        else:
            # cast to type
            bound_arguments.arguments[key] = annotation(value)
示例#2
0
def signature_key(bound_args: inspect.BoundArguments,
                  prefix: str,
                  only: Optional[Iterable] = None,
                  exclude: Optional[Iterable] = None,
                  **generate_kwargs) -> str:
    """Generate a cache key based on function arguments for use with
    Redis of the form:

        '<prefix><sep><hashed_args>'

    :param bound_args: function signature bounded arguments
    :param prefix: unhashed prefix of the cache key
    :param only: optional whitelist of arguments to include in cache key
    :param exclude: optional blacklist of arguments to exclude in cache key
    :param generate_kwargs: keyword arguments passed to ``generate_key``
    """
    only = None if only is None else set(only)
    exclude = None if exclude is None else set(exclude)

    bound_args.apply_defaults()
    objects = (obj for argname, value in bound_args.arguments.items()
               for obj in [argname, value]
               if (only is None or argname in only) and (
                   exclude is None or argname not in exclude))
    return generate_key(prefix, objects, **generate_kwargs)
示例#3
0
def cast_bound_arguments_to_simple(bound_arguments: BoundArguments):
    """Cast the requested parameters, which are of simple types, to the types required by the method.

    Args:
        bound_arguments: Incoming parameters.
    """
    # loop all arguments
    for key, value in bound_arguments.arguments.items():
        # special cases
        if isinstance(value, str):
            # escape strings
            bound_arguments.arguments[key] = xml.sax.saxutils.escape(value)
        elif isinstance(value, Enum):
            # get value of enum
            bound_arguments.arguments[key] = value.value
示例#4
0
    def compile_model(
        context: keras.TFKerasContext,
        compile_args: inspect.BoundArguments,
        env: det.EnvContext,
        hvd_config: horovod.HorovodContext,
    ) -> None:
        context.model = keras._get_multi_gpu_model_if_using_native_parallel(
            pre_compiled_model=context.model,
            env=env,
            hvd_config=hvd_config,
        )

        if "optimizer" in compile_args.arguments:
            # For backwards compatibility we check if an optimizer is passed as part
            # of the compile call. If `wrap_optimizer()` is used, we will ignore this
            # this optimizer.
            compile_args.arguments[
                "optimizer"] = context._process_optimizer_from_compile(
                    compile_args.arguments["optimizer"])

        if hvd_config.use and version.parse("2.0.0") <= version.parse(
                tf.__version__) < version.parse("2.2.0"):
            logging.info(
                "Calling `model.compile(...)` with `experimental_run_tf_function=False` to ensure "
                "TensorFlow calls `optimizer.get_gradients()` to compute gradients."
            )

            context.model.compile(*compile_args.args,
                                  **compile_args.kwargs,
                                  experimental_run_tf_function=False)
        else:
            context.model.compile(*compile_args.args, **compile_args.kwargs)
示例#5
0
    def compile_model(
        context: keras.TFKerasContext,
        compile_args: inspect.BoundArguments,
        env: det.EnvContext,
        hvd_config: horovod.HorovodContext,
    ) -> None:
        if "optimizer" in compile_args.arguments:
            # For backwards compatibility we check if an optimizer is passed as part
            # of the compile call. If `wrap_optimizer()` is used, we will ignore this
            # this optimizer.
            compile_args.arguments[
                "optimizer"] = context._process_optimizer_from_compile(
                    compile_args.arguments["optimizer"])

        # context.model is Optional[Model]. This assert signals to mypy it can't
        # be none because we check that in `from_trial`.
        assert context.model is not None

        if hvd_config.use and version.parse("2.0.0") <= version.parse(
                tf.__version__) < version.parse("2.2.0"):
            logging.info(
                "Calling `model.compile(...)` with `experimental_run_tf_function=False` to ensure "
                "TensorFlow calls `optimizer.get_gradients()` to compute gradients."
            )

            context.model.compile(*compile_args.args,
                                  **compile_args.kwargs,
                                  experimental_run_tf_function=False)
        else:
            context.model.compile(*compile_args.args, **compile_args.kwargs)
示例#6
0
def recreate_system(
    ba: inspect.BoundArguments,
    particles: Particles,
    converter: (nbody_system.nbody_to_si, None) = None,
):
    """Reinitialize a System made with `initialize_system`.

    Parameters
    ----------
    ba : `inspect.BoundArguments`
    particles : `Particles`
        should have everything, including for evolution
    converter : `generic_unit_converter`
        needed if 'converter' not in `ba.kwargs`
        if provided, will overwrite built-in, if exists

    Notes
    -----
    A good way to do this is to have `particles` be the set with all the
    up-to-date information. Then there is no need to pass `evolution` or
    `gravity`.

    """
    # change number_of_particles arg to the `particles` set
    # this will then initialize the system off the existing particles
    # skipping the IMF and distribution function steps
    ba = copy.copy(ba)

    ba.arguments["number_of_particles"] = particles

    if converter is not None:
        ba.arguments["converter"] = converter

    # make the system
    system = initialize_system(*ba.args, store_inputs=False, **ba.kwargs)

    return system
示例#7
0
    def _coerce_arguments(
            func: Callable[..., Any],
            ba: inspect.BoundArguments) -> inspect.BoundArguments:
        args = OrderedDict()
        annotations = {}
        if hasattr(func, "__annotations__"):
            annotations = func.__annotations__

        for key, val in ba.arguments.items():
            annotation = annotations.get(key, None)
            if annotation is not None and annotation == Millisatoshi:
                args[key] = Millisatoshi(val)
            else:
                args[key] = val
        ba.arguments = args
        return ba
示例#8
0
def _bind(thesignature, args, kwargs, *, partial):
    """Private method. Don't use directly."""

    arguments = OrderedDict()

    parameters = iter(thesignature.parameters.values())
    parameters_ex = ()
    arg_vals = iter(args)

    # These are added for `unpythonic`.
    unbound_parameters = []
    extra_args = []
    extra_kwargs = OrderedDict()
    kwargs = copy.copy(kwargs)  # the caller might need the original later

    while True:
        # Let's iterate through the positional arguments and corresponding
        # parameters
        try:
            arg_val = next(arg_vals)
        except StopIteration:
            # No more positional arguments
            try:
                param = next(parameters)
            except StopIteration:
                # No more parameters. That's it. Just need to check that
                # we have no `kwargs` after this while loop
                break
            else:
                if param.kind == Parameter.VAR_POSITIONAL:
                    # That's OK, just empty *args.  Let's start parsing
                    # kwargs
                    break
                elif param.name in kwargs:
                    if param.kind == Parameter.POSITIONAL_ONLY:
                        msg = '{arg!r} parameter is positional only, ' \
                              'but was passed as a keyword'
                        msg = msg.format(arg=param.name)
                        raise TypeError(msg) from None
                    parameters_ex = (param, )
                    break
                elif (param.kind == Parameter.VAR_KEYWORD
                      or param.default is not _empty):
                    # That's fine too - we have a default value for this
                    # parameter.  So, lets start parsing `kwargs`, starting
                    # with the current parameter
                    parameters_ex = (param, )
                    break
                else:
                    # No default, not VAR_KEYWORD, not VAR_POSITIONAL,
                    # not in `kwargs`
                    if partial:
                        parameters_ex = (param, )
                        break
                    else:
                        # msg = 'missing a required argument: {arg!r}'
                        # msg = msg.format(arg=param.name)
                        # raise TypeError(msg) from None
                        unbound_parameters.append(param)
        else:
            # We have a positional argument to process
            try:
                param = next(parameters)
            except StopIteration:
                # raise TypeError('too many positional arguments') from None
                extra_args.append(arg_val)
            else:
                if param.kind in (Parameter.VAR_KEYWORD,
                                  Parameter.KEYWORD_ONLY):
                    # Looks like we have no parameter for this positional
                    # argument
                    # raise TypeError(
                    #     'too many positional arguments') from None
                    extra_args.append(arg_val)

                if param.kind == Parameter.VAR_POSITIONAL:
                    # We have an '*args'-like argument, let's fill it with
                    # all positional arguments we have left and move on to
                    # the next phase
                    values = [arg_val]
                    values.extend(arg_vals)
                    arguments[param.name] = tuple(values)
                    break

                if param.name in kwargs and param.kind != Parameter.POSITIONAL_ONLY:
                    raise TypeError(
                        'multiple values for argument {arg!r}'.format(
                            arg=param.name)) from None

                arguments[param.name] = arg_val

    # Now, we iterate through the remaining parameters to process
    # keyword arguments
    kwargs_param = None
    for param in itertools.chain(parameters_ex, parameters):
        if param.kind == Parameter.VAR_KEYWORD:
            # Memorize that we have a '**kwargs'-like parameter
            kwargs_param = param
            continue

        if param.kind == Parameter.VAR_POSITIONAL:
            # Named arguments don't refer to '*args'-like parameters.
            # We only arrive here if the positional arguments ended
            # before reaching the last parameter before *args.
            continue

        param_name = param.name
        try:
            arg_val = kwargs.pop(param_name)
        except KeyError:
            # We have no value for this parameter.  It's fine though,
            # if it has a default value, or it is an '*args'-like
            # parameter, left alone by the processing of positional
            # arguments.
            if (not partial and param.kind != Parameter.VAR_POSITIONAL
                    and param.default is _empty):
                # raise TypeError('missing a required argument: {arg!r}'.
                #                 format(arg=param_name)) from None
                unbound_parameters.append(param)

        else:
            if param.kind == Parameter.POSITIONAL_ONLY:
                # This should never happen in case of a properly built
                # Signature object (but let's have this check here
                # to ensure correct behaviour just in case)
                raise TypeError(
                    '{arg!r} parameter is positional only, '
                    'but was passed as a keyword'.format(arg=param.name))

            arguments[param_name] = arg_val

    if kwargs:
        if kwargs_param is not None:
            # Process our '**kwargs'-like parameter
            arguments[kwargs_param.name] = kwargs
        else:
            # raise TypeError(
            #     'got an unexpected keyword argument {arg!r}'.format(
            #         arg=next(iter(kwargs))))
            extra_kwargs.update(kwargs)

    return (BoundArguments(thesignature, arguments), tuple(unbound_parameters),
            (tuple(extra_args), extra_kwargs))