• [^] # Re: Interview de Matz le créateur de Ruby

    Posté par . En réponse à la dépêche Interview de Matz le créateur de Ruby. Évalué à -1.

    Je suis un adorateur de Python. J'ai testé Ruby (un article pour Login: de plusieurs pages écrit en novembre dernier, à paraître en septembre prochain seulement) et je préfère Python. MAIS, voici trois exemples de ce qu'on peut faire avec Python dans le genre illisible :
    FILE_EXTENSIONS = ['.bmp', '.pcx'] # list of file extensions to handle
    STOP_BY_DIRECTORY = ['htrack', 'strack'] # handle directories of one of these names
    def fileTreeVisitor(arg, dirname, names):
     if os.path.split(dirname)[1] in STOP_BY_DIRECTORY: filterFiles(dirname, names)
    def filterFiles(dirname, names):
     fileNames = [name for name in names if os.path.splitext(name)[1] in FILE_EXTENSIONS]
     if len(fileNames) == 0: return
     fileNames.sort()
     map(os.mkdir, [os.path.join(dirname, "Angle" + str(i)) for i in range(1, 9) if not os.path.exists(os.path.join(dirname, "Angle" + str(i)))])
     for i in range(0, len(fileNames), len(fileNames) / 8):
     files = filter(lambda name: int(re.search("(\d+)\.", name).group(1)) % 2 != 0, fileNames[i:i + len(fileNames) / 16])
     for file in files:
     os.system(".\i_view32.exe " + os.path.join(os.getcwd(), dirname, file) + " /bpp=8 /convert=" + os.path.join(os.getcwd(), dirname, file))
     os.rename(os.path.join(dirname, file), os.path.join(dirname, "Angle" + str((i / (len(fileNames) / 8)) + 1), file))
    if __name__ == "__main__":
     if len(sys.argv) == 1:
     print "Please specify the root working directory"
     else:
     os.path.walk(sys.argv[1], fileTreeVisitor, ())
    
    Maintenant un bot IRC :
    BOT_CHANS, BOT_HOST, BOT_PORT, BOT_IDENT, OP, IDENT_PORT, ident, bot = '#pcteam,#progworld', 'ircnet.grolier.net', 6667, 'remouleur', '~romain\.gu@*\.abo\.wanadoo\.fr', 113, socket(AF_INET, SOCK_STREAM), socket(AF_INET, SOCK_STREAM)
    ident.bind(('', IDENT_PORT)) or ident.listen(1) or bot.connect((BOT_HOST, BOT_PORT))
    irc, irchost = ident.accept()
    irc.send(re.search('(\d+)', irc.recv(1024)).group(1) + ", " + str(BOT_PORT) + ' : USERID : UNIX : ' + BOT_IDENT) and (irc.close() or ident.close())
    bot.send('NICK ' + BOT_IDENT + '\r\nUSER ' + BOT_IDENT + ' ' + gethostname() + ' ' + BOT_HOST + ' :' + BOT_IDENT + '\r\n')
    while (bot.recv(1024).find('376 ' + BOT_IDENT) == -1): pass
    bot.send('JOIN ' + BOT_CHANS + '\r\n')
    while 1:
     match = re.search('(?m)^PING.+|:([^!]+)!([^\s]+) \w+ #(\w+)\s*:(.*)', bot.recv(1024))
     if match is not None:
     if match.group(0).find('PING') == 0: bot.send('PONG ' + BOT_IDENT + '\r\n')
     elif re.search(OP, match.group(2)) is not None and re.search(BOT_IDENT + ', (.*)\r', match.group(4)) is not None and re.search(BOT_IDENT + ', (.*)\r', match.group(4)).group(1) == 'quit': bot.send("QUIT :BOUM ! V'LA L'REMOULEUR !\r\n") and (bot.close() or sys.exit(0))
     elif chr(int(re.search('0\.(\d\d)', str(Random().random())).group(1))) in match.group(1).upper(): bot.send('PRIVMSG #' + match.group(3) + ' :' + base64.decodestring(binascii.unhexlify("".join(map(lambda x: hex(x)[2:], [5330296, 81040486, 206591574, 76969238, 189158482, 87303205, 5682518, 122776406, 81085187, 88623430, 86312084, 106320906])))) + '\r\n')
     
    Et enfin un vérificateur d'XHTML :
    exec """stack = []\nfor tag in re.findall("<(/?\w+)[^/]*?>", " ".join(open(sys.argv[1]).readlines())): print (tag[0] != '/' and (lambda t: stack.append(t)) or (lambda t: stack.pop() != tag[1:]))(tag) and "\\n%s invalid because of missing <%s> " % (sys.argv[1], tag) or "",\n"""
    
    Alors non, Python n'est pas FORCEMENT lisible :-)