Re-implemented gen_ratio in python