sample_data_utils/utils.py
def unique(func, num_args=0, max_attempts=100, cache=None):
    """
    wraps a function so that produce unique results

    :param func:
    :param num_args:

    >>> import random
    >>> choices = [1,2]
    >>> a = unique(random.choice, 1)
    >>> a,b = a(choices), a(choices)
    >>> a == b
    False
    """
    if cache is None:
        cache = _cache_unique

    @wraps(func)
    def wrapper(*args):
        key = "%s_%s" % (str(func.__name__), str(args[:num_args]))
        attempt = 0
        while attempt < max_attempts:
            attempt += 1
            drawn = cache.get(key, [])
            result = func(*args)
            if result not in drawn:
                drawn.append(result)
                cache[key] = drawn
                return result

        raise MaxAttemptException()

    return wrapper
def unique(func, num_args=0, max_attempts=100, cache=None):
    if cache is None:
        cache = _cache_unique

    @wraps(func)
    def wrapper(*args):
        key = "%s_%s" % (str(func.__name__), str(args[:num_args]))
        attempt = 0
        while attempt < max_attempts:
            attempt += 1
            drawn = cache.get(key, [])
            result = func(*args)
            if result not in drawn:
                drawn.append(result)
                cache[key] = drawn
                return result

        raise MaxAttemptException()

    return wrapper
def unique ( func , num_args = 0 , max_attempts = 100 , cache = None ) : if cache is None : cache = _cache_unique @ wraps ( func ) def wrapper ( * args ) : key = "%s_%s" % ( str ( func . __name__ ) , str ( args [ : num_args ] ) ) attempt = 0 while attempt < max_attempts : attempt += 1 drawn = cache . get ( key , [ ] ) result = func ( * args ) if result not in drawn : drawn . append ( result ) cache [ key ] = drawn return result raise MaxAttemptException ( ) return wrapper
wraps a function so that produce unique results

    :param func:
    :param num_args:

    >>> import random
    >>> choices = [1,2]
    >>> a = unique(random.choice, 1)
    >>> a,b = a(choices), a(choices)
    >>> a == b
    False
wraps a function so that produce unique results