OddEven Sample Python Code

Code:

class OddEven(object):

    Even = ("""Even""")
    Odd = ("""Odd""")

    def convert(number):
        if number % 2 == 0:
            return OddEven.Even
        if OddEven.__is_prime(number):
            return number

        return OddEven.Odd

    def convert_range(start, stop):
        result = ""
        for x in range(start, stop):
            result += str(OddEven.convert(x)) + " "
        return result.rstrip()

    def convert_with_string(number):

        if (str(number).endswith("0") or 
            str(number).endswith("2") or 
            str(number).endswith("4") or 
            str(number).endswith("6") or 
            str(number).endswith("8")):
            return OddEven.Even
        
        if OddEven.__is_prime(number):
            return number

        return OddEven.Odd

    def __is_prime(n):
        if n == 2 or n == 3: return True
        if n < 2 or n%2 == 0: return False
        if n < 9: return True
        if n%3 == 0: return False
  
        r = int(n**0.5)
        f = 5
        while f <= r:
            if n%f == 0: return False
            if n%(f+2) == 0: return False
            f +=6
        return True 

Test:

class Test_oddeven(unittest.TestCase):
    def test_oddeven(self):

        testcases = {}
        testcases[1] = "Odd"
        testcases[2] = "Even"
        testcases[3] = 3
        testcases[4] = "Even"
        testcases[5] = 5
        testcases[6] = "Even"
        testcases[7] = 7
        testcases[8] = "Even"
        testcases[9] = "Odd"
        testcases[10] ="Even"

        oddEven = oddeven.OddEven

        for key, value in testcases.items():

            result = oddEven.convert(key)
            self.assertEqual(value, result)

    def test_oddeven_range(self):

        oddEven = oddeven.OddEven
        actual = oddEven.convert_range(1,11)
        self.assertEqual(actual, "Odd Even 3 Even 5 Even 7 Even Odd Even")

    def test_oddeven_with_string(self):

        testcases = {}
        testcases[1] = "Odd"
        testcases[2] = "Even"
        testcases[3] = 3
        testcases[4] = "Even"
        testcases[5] = 5
        testcases[6] = "Even"
        testcases[7] = 7
        testcases[8] = "Even"
        testcases[9] = "Odd"
        testcases[10] ="Even"

        oddEven = oddeven.OddEven

        for key, value in testcases.items():

            result = oddEven.convert_with_string(key)
            self.assertEqual(value, result)