set_expr.py 文件源码

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

项目:redbiom 作者: biocore 项目源码 文件源码
def seteval(str_, get=None, stemmer=None, target=None):
    """Evaluate a set operation string, where each Name is fetched

    Parameters
    ----------
    str_ : str
        The query to evaluate
    get : function, optional
        A getting method, defaults to instatiating one from _requests
    stemmer : function, optional
        A method to stem a query Name. If None, defaults to passthrough.
    target : str, optional
        A subcontext to query against. If None, defaults to text-search.
    """
    if get is None:
        import redbiom
        config = redbiom.get_config()
        get = redbiom._requests.make_get(config)

    if stemmer is None:
        stemmer = passthrough

    if target is None:
        target = 'text-search'

    # Load is subject to indirection to simplify testing
    globals()['Load'] = make_Load(get)

    # this seems right now to be the easiest way to inject parameters
    # into Name
    globals()['stemmer'] = stemmer
    globals()['target'] = target

    formed = ast.parse(str_, mode='eval')

    node_types = (ast.BitAnd, ast.BitOr, ast.BitXor, ast.Name, ast.Sub,
                  ast.Expression, ast.BinOp, ast.Load)

    for node in ast.walk(formed):
        if not isinstance(node, node_types):
            raise TypeError("Unsupported node type: %s" % ast.dump(node))

    result = eval(ast.dump(formed))

    # clean up
    global Load
    del Load
    del stemmer
    del target

    return result
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号