Пример #1
0
def test_def_():
    code = Code()
    with code.def_('foo', ['x', 'y']):
        code.w('return x+y')
    #
    code.compile()
    foo = code['foo']
    assert foo(1, 2) == 3
Пример #2
0
def test_def__default_args():
    code = Code()
    with code.def_('foo', ['x', ('y', 3)]):
        code.w('return x+y')
    #
    code.compile()
    foo = code['foo']
    assert foo(1) == 4
    assert foo(1, 10) == 11
Пример #3
0
def test_cdef_var():
    code = Code()
    with code.def_('foo', ['x', 'y']):
        code.cdef_var('long', 'foo')
        code.cdef_var('int', 'bar', 42)
        code.w('return')
    src = code.build()
    assert src == ("def foo(x, y):\n" "    return")
    #
    code = Code(pyx=True)
    with code.def_('foo', ['x', 'y']):
        code.cdef_var('long', 'foo')
        code.cdef_var('int', 'bar', 42)
        code.w('return')
    src = code.build()
    assert src == ("def foo(object x, object y):\n"
                   "    cdef long foo\n"
                   "    cdef int bar = 42\n"
                   "    return")
Пример #4
0
def make_wrappers():
    from pypytools.codegen import Code
    code = Code()
    names = [
        'all', 'any', 'argmax', 'argmin', 'argsort', 'choose', 'clip', 'copy',
        'cumprod', 'cumsum', 'diagonal', 'max', 'min', 'nonzero', 'prod',
        'ptp', 'put', 'ravel', 'repeat', 'reshape', 'round', 'squeeze', 'sum',
        'swapaxes', 'transpose'
    ]
    #
    for name in names:
        ns = code.new_scope(name=name)
        with code.def_(name, ['myarray'], '*args', '**kwargs'):
            ns.w('return myarray.{name}(*args, **kwargs)')
    #
    code.compile()
    gbl = globals()
    for name in names:
        gbl[name] = code[name]