• [^] # Re: A quand l'équivalent des symboles Ruby en Python ?

    Posté par . En réponse à la dépêche Python 3.4 est sorti avec 7 nouveaux modules. Évalué à 3. Dernière modification le 19 mars 2014 à 14:50.

    Je viens d'aller voir le module logger, et comme je le craignais, on a un truc affreux comme ça :

    _levelNames = {
     CRITICAL : 'CRITICAL',
     ERROR : 'ERROR',
     WARNING : 'WARNING',
     INFO : 'INFO',
     DEBUG : 'DEBUG',
     NOTSET : 'NOTSET',
     'CRITICAL' : CRITICAL,
     'ERROR' : ERROR,
     'WARN' : WARNING,
     'WARNING' : WARNING,
     'INFO' : INFO,
     'DEBUG' : DEBUG,
     'NOTSET' : NOTSET,
    }
    

    avec plus loin ceci :

    def getLevelName(level):
     """
     Return the textual representation of logging level 'level'.
     If the level is one of the predefined levels (CRITICAL, ERROR, WARNING,
     INFO, DEBUG) then you get the corresponding string. If you have
     associated levels with names using addLevelName then the name you have
     associated with 'level' is returned.
     If a numeric value corresponding to one of the defined levels is passed
     in, the corresponding string representation is returned.
     Otherwise, the string "Level %s" % level is returned.
     """
     return _levelNames.get(level, ("Level %s" % level))
    def addLevelName(level, levelName):
     """
     Associate 'levelName' with 'level'.
     This is used when converting levels to text during message formatting.
     """
     _acquireLock()
     try: #unlikely to cause an exception, but you never know...
     _levelNames[level] = levelName
     _levelNames[levelName] = level
     finally:
     _releaseLock()
    

    Je trouve ça moche et inefficace. Voici donc un exemple précis ou les symboles simplifient la vie et la clarté. Et c'est à cause de ce genre de détails que je préfère Ruby à Python.