django_any/xunit.py
def weighted_choice(choices):
    """
    Supposes that choices is sequence of two elements items,
    where first one is the probability and second is the
    result object or callable

    >>> result = weighted_choice([(20,'x'), (100, 'y')])
    >>> result in ['x', 'y']
    True
    """
    total = sum([weight for (weight, _) in choices])
    i = random.randint(0, total - 1)
    for weight, choice in choices:
        i -= weight
        if i < 0: 
            if callable(choice):
                return choice()
            return choice
    raise Exception('Bug')
def weighted_choice(choices):
    """
    Supposes that choices is sequence of two elements items,
    where first one is the probability and second is the
    result object or callable

    >>> result = weighted_choice([(20,'x'), (100, 'y')])
    >>> result in ['x', 'y']
    True
    """
    total = sum([weight for (weight, _) in choices])
    i = random.randint(0, total - 1)
    for weight, choice in choices:
        i -= weight
        if i < 0: 
            if callable(choice):
                return choice()
            return choice
    raise Exception('Bug')
def weighted_choice ( choices ) : total = sum ( [ weight for ( weight , _ ) in choices ] ) i = random . randint ( 0 , total - 1 ) for weight , choice in choices : i -= weight if i < 0 : if callable ( choice ) : return choice ( ) return choice raise Exception ( 'Bug' )
Supposes that choices is sequence of two elements items,
    where first one is the probability and second is the
    result object or callable

    >>> result = weighted_choice([(20,'x'), (100, 'y')])
    >>> result in ['x', 'y']
    True
Supposes that choices is sequence of two elements items where first one is the probability and second is the result object or callable >>> result = weighted_choice ( [ ( 20 x ) ( 100 y ) ] ) >>> result in [ x y ] True