commons.py 文件源码

python
阅读 25 收藏 0 点赞 0 评论 0

项目:graffunc 作者: Aluriak 项目源码 文件源码
def call_converter(converter:callable, predecessors:dict) -> dict:
    """Return the result obtained by calling the converter with the
    given predecessors {type: value} as input.
    """
    argspec = inspect.getfullargspec(converter)
    all_args = tuple(argspec.args + argspec.kwonlyargs)
    nb_args = len(all_args)

    # if number of (positional) args equals the number of predecessor
    if (nb_args == len(predecessors) == 1) or (len(argspec.args) == len(predecessors) == 1):
        params = {all_args[0]: next(iter(predecessors.values()))}
    else:  # let's use args name
        params = {arg: predecessors[arg] for arg in all_args if arg in predecessors}
        if len(params) < len(predecessors):  # let's use annotations
            # map  predecessor type -> arg, inferred from annotations, minus already matching args
            matching_annotations = {argspec.annotations[arg]: arg for arg in all_args
                                    if argspec.annotations.get(arg) in predecessors
                                    and arg not in params}
            params.update({
                arg: predecessors[pred]
                for pred, arg in matching_annotations.items()
            })
    return converter(**params)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号