Path: csiph.com!x330-a1.tempe.blueboxinc.net!aioe.org!feeder.news-service.com!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.016 X-Spam-Evidence: '*H*': 0.97; '*S*': 0.00; 'subject:Python': 0.04; 'sys': 0.04; '__name__': 0.07; 'python': 0.07; 'def': 0.13; "'__main__':": 0.16; 'kwds': 0.16; 'received:192.168.11': 0.16; 'skip:3 30': 0.16; 'soup': 0.16; 'text):': 0.16; 'to:name:python- list@python.org': 0.16; 'language': 0.20; 'code': 0.22; 'header :In-Reply-To:1': 0.22; 'posted': 0.22; 'skip:b 20': 0.27; "skip:' 10": 0.32; 'import': 0.32; 'to:addr:python-list': 0.32; "i've": 0.33; 'things': 0.33; 'received:192': 0.34; 'done.': 0.35; 'print': 0.35; '\xfcber': 0.35; 'quite': 0.36; 'think': 0.36; 'received:192.168': 0.37; 'but': 0.38; 'languages': 0.38; 'to:addr:python.org': 0.39; "it's": 0.40; 'header:Received:5': 0.40; 'simple': 0.60; 'fox': 0.73; 'dead': 0.80; "'french'": 0.84; 'shit': 0.84; 'sprang': 0.84; 'brown': 0.97 From: Trent Nelson To: "python-list@python.org" Date: Mon, 9 May 2011 02:31:07 -0700 Subject: RE: Coolest Python recipe of all time Thread-Topic: Coolest Python recipe of all time Thread-Index: AcwI8Iz4zCi/Q5YrRhSFsjsKFNL+WQFOYuVQ References: <69c1813d-1a9a-4686-9768-8ec1910a45f8@d19g2000prh.googlegroups.com> In-Reply-To: <69c1813d-1a9a-4686-9768-8ec1910a45f8@d19g2000prh.googlegroups.com> Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: acceptlanguage: en-US Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 48 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1304933611 news.xs4all.nl 81478 [::ffff:82.94.164.166]:41316 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:4986 > What are your favorites? I think I've posted this before, but I love my 3-lines-if-you-ignore-the-sc= affolding language translator. Not because it's clever code -- quite the o= pposite, the code is dead simple -- but because it encompasses one of the t= hings 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 d= og.') Le renard brun rapide a saut=E9 par-dessus le chien paresseux. In [3]: translate('German', 'The quick brown fox jumped over the lazy d= og.') Der schnelle braune Fuchs sprang =FCber den faulen Hund. In [4]: translate('Spanish', 'The quick brown fox jumped over the lazy = dog.') El zorro marr=F3n r=E1pido salt=F3 sobre el perro perezoso. translate.py: import sys from urllib import urlopen, urlencode from BeautifulSoup import BeautifulSoup url =3D 'http://babelfish.altavista.com/tr' languages =3D { '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 =3D { 'trtext' : text, 'lp' : languages[lang]} soup =3D BeautifulSoup(urlopen(url, urlencode(kwds))) print soup.find('div', style=3D'padding:10px;').string if __name__ =3D=3D '__main__': translate(sys.argv[1], sys.argv[2])