Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #4986

RE: Coolest Python recipe of all time

From Trent Nelson <trent@snakebite.org>
Date 2011-05-09 02:31 -0700
Subject RE: Coolest Python recipe of all time
References <69c1813d-1a9a-4686-9768-8ec1910a45f8@d19g2000prh.googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.1327.1304933610.9059.python-list@python.org> (permalink)

Show all headers | View raw


> What are your favorites?

I think I've posted this before, but I love my 3-lines-if-you-ignore-the-scaffolding language translator.  Not because it's clever code -- quite the opposite, the code is dead simple -- but because it encompasses one of the things I love about Python the most: it gets shit done.

    In [1]: from translate import *

    In [2]: translate('French', 'The quick brown fox jumped over the lazy dog.')
    Le renard brun rapide a sauté par-dessus le chien paresseux.

    In [3]: translate('German', 'The quick brown fox jumped over the lazy dog.')
    Der schnelle braune Fuchs sprang über den faulen Hund.

    In [4]: translate('Spanish', 'The quick brown fox jumped over the lazy dog.')
    El zorro marrón rápido saltó sobre el perro perezoso.

translate.py:

    import sys
    from urllib import urlopen, urlencode
    from BeautifulSoup import BeautifulSoup

    url = 'http://babelfish.altavista.com/tr'
    languages = {
        'French'    : 'en_fr',
        'German'    : 'en_de',
        'Italian'   : 'en_it',
        'Spanish'   : 'en_es',
        'Russian'   : 'en_ru',
        'Portuguese': 'en_pt',
        'Dutch'     : 'en_nl',
        'Japanese'  : 'en_ja',
    }

    def translate(lang, text):
        kwds = { 'trtext' : text, 'lp' : languages[lang]}
        soup = BeautifulSoup(urlopen(url, urlencode(kwds)))
        print soup.find('div', style='padding:10px;').string

    if __name__ == '__main__':
        translate(sys.argv[1], sys.argv[2])

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Coolest Python recipe of all time Raymond Hettinger <python@rcn.com> - 2011-05-02 10:33 -0700
  Re: Coolest Python recipe of all time David Monaghan <monaghand.david@gmail.com> - 2011-05-02 21:48 +0100
    Re: Coolest Python recipe of all time Ian Kelly <ian.g.kelly@gmail.com> - 2011-05-02 14:58 -0600
      Re: Coolest Python recipe of all time David Monaghan <monaghand.david@gmail.com> - 2011-05-02 22:45 +0100
        Re: Coolest Python recipe of all time Stefan Behnel <stefan_ml@behnel.de> - 2011-05-03 07:04 +0200
          Re: Coolest Python recipe of all time Raymond Hettinger <python@rcn.com> - 2011-05-03 09:43 -0700
            Re: Coolest Python recipe of all time Chris Angelico <rosuav@gmail.com> - 2011-05-04 07:54 +1000
            Re: Coolest Python recipe of all time Ian Kelly <ian.g.kelly@gmail.com> - 2011-05-03 16:10 -0600
        Re: Coolest Python recipe of all time Ian Kelly <ian.g.kelly@gmail.com> - 2011-05-02 23:17 -0600
        Re: Coolest Python recipe of all time Terry Reedy <tjreedy@udel.edu> - 2011-05-03 02:00 -0400
          Re: Coolest Python recipe of all time Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2011-05-03 18:29 +1200
            Re: Coolest Python recipe of all time Terry Reedy <tjreedy@udel.edu> - 2011-05-03 11:49 -0400
            Re: Coolest Python recipe of all time Raymond Hettinger <python@rcn.com> - 2011-05-03 09:32 -0700
            Re: Coolest Python recipe of all time geremy condra <debatem1@gmail.com> - 2011-05-03 09:51 -0700
        Re: Coolest Python recipe of all time Stefan Behnel <stefan_ml@behnel.de> - 2011-05-03 08:23 +0200
          Re: Coolest Python recipe of all time Raymond Hettinger <python@rcn.com> - 2011-05-03 15:19 -0700
  Re: Coolest Python recipe of all time Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-05-06 16:59 +0000
    Re: Coolest Python recipe of all time geremy condra <debatem1@gmail.com> - 2011-05-06 10:43 -0700
    Re: Coolest Python recipe of all time Ian Kelly <ian.g.kelly@gmail.com> - 2011-05-06 12:36 -0600
      Re: Coolest Python recipe of all time Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-05-07 08:29 +0000
        Re: Coolest Python recipe of all time Ian Kelly <ian.g.kelly@gmail.com> - 2011-05-07 08:54 -0600
        Re: Coolest Python recipe of all time Raymond Hettinger <python@rcn.com> - 2011-05-07 14:02 -0700
    Re: Coolest Python recipe of all time Ian Kelly <ian.g.kelly@gmail.com> - 2011-05-06 13:38 -0600
    Re: Coolest Python recipe of all time Raymond Hettinger <python@rcn.com> - 2011-05-06 12:58 -0700
  RE: Coolest Python recipe of all time Trent Nelson <trent@snakebite.org> - 2011-05-09 02:31 -0700
    Re: Coolest Python recipe of all time Raymond Hettinger <python@rcn.com> - 2011-05-09 14:10 -0700

csiph-web