Ejemplo n.º 1
0
def count_calls(function):
    '''
    Decorator for counting the calls made to a function.

    The number of calls is available in the decorated function's `.call_count`
    attribute.
    
    Example usage:
    
        >>> @count_calls
        ... def f(x):
        ...     return x*x
        ... 
        >>> f(3)
        9
        >>> f(6)
        36
        >>> f.call_count
        2
        >>> f(9)
        81
        >>> f.call_count
        3
    
    '''
    def _count_calls(function, *args, **kwargs):
        decorated_function.call_count += 1
        return function(*args, **kwargs)

    decorated_function = decorator_tools.decorator(_count_calls, function)

    decorated_function.call_count = 0

    return decorated_function
Ejemplo n.º 2
0
def count_calls(function):
    '''
    Decorator for counting the calls made to a function.

    The number of calls is available in the decorated function's `.call_count`
    attribute.
    
    Example usage:
    
        >>> @count_calls
        ... def f(x):
        ...     return x*x
        ... 
        >>> f(3)
        9
        >>> f(6)
        36
        >>> f.call_count
        2
        >>> f(9)
        81
        >>> f.call_count
        3
    
    '''
    def _count_calls(function, *args, **kwargs):
        decorated_function.call_count += 1
        return function(*args, **kwargs)
    
    decorated_function = decorator_tools.decorator(_count_calls, function)
    
    decorated_function.call_count = 0
    
    return decorated_function
Ejemplo n.º 3
0
    def decorator(function):

        # In case we're being given a function that is already cached:
        if getattr(function, 'is_cached', False): return function

        if max_size == infinity:

            cache_dict = {}

            def cached(function, *args, **kwargs):
                sleek_call_args = \
                    SleekCallArgs(cache_dict, function, *args, **kwargs)
                try:
                    return cached._cache[sleek_call_args]
                except KeyError:
                    cached._cache[sleek_call_args] = value = \
                          function(*args, **kwargs)
                    return value

        else:  # max_size < infinity

            cache_dict = OrderedDict()

            def cached(function, *args, **kwargs):
                sleek_call_args = \
                    SleekCallArgs(cache_dict, function, *args, **kwargs)
                try:
                    result = cached._cache[sleek_call_args]
                    cached._cache.move_to_end(sleek_call_args)
                    return result
                except KeyError:
                    cached._cache[sleek_call_args] = value = \
                        function(*args, **kwargs)
                    if len(cached._cache) > max_size:
                        cached._cache.popitem(last=False)
                    return value

        cached._cache = cache_dict

        result = decorator_tools.decorator(cached, function)

        def cache_clear():
            '''Clear the cache, deleting all saved results.'''
            cached._cache.clear()

        result.cache_clear = cache_clear

        result.is_cached = True

        return result
Ejemplo n.º 4
0
    def decorator(function):
        
        # In case we're being given a function that is already cached:
        if getattr(function, 'is_cached', False): return function
        
        if max_size == infinity:
            
            cache_dict = {}

            def cached(function, *args, **kwargs):
                sleek_call_args = \
                    SleekCallArgs(cache_dict, function, *args, **kwargs)
                try:
                    return cached._cache[sleek_call_args]
                except KeyError:
                    cached._cache[sleek_call_args] = value = \
                          function(*args, **kwargs)
                    return value
    
        else: # max_size < infinity
            
            cache_dict = OrderedDict()
            
            def cached(function, *args, **kwargs):
                sleek_call_args = \
                    SleekCallArgs(cache_dict, function, *args, **kwargs)
                try:
                    result = cached._cache[sleek_call_args]
                    cached._cache.move_to_end(sleek_call_args)
                    return result
                except KeyError:
                    cached._cache[sleek_call_args] = value = \
                        function(*args, **kwargs)
                    if len(cached._cache) > max_size:
                        cached._cache.popitem(last=False)
                    return value
                    
        cached._cache = cache_dict
        
        result = decorator_tools.decorator(cached, function)
        
        def cache_clear():
            '''Clear the cache, deleting all saved results.'''
            cached._cache.clear()    
        result.cache_clear = cache_clear
        
        result.is_cached = True
        
        return result
Ejemplo n.º 5
0
    def decorator(function):
        def inner(function_, *args, **kwargs):

            if decorated_function.condition is not None:

                if decorated_function.condition is True or \
                   decorated_function.condition(
                       decorated_function.original_function,
                       *args,
                       **kwargs
                       ):

                    decorated_function.profiling_on = True

            if decorated_function.profiling_on:

                if decorated_function.off_after:
                    decorated_function.profiling_on = False
                    decorated_function.condition = None

                # This line puts it in locals, weird:
                decorated_function.original_function

                base_profile.runctx(
                    'result = '
                    'decorated_function.original_function(*args, **kwargs)',
                    globals(),
                    locals(),
                    sort=decorated_function.sort)
                return locals()['result']

            else:  # decorated_function.profiling_on is False

                return decorated_function.original_function(*args, **kwargs)

        decorated_function = decorator_tools.decorator(inner, function)

        decorated_function.original_function = function
        decorated_function.profiling_on = None
        decorated_function.condition = condition
        decorated_function.off_after = off_after
        decorated_function.sort = sort

        return decorated_function
Ejemplo n.º 6
0
 def decorator(function):
     
     def inner(function_, *args, **kwargs):
         
         if decorated_function.condition is not None:
             
             if decorated_function.condition is True or \
                decorated_function.condition(
                    decorated_function.original_function,
                    *args,
                    **kwargs
                    ):
                 
                 decorated_function.profiling_on = True
                 
         if decorated_function.profiling_on:
             
             if decorated_function.off_after:
                 decorated_function.profiling_on = False
                 decorated_function.condition = None
                 
             # This line puts it in locals, weird:
             decorated_function.original_function
             
             base_profile.runctx(
                 'result = '
                 'decorated_function.original_function(*args, **kwargs)',
                 globals(), locals(), sort=decorated_function.sort
             )                
             return locals()['result']
         
         else: # decorated_function.profiling_on is False
             
             return decorated_function.original_function(*args, **kwargs)
         
     decorated_function = decorator_tools.decorator(inner, function)
     
     decorated_function.original_function = function
     decorated_function.profiling_on = None
     decorated_function.condition = condition
     decorated_function.off_after = off_after
     decorated_function.sort = sort
     
     return decorated_function
Ejemplo n.º 7
0
 def __call__(self, function):
     '''Decorate `function` to use this context manager when it's called.'''
     def inner(function_, *args, **kwargs):
         with self:
             return function_(*args, **kwargs)
     return decorator_tools.decorator(inner, function)