def str2bucket(text: str, num_buckets: int) ->int:
    """Takes a string and uses a consistent hash to return an int selected
    evenly from the range 0 to (num_buckets - 1). Useful for 'sticky' load
    balancing on an arbitrary string key.

    >>> str2bucket('foo', 99)
    91
    >>> str2bucket('bar', 99)
    63
    >>> str2bucket('foo', 3)
    1
    >>> str2bucket('bar', 3)
    0
    >>> str2bucket('baz', 0)
    0
    """
    if num_buckets == 0:
        return 0
    from hashlib import md5
    return int(md5(text.encode()).hexdigest(), 16) % num_buckets
