Skip to content

moduleutils

Utility fuctions to support smart modules.

Functions

copy_func(func, func_name, doc_string=None, arg_list=None)

Copy a function object to create a new function.

This is used inside smart modules that auto-generate functions based on context.

Parameters:

Name Type Description Default
func object

python source function object

required
func_name str

new function name

required
doc_string str

new function docstring

None
arg_list list

list of default values for function arguments

None

Returns:

Type Description
object

new python function objects

Source code in pyrevitlib/pyrevit/coreutils/moduleutils.py
def copy_func(func, func_name, doc_string=None, arg_list=None):
    """Copy a function object to create a new function.

    This is used inside smart modules that auto-generate functions based on
    context.

    Args:
        func (object): python source function object
        func_name (str): new function name
        doc_string (str): new function docstring
        arg_list (list): list of default values for function arguments

    Returns:
        (object): new python function objects
    """
    new_func = types.FunctionType(func.func_code, func.func_globals,
                                  func_name, tuple(arg_list), func.func_closure)
    new_func.__doc__ = doc_string
    return new_func

mark(prop_name)

Decorator function to add a marker property to the given type.

Source code in pyrevitlib/pyrevit/coreutils/moduleutils.py
def mark(prop_name):
    """Decorator function to add a marker property to the given type."""
    def setter_decorator(type_obj):
        setattr(type_obj, prop_name, True)
        return type_obj
    return setter_decorator

collect_marked(module_obj, prop_name)

Collect module objects that are marked with given property.

Source code in pyrevitlib/pyrevit/coreutils/moduleutils.py
def collect_marked(module_obj, prop_name):
    """Collect module objects that are marked with given property."""
    marked_objs = []
    for member in inspect.getmembers(module_obj):
        _, type_obj = member
        if (inspect.isclass(type_obj) or inspect.isfunction(type_obj)) \
                and getattr(type_obj, prop_name, False):
            marked_objs.append(type_obj)
    return marked_objs

has_argument(function_obj, arg_name)

Check if given function object has argument matching arg_name.

Source code in pyrevitlib/pyrevit/coreutils/moduleutils.py
def has_argument(function_obj, arg_name):
    """Check if given function object has argument matching arg_name."""
    return arg_name in inspect.getargspec(function_obj)[0] #pylint: disable=deprecated-method

has_any_arguments(function_obj, arg_name_list)

Check if given function object has any of given arguments.

Source code in pyrevitlib/pyrevit/coreutils/moduleutils.py
def has_any_arguments(function_obj, arg_name_list):
    """Check if given function object has any of given arguments."""
    args = inspect.getargspec(function_obj)[0] #pylint: disable=deprecated-method
    if arg_name_list:
        return any(x in args for x in arg_name_list)
    return False

filter_kwargs(function_obj, kwargs)

Filter given arguments dict for function_obj arguments.

Source code in pyrevitlib/pyrevit/coreutils/moduleutils.py
def filter_kwargs(function_obj, kwargs):
    """Filter given arguments dict for function_obj arguments."""
    filtered_kwargs = {}
    for arg_name in inspect.getargspec(function_obj)[0]: #pylint: disable=deprecated-method
        filtered_kwargs[arg_name] = kwargs.get(arg_name, None)
    return filtered_kwargs