Un enum plutôt complexe (java vers python)

Fermé
periplasme Messages postés 391 Date d'inscription vendredi 22 avril 2011 Statut Membre Dernière intervention 5 février 2013 - Modifié par periplasme le 12/04/2012 à 09:06
periplasme Messages postés 391 Date d'inscription vendredi 22 avril 2011 Statut Membre Dernière intervention 5 février 2013 - 12 avril 2012 à 09:06
Bonjour la compagnies !

ayant fini un projet en java pour mon IUT, je souhaitais le retranscrire en python.
pour ce projet, on nous as fournit un code de base, à agrémenté et à complété; C'est ce code que je souhaite retranscrire dans un premier temps. le soucis étant que python n'as pas de type énuméré de base, j'ai du mal à concevoir comment réimplémenté ce gros bouzin :

enum CommandWord { 
    GO("go"), QUIT("quit"), HELP("help"); 

    private final static Map<String, CommandWord> COMMANDS; 

    private final static String ALL_COMMANDS; 

    static { 
        final Map<String, CommandWord> commands = new LinkedHashMap<String, CommandWord>(); 
        final StringBuilder stb = new StringBuilder(); 
        for (CommandWord cw : CommandWord.values()) { 
            commands.put(cw.stringDesc, cw); 
            stb.append(cw.stringDesc).append(' '); 
        } 
        COMMANDS = Collections.unmodifiableMap(commands); 
        ALL_COMMANDS = stb.toString(); 
    } 

    private final String stringDesc; 

    private CommandWord(final String stringDesc) { 
        this.stringDesc = stringDesc; 
    } 

    /** 
     * Returns the CommandWord member matching the given String value, or null 
     * when the given String is either null or unknown. 
     */ 
    public static CommandWord command(final String aString) { 
        if (aString == null) { 
            return null; 
        } 
        return COMMANDS.get(aString); 
    } 

    /** 
     * Returns the list of all commands. 
     */ 
    public static String allCommands() { 
        return ALL_COMMANDS; 
    } 
}


vous me conseillez quoi ? j'ai commencer une petite classe, mais j'ai peur que cela ne conviennent pas ...
j'avoue que ça me perturbe un peu ...


A voir également:

1 réponse

periplasme Messages postés 391 Date d'inscription vendredi 22 avril 2011 Statut Membre Dernière intervention 5 février 2013 53
12 avril 2012 à 09:06
je me permet un petit up, avec mise à jour du sujet
0