def sg_inject(path, mod_name):
r"""Converts all functions in the given Python module to sugar functions
so that they can be used in a chainable manner.
Args:
path: A string. Path to the Python module
mod_name: A string. The name of the Python module to inject.
Returns:
None
"""
# import module
import sys
if path not in list(sys.path):
sys.path.append(path)
globals()[mod_name] = importlib.import_module(mod_name)
# find functions
for func_name in dir(globals()[mod_name]):
if isinstance(globals()[mod_name].__dict__.get(func_name), types.FunctionType):
if not func_name.startswith('_'):
# inject to tf.Variable type
exec('tf.Variable.%s = %s.%s' % (func_name, mod_name, func_name))
# inject to tf.Tensor type
exec('tf.Tensor.%s = %s.%s' % (func_name, mod_name, func_name))
评论列表
文章目录