Decorator that will call the optimized c++ version
    of a pycast function if available rather than theo
    original pycast function

    :param function fn: original pycast function

    :return: return the wrapped function
    :rtype: function
194570
def optimized(fn):
    

    def _optimized(self, *args, **kwargs):
        """ This method calls the pycastC function if
        optimization is enabled and the pycastC function
        is available.

        :param: PyCastObject self: reference to the calling object.
                                    Needs to be passed to the pycastC function,
                                    so that all uts members are available.
        :param: list *args: list of arguments the function is called with.
        :param: dict **kwargs: dictionary of parameter  names and values the function has been called with.

        :return result of the function call either from pycast or pycastC module.
        :rtype: function
        """
        if self.optimizationEnabled:
            class_name = self.__class__.__name__
            module     = self.__module__.replace("pycast", "pycastC")
            try:
                imported = __import__("%s.%s" % (module, class_name), globals(), locals(), [fn.__name__])
                function = getattr(imported, fn.__name__)
                return function(self, *args, **kwargs)
            except ImportError:
                print  "[WARNING] Could not enable optimization for %s, %s" % (fn.__name__, self)
                return fn(self, *args, **kwargs)
        else:
            return fn(self, *args, **kwargs)

    setattr(_optimized, "__name__", fn.__name__)
    setattr(_optimized, "__repr__", fn.__repr__)
    setattr(_optimized, "__str__",  fn.__str__)
    setattr(_optimized, "__doc__",  fn.__doc__)

    return _optimized
1003662
def SpiceUDFUNS(f):
    

    @functools.wraps(f)
    def wrapping_udfuns(x, value):
        result = f(x)
        value[0] = c_double(result)

    return UDFUNS(wrapping_udfuns)
224902
def lazyfunc(func):
    
    closuremem_ = [{}]
    def wrapper(*args, **kwargs):
        mem = closuremem_[0]
        key = (repr(args), repr(kwargs))
        try:
            return mem[key]
        except KeyError:
            mem[key] = func(*args, **kwargs)
        return mem[key]
    return wrapper
759677
def scoped(self, func):
        
        @wraps(func)
        def wrapper(*args, **kwargs):
            scope = kwargs.get("scope", self.__factory__.default_scope)
            with self.scoped_to(scope):
                return func(*args, **kwargs)
        return wrapper
447092
def wraps(
    wrapped, assigned=functools.WRAPPER_ASSIGNMENTS, updated=functools.WRAPPER_UPDATES
):
    """"""
    if not is_cython_function(wrapped):
        return functools.wraps(wrapped, assigned, updated)
    else:
        return lambda wrapper: wrapper
443368
def deprecated(func):
    """"""
    def newFunc(*args, **kwargs):
        print("Call to deprecated function %s." % func.__name__)
        return func(*args, **kwargs)
    newFunc.__name__ = func.__name__
    newFunc.__doc__ = func.__doc__
    newFunc.__dict__.update(func.__dict__)
    return newFunc
31522
def wrap(self, function):
        
        func = inspect.getfullargspec(function)
        needed_arguments = func.args + func.kwonlyargs

        @wraps(function)
        def wrapper(*args, **kwargs):
            arguments = kwargs.copy()
            missing_arguments = needed_arguments - arguments.keys()
            for arg in missing_arguments:
                try:
                    arguments[arg] = self._get_argument(arg)
                except KeyError:
                    pass
            return function(*args, **arguments)

        return wrapper
1190026
def deprecated(func, msg=None):
    

    message = msg or "Use of deprecated function '{}`.".format(func.__name__)

    @functools.wraps(func)
    def wrapper_func(*args, **kwargs):
        warnings.warn(message, DeprecationWarning, stacklevel=2)
        return func(*args, **kwargs)

    return wrapper_func
182553
def _reduce_method(cls, func):
        

        def wrapped_func(self, **kwargs):
            return self.reduce(func, **kwargs)
        return wrapped_func
146051
def function(self, fun, *args, **kwargs):
        '''
        
        '''
        func = self.sminion.functions[fun]
        args, kwargs = salt.minion.load_args_and_kwargs(
            func,
            salt.utils.args.parse_input(args, kwargs=kwargs),)
        return func(*args, **kwargs)
542615
def copy_func(f, name=None, sinceversion=None, doc=None):
    
    # See
    # http://stackoverflow.com/questions/6527633/how-can-i-make-a-deepcopy-of-a-function-in-python
    fn = types.FunctionType(f.__code__, f.__globals__, name or f.__name__, f.__defaults__,
                            f.__closure__)
    # in case f was given attrs (note this dict is a shallow copy):
    fn.__dict__.update(f.__dict__)
    if doc is not None:
        fn.__doc__ = doc
    if sinceversion is not None:
        fn = since(sinceversion)(fn)
    return fn
1054814
def _wrapper(self, func):
        
        def wrapped(*args, **kwargs):
            ret_val = func(*args, **kwargs)
            if isinstance(ret_val, pymongo.cursor.Cursor):
                ret_val = self.from_cursor(ret_val)
            return ret_val

        return wrapped
312018
def better_ts_function(f):
    ''''''
    fname = f.__name__

    def _(ts, *args, **kwargs):
        func = getattr(ts, fname, None)
        if func:
            return func(*args, **kwargs)
        else:
            return f(ts, *args, **kwargs)

    _.__name__ = fname

    return _
284710
def wrap_python_function(cls, fn):
        
        def wrapped(sass_arg):
            # TODO enforce no units for trig?
            python_arg = sass_arg.value
            python_ret = fn(python_arg)
            sass_ret = cls(
                python_ret,
                unit_numer=sass_arg.unit_numer,
                unit_denom=sass_arg.unit_denom)
            return sass_ret

        return wrapped
138788
def randomizable(function):
    
    @functools.wraps(function)
    def wrapper(self, *args, **kwargs):
        if self.randomize:
            self.randomize_headers()
        return function(self, *args, **kwargs)
    return wrapper
1263056
def is_cython_function(fn):
    """"""
    if hasattr(fn, "__func__"):
        fn = fn.__func__  # Class method, static method
    name = type(fn).__name__
    return (
        name == "method_descriptor"
        or name == "cython_function_or_method"
        or name == "builtin_function_or_method"
    )
1324112
def emit_function(self, return_type=None, argtypes=[], proxy=True):
        """"""

        if argtypes is not None:
            make_func = ctypes.CFUNCTYPE(return_type, *argtypes)
        else:
            make_func = ctypes.CFUNCTYPE(return_type)

        # NOTE: An optional way of binding the function is to use cffi.
        # It's a tad faster then emit_function_fast:
        #   import cffi
        #   ffi = cffi.FFI()
        #   ...
        #   code = jit.emit()
        #   func = ffi.cast("long (*fptr)(long, long)", code.value)
        #   func(123)

        code = self.emit()
        func = make_func(code.value)

        # Save this in case anyone wants to disassemble using external
        # libraries
        func.address = code

        # Because functions code are munmapped when we call _jit_destroy_state,
        # we need to return weakrefs to the functions. Otherwise, a user could
        # call a function that points to invalid memory.
        if proxy:
            self.functions.append(func)
            return weakref.proxy(func)
        else:
            return func