Exemple #1
0
 def format_cell(cell):
     if ut.is_funclike(cell):
         header = '# ' + ut.to_title_caps(ut.get_funcname(cell))
         code = (header, ut.get_func_sourcecode(cell, stripdef=True, stripret=True))
     else:
         code = (None, cell)
     return generate_notebook.format_cells(code)
Exemple #2
0
 def format_cell(cell):
     if ut.is_funclike(cell):
         header = '# ' + ut.to_title_caps(ut.get_funcname(cell))
         code = (header,
                 ut.get_func_sourcecode(cell, stripdef=True, stripret=True))
     else:
         code = (None, cell)
     return generate_notebook.format_cells(code)
Exemple #3
0
def test_decorator_module():
    import utool as ut
    import decorator
    ut.rrrr()

    def testdecor(func):
        @ut.on_exception_report_input
        @ut.accepts_scalar_input2([0])
        @ut.ignores_exc_tb
        def testwrp(*args, **kwargs):
            print('was wrapped')
            return func(*args, **kwargs)

        return testwrp

    preserving_testdecor = decorator.decorator(testdecor)

    def myfunction(self, listinput_, arg1, *args, **kwargs):
        " just a test function "
        return [x + 1 for x in listinput_]

    wrapper = testdecor(myfunction)
    orig_func = myfunction

    _wrp_preserve0 = preserving_testdecor(myfunction)
    _wrp_preserve1 = ut.preserve_sig(wrapper, orig_func, True)
    _wrp_preserve2 = ut.preserve_sig(wrapper, orig_func, False)

    print('___')
    print(ut.get_func_sourcecode(_wrp_preserve0))
    print('___')
    print(ut.get_func_sourcecode(_wrp_preserve1))
    print('___')
    print(ut.get_func_sourcecode(_wrp_preserve2))
    print('___')

    print('---')
    print(ut.get_docstr(_wrp_preserve0))
    print('---')
    print(ut.get_docstr(_wrp_preserve1))
    print('---')
    print(ut.get_docstr(_wrp_preserve2))
    print('---')

    print(ut.dict_str(_wrp_preserve2._utinfo))
Exemple #4
0
def test_decorator_module():
    import utool as ut
    import decorator
    ut.rrrr()
    def testdecor(func):
        @ut.on_exception_report_input
        @ut.accepts_scalar_input2([0])
        @ut.ignores_exc_tb
        def testwrp(*args, **kwargs):
            print('was wrapped')
            return func(*args, **kwargs)
        return testwrp

    preserving_testdecor = decorator.decorator(testdecor)

    def myfunction(self, listinput_, arg1, *args, **kwargs):
        " just a test function "
        return [x + 1 for x in listinput_]

    wrapper = testdecor(myfunction)
    orig_func = myfunction

    _wrp_preserve0 = preserving_testdecor(myfunction)
    _wrp_preserve1 = ut.preserve_sig(wrapper, orig_func, True)
    _wrp_preserve2 = ut.preserve_sig(wrapper, orig_func, False)

    print('___')
    print(ut.get_func_sourcecode(_wrp_preserve0))
    print('___')
    print(ut.get_func_sourcecode(_wrp_preserve1))
    print('___')
    print(ut.get_func_sourcecode(_wrp_preserve2))
    print('___')

    print('---')
    print(ut.get_docstr(_wrp_preserve0))
    print('---')
    print(ut.get_docstr(_wrp_preserve1))
    print('---')
    print(ut.get_docstr(_wrp_preserve2))
    print('---')

    print(ut.dict_str(_wrp_preserve2._utinfo))
Exemple #5
0
 def expand_closure_source(funcname, func):
     source = ut.get_func_sourcecode(func)
     closure_vars = [(k, v.cell_contents) for k, v in
                     zip(func.func_code.co_freevars, func.func_closure)]
     source = ut.unindent(source)
     import re
     for k, v in closure_vars:
         source = re.sub('\\b' + k + '\\b', ut.repr2(v), source)
     source = re.sub(r'def .*\(self', 'def ' + funcname + '(self', source)
     source = ut.indent(source.strip(), '    ') + '\n'
     return source
Exemple #6
0
# it to C, compiled the code and wrapped it with Python
fastfilter_2d = jit(double[:, :](double[:, :], double[:, :]))(filter2d)
autofilter_2d = autojit(filter2d)

# Use utool to time this
imports = ut.codeblock(r'''
    # STARTBLOCK
    import numpy as np
    from numba import double, jit, autojit
    # ENDBLOCK
    ''')

datas = ut.codeblock(r'''
    # STARTBLOCK
    fastfilter_2d = jit(double[:, :](double[:, :], double[:, :]))(filter2d)
    autofilter_2d = autojit(filter2d)
    rng = np.random.RandomState(0)
    image = rng.rand(100, 100)
    filt = rng.rand(10, 10)
    # ENDBLOCK
    ''')

setup = '\n'.join([imports, ut.get_func_sourcecode(filter2d), datas])
print(ut.highlight_code(setup))
stmt_list1 = ut.codeblock(r'''
    fastfilter_2d(image, filt)
    filter2d(image, filt)
    autofilter_2d(image, filt)
    ''').split('\n')
ut.util_dev.timeit_compare(stmt_list1, setup, int(1))
Exemple #7
0
# Use utool to time this
imports = ut.codeblock(
    r'''
    # STARTBLOCK
    import numpy as np
    from numba import double, jit, autojit
    # ENDBLOCK
    ''')

datas = ut.codeblock(
    r'''
    # STARTBLOCK
    fastfilter_2d = jit(double[:, :](double[:, :], double[:, :]))(filter2d)
    autofilter_2d = autojit(filter2d)
    rng = np.random.RandomState(0)
    image = rng.rand(100, 100)
    filt = rng.rand(10, 10)
    # ENDBLOCK
    ''')

setup =  '\n'.join([imports, ut.get_func_sourcecode(filter2d), datas])
print(ut.highlight_code(setup))
stmt_list1 = ut.codeblock(
    r'''
    fastfilter_2d(image, filt)
    filter2d(image, filt)
    autofilter_2d(image, filt)
    ''').split('\n')
ut.util_dev.timeit_compare(stmt_list1, setup, int(1))