diff options
author | jmpc <jumapico@gmail.com> | 2010-03-21 06:30:01 (GMT) |
---|---|---|
committer | jmpc <jumapico@gmail.com> | 2010-03-21 06:30:01 (GMT) |
commit | 44c48787fe7aca42efd4ddbbb8fc76e357b72207 (patch) | |
tree | 44514251e05bd6c143c6ad38b4dd7c9456152e61 /fracciones.activity | |
parent | 52e78671ea8fd8ccbd07da236ce2d9a87f1da0a9 (diff) |
Added logic module
Diffstat (limited to 'fracciones.activity')
-rw-r--r-- | fracciones.activity/fractionlogic.py | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/fracciones.activity/fractionlogic.py b/fracciones.activity/fractionlogic.py new file mode 100644 index 0000000..6c1fcf0 --- /dev/null +++ b/fracciones.activity/fractionlogic.py @@ -0,0 +1,46 @@ +# -*- coding: utf-8 -*- +""" +Contain the logic of fractions and comparations of fractions. + +""" +import random +DENOMINATOR_MIN = 4 +DENOMINATOR_MAX = 10 + + +class FractionLogic(object): + + def __init__(self): + self.numerator = None + self.denominator = None + + + def generate(self): + """Generate new fraction""" + if DENOMINATOR_MIN < 1: + raise Exception("DENOMINATOR_MIN need be greather than 0") + self.denominator = random.randrange(DENOMINATOR_MIN, DENOMINATOR_MAX) + self.numerator = random.randrange(0, self.denominator) + + + def get_current(self): + """Return the current fraction, raise an exception if generate_fraction + hasn't called before""" + if self.denominator is None: + raise Exception("generate_fraction must be called before get_current_fraction") + return (self.numerator, self.denominator) + + + def is_equal(self, fraction): + """Check if fraction is equal that the internal""" + if not(type(fraction) is tuple and len(fraction) == 2): + raise Exception("fraction must be a tuple of length 2") + if self.denominator is None: + raise Exception("generate_fraction must be called before is_equal") + return fraction[0] * self.denominator == fraction[1] * self.numerator + + + def __repr__(self): + if self.denominator is None: + return "<FractionLogic(Undefined)>" + return "<FractionLogic(%i,%i)>"%(self.numerator,self.denominator) |