Esempio n. 1
0
def build_binop_factory(op):
    src = 'lambda x, y: x %s y'
    if op in ('in', 'not in'):
        src = 'lambda x, y: x.%s(y)'
    op = binops_mappig.get(op, op)

    return eval_src(src % op)
Esempio n. 2
0
def build_binop_factory(op):
    src = 'lambda x, y: x %s y'
    if op == 'in':
        src = 'lambda x, y: x.%s(y) if hasattr(x, "in_") else y.contains_(x)'
    elif op == 'not in':
        src = 'lambda x, y: x.%s(y) if hasattr(x, "not_in_") else y.not_contains_(x)'

    op = binops_mapping.get(op, op)

    return eval_src(src % op)
Esempio n. 3
0
def transform_func(func):
    if not isinstance(func, types.FunctionType):
        return func

    res = getattr(func, CK_TRANS_RES, None)
    if res is not None:
        return res

    ast, _, _ = decompile(func)
    FuncTranslator(ast)

    argspec = inspect.getargspec(func)
    args = argspec.args
    defaults = argspec.defaults or []
    l = len(defaults)
    if l != 0:
        args = args[: -l]
        defaults = zip(argspec.args[-l:], defaults)
    varargs = argspec.varargs
    keywords = argspec.keywords
    arg_str = ', '.join(filter(None, [
        ', '.join(args),
        ', '.join(
            '{}={}'.format(k, repr(v))
            for k, v in defaults
        ),
        '*%s' % varargs if varargs else '',
        '**%s' % keywords if keywords else '',
    ]))

    src = 'lambda {}: {}'.format(
        arg_str,
        ast.src
    )

    globals = func.func_globals
    if func.func_closure:
        globals = dict(globals, **dict(
            izip(
                func.func_code.co_freevars,
                (c.cell_contents for c in func.func_closure)
            )
        ))
    res = eval_src(src, globals=globals)
    setattr(func, CK_TRANS_RES, res)
    return res
Esempio n. 4
0
 def get_query(self):
     src = PATTERN_SRC.sub(
         '{}'.format(self.key),
         ast2src(self.tree)
     )
     return eval_src(src, self.globals, self.locals)