mirror of
https://github.com/wassname/scikit-image.git
synced 2026-07-24 13:20:43 +08:00
Add utility to rename functions
This commit is contained in:
@@ -0,0 +1,21 @@
|
||||
from skimage._shared.utils import copy_func
|
||||
import numpy.testing as npt
|
||||
|
||||
|
||||
def test_copyfunc():
|
||||
def foo(a):
|
||||
return a
|
||||
|
||||
bar = copy_func(foo, name='bar')
|
||||
other = copy_func(foo)
|
||||
|
||||
npt.assert_equal(bar.__name__, 'bar')
|
||||
npt.assert_equal(other.__name__, 'foo')
|
||||
|
||||
other.__name__ = 'other'
|
||||
|
||||
npt.assert_equal(foo.__name__, 'foo')
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
npt.run_module_suite()
|
||||
@@ -2,6 +2,7 @@ import warnings
|
||||
import functools
|
||||
import sys
|
||||
import numpy as np
|
||||
import types
|
||||
|
||||
import six
|
||||
|
||||
@@ -174,3 +175,19 @@ def _mode_deprecations(mode):
|
||||
"removed in a future release."))
|
||||
mode = 'edge'
|
||||
return mode
|
||||
|
||||
|
||||
def copy_func(f, name=None):
|
||||
"""Create a copy of a function.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
f : function
|
||||
Function to copy.
|
||||
name : str, optional
|
||||
Name of new function.
|
||||
|
||||
"""
|
||||
return types.FunctionType(six.get_function_code(f),
|
||||
six.get_function_globals(f), name or f.__name__,
|
||||
six.get_function_defaults(f), six.get_function_closure(f))
|
||||
|
||||
Reference in New Issue
Block a user