Voici mon petit code :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# -*- coding: utf-8 -*-
#!/usr/bin/env python
 
import re
from fractions import Fraction
 
# The following pattern commes from the book "Dive into Python".
# It will be use to test if a text is a valid roman number.
PATTERN_ROMAN_NUMERAL = re.compile("""
    ^                   # beginning of string
    M{0,4}              # thousands - 0 to 4 M's
    (CM|CD|D?C{0,3})    # hundreds - 900 (CM), 400 (CD), 0-300 (0 to 3 C's),
                        #            or 500-800 (D, followed by 0 to 3 C's)
    (XC|XL|L?X{0,3})    # tens - 90 (XC), 40 (XL), 0-30 (0 to 3 X's),
                        #        or 50-80 (L, followed by 0 to 3 X's)
    (IX|IV|V?I{0,3})    # ones - 9 (IX), 4 (IV), 0-3 (0 to 3 I's),
                        #        or 5-8 (V, followed by 0 to 3 I's)
    $                    # end of string
    """ ,re.VERBOSE)
 
def isUpperRoman(stringToTest):
    return bool(PATTERN_ROMAN_NUMERAL.search(stringToTest))
 
def isInteger(stringToTest):
    try:
# We cannot use  isinstance(stringToTest, int) ...
        int(stringToTest)
        return True
    except:
        return False
 
def isRational(stringToTest):
    try:
        Fraction(stringToTest)
        return True
    except:
        return False
 
 
###############
###############
##           ##
## FOR TESTS ##
##           ##
###############
###############
 
if __name__ == '__main__':
    tests = []
 
    tests.append("isUpperRoman('I')")
    tests.append("isUpperRoman('4')")
 
    tests.append("isInteger('I')")
    tests.append("isInteger('4')")
 
    tests.append("isRational('I')")
    tests.append("isRational('4')")
    tests.append("isRational('4/5')")
    tests.append("isRational('123.667')")
 
    for oneTest in tests:
        print oneTest
        print '\t',
        exec "print " + oneTest
Les méthodes sont typiquement celles de canards boîteux dont raffolent Python.

Ma question est de savoir s'il est possible de faire autrement. Simple curiosité.