Archive

Archive for the ‘Programming’ Category

Google Translate in Python

August 2nd, 2009 admin No comments

I was looking around for an example of how to access Google’s Translation services from Python, and all the examples I found seemed too bloated to me, so I decided to write my own, and managed it in just a few lines of code. Perhaps the Ajax API didn’t exist when others wrote theirs, but it’s certainly much easier now.

So here it is, gtrans.py:

#!/usr/bin/env python
# author: sublime@3euser.com
# usage: gtrans <from_lang> <to_lang> <text>
import sys
import urllib
url = ‘http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=’+urllib.quote(‘ ‘.join(sys.argv[3:]))+’&langpair=’+sys.argv[1]+’%7C’+sys.argv[2]
# Google returns the result string in exactly the same format as a python dictionary, just need to escape ‘null’ values.
transtext = eval(urllib.urlopen(url).read().replace(‘null,’, ‘”",’))
if transtext['responseData']:
print “\n”, transtext['responseData']['translatedText'], “\n”

`

url = 'http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q='+urllib.quote(' '.join(sys.argv[3:]))+'&langpair='+sys.argv[1]+'%7C'+sys.argv[2]

transtext = eval(urllib.urlopen(url).read().replace('null,', '"",'))

if transtext['responseData']:
print "\n", transtext['responseData']['translatedText'], "\n"

`

Ultralight text viewer

July 25th, 2009 admin No comments

textview is my first application written in Genie, a python-like language which is converted into C and then compiled as a normal C program. The language is cross-platform also, and I have compiled textview on Windows just to prove it!

Although I think there’s more than enough text editors available for Linux, I was unable to find a really lightweight text viewer that could also perform basic editing functions. Previously I used leafpad or gedit for this purpose, but even that is overkill, since most of the time I just view Readme files etc. which don’t need syntax highlighting, vast arrays of menu options or dialogs popping up asking me if I want to save etc.

Be warned that textview does not have any undo or backup features, and probably never will. It’s primarily for use as a viewer, not an editor. To enter edit mode, press escape, to return to view-only mode press escape again. To save any changes, press “w” in view-only mode.

Downloads


textview_0.1.deb (deb package)

textview-windows.zip (compiled for Windows)

You will also need to download GTK for windows to run this.

textview.gs (source code)

Categories: Programming Tags: ,