#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""Utilities for random processes"""


import sys
import random


NumberTypes = (int, float)

class RandUtilException(Exception):
    """A simple custom Exception class for random_utils.py"""
    def __init__(self, m):
        message = "[random_utils] {}".format(m)
        super(RandUtilException, self).__init__(message)


class WeightedRandom(object):
    """A class for testing objects with predefined odds ratios"""
    
    def __init__(self, weights, prob):
        if not isinstance(weights, dict):
            raise RandUtilException("weights argument has to a dictionary")
        if not all(isinstance(x, NumberTypes) for x in weights.values()):
            raise RandUtilException("weights have to a number")
        self.odds_ratio = weights
        self.probs = {k: v / (1 + v) for k, v in self.odds_ratio.items()}
        max_w = max(self.odds_ratio.values())
        self.probs2 = {k: v / max_w for k, v in self.odds_ratio.items()}
        max_p = max([p / (1 - p * prob) for p in self.odds_ratio.values()])
        self.probs3 = {k: (v / (1 - v * prob)) / max_p for k, v
                       in self.odds_ratio.items()}
    def is_chosen(self, key):
        if not key in self.probs3:
            return False
        return self.probs3[key] > random.random()
            

def main():
    """Main body of code"""
    return 0


if __name__ == '__main__':
    sys.exit(main())
