Sunday, December 9, 2012

Google App Engine Launcher in Fedora



    Google App Engine launcher is a handy tool for GAE development. Although GAE sdk and command line are sufficient, one click execution\deployment is often more convenient. When I was installing GAE launcher I couldn't find a single guide for Fedora, hence this post. I am using  Fedora 16 + LXDE.  This post assumes that you already have GAE sdk.

Get the prerequisites:

sudo yum install wxGlade wxPython

Download source code:

The app engine launcher source is available on code.google.com.  If you have subversion installed, go to this link and follow the instructions. 
If you don't have subversion, you can use wget as follows:

wget -m -np http://google-appengine-wx-launcher.googlecode.com/svn/trunk/ google-appengine-wx-launcher-read-only


This command will fetch the code and put it in ./google-appengine-wx-launcher.googlecode.com/svn/trunk You can move the code to another directory. I copied the content of trunk directory to /opt/appengine_launcher

Create a desktop entry:

Save the following text to /usr/share/applications/gae.desktop
(make sure you use correct path in Exec parameter)

[Desktop Entry]
Encoding=UTF-8
Name=App Engine Launcher
Categories=Development;
Type=Application
Exec=python /opt/appengine_launcher/GoogleAppEngineLauncher.py
Icon=AppEngine

Icon for desktop entry:

Copy AppEngine.png inside images directory to /usr/share/icons/ and Done!

GAE launcher should appear in the programming section. You'll probably need to configure path to gae sdk the first time you run it. Set it in GAE Launcher preferences.


Thats it! Happy Coding!

Thursday, October 11, 2012

Building a Search Application (well, kind of...)

    
    When I finished CS 101 on Udacity, I didn't realize the potential of the things I learned. I was happy that I got to learn some Python constructs and got to know how search engines work. It's only when the results of Udacity's contest were announced, I realized the awesome things I could do with what I learned in CS 101. Udacians had developed some great apps. Then in June I took CS 253 and learned to use Google App Engine (GAE). It was a great experience. Though Steve Huffman isn't a professional teacher, he did a great job (better than most of my college professors). Later in September, I decided to build something to reinforce my knowledge of Python and GAE. That's how Find Dialogue happened.

    Find Dialogue is an app that lets you search the transcripts of Big Bang Theory (season 1-5). It is built in Python using webapp2 framework and Jinja2 template engine. Most of the styling is done using Twitter Bootstrap. I have also used css examples from various other sources where bootstrap didn't give the desired results. Initially I was thinking about adding transcripts of multiple sitcoms but after some googling and reading I came to the conclusion that it would be better if I concentrated on a single sitcom. So I chose Big Bang Theory.  

    I started looking for transcripts and I found this blog, perfect for my purpose. I used BeautifulSoup to get the transcripts and Python to parse the transcripts and to build the search index. Parsing involved adding line numbers to the transcripts so that it would be possible to search and extract the exact line where certain words occur. Search index basically maps words to their occurrences in the transcripts enabling faster lookups. Again, Python was used to convert the index and transcripts into csv format required by GAE bulk loader. Figuring out how the bulk loader works took a while but it was worth it. 

    Once I had transcripts and index in my local datastore, I wrote the search logic. The process of obtaining results after user submits a search query is as follows:
Upon receiving search query, the program filters it to remove some elements and divides it into words. If after filtering there are more than 10 words, the remaining words are discarded. List of occurrences of each word is obtained from the index. These lists are combined and processed to obtain list of results sorted according to the relevance. Only 10 most relevant results  are considered for further processing. Using these results, snippets of conversation are obtained from datastore (or memcahce) and are passed to the template engine. The template engine replaces \n with <br/>, inserts <mark> tags (with a little help from python) and generates HTML. Additionally, each result can be clicked to view the entire transcript in a nice format. 

    Building Find Dialogue was a great learning experience. Dealing with non-ascii characters and GAE bulk loader was difficult but I learned things that will help me in future projects. I was greatly inspired by Connor Mendenhall's DaveDaveFind. Big thanks to Ash for the transcripts. If you want to build something like this, I would suggest taking CS 101 and CS 253. Source code of my application, along with the code I used for pre-processing is available on github. Feel free to edit/improve/use it for whatever you want. :)


Wednesday, August 8, 2012

Beating Google Olympic Doodles

 
    Google has been creating interactive doodles for a while now. I am a big fan of these doodles. So when Google launched their first interactive Olympic doodle last night, I was hooked to it. Soon my friends started posting their high scores and my best score was far from theirs. I played for a while but soon lost interest. So I decided to try programming.

    Few months ago I had come across Robot class in Java. I used it to simulate keystrokes from java program. Here is what Java Documentation says about Robot:

This class is used to generate native system input events for the purposes of test automation, self-running demos, and other applications where control of the mouse and keyboard is needed. The primary purpose of Robot is to facilitate automated testing of Java platform implementations.

    I created a java program that simulates left-right keystrokes for the hurdles doodle. I didn't hardcode the jumping part because that would have required a lot of trial and error and would have taken the fun out of it. So you still have to jump the hurdles yourself. With the help of my program I clocked 9.6, better than any of my friends.

    Next day Google launched the basketball doodle. I played it for a while and realized that unlike the hurdles doodle, this doodle could be completely automated without extensive trial and error. So I modified the previous code and scored 42 pts in it. It was amusing to see it in action.

Both the programs are simple and easy to understand. The source codes can be found on pastebin.
Hurdles Doodle in Java 
Basketball Doodle in Java


UPDATE:
There are some hacks in JavaScript that make the runner go faster. It is perhaps much easier to do the above programs in JavaScript. But my intention wasn't to hack the doodles, I just wanted to see if it could be done using Java .

Google's view on this cheating stuff is interesting (Mashable):
“Our hope is that people will want to play a fair game, but we also understand that for some people cheating is in fact an engineering challenge — its own kind of programming Olympics.”

Saturday, June 16, 2012

ID3 Tag Modifier using Python


        I was organizing my mp3 library & I noticed that a lot of meta-information was not in proper case. As I am learning python, I decided to test whether mp3 metadata could be modified using python program. After an hour spent googling and coding, finally I have a simple program that changes title, artist and album information of mp3 files in 'Title' case. The program uses eyeD3 library to acess ID3 metadata.

import eyeD3
import os
import fnmatch
#---------------------------------------------
def modifyEyeD3Tags(path):
    tag = eyeD3.Tag()
    tag.link(path)
    
    song_artist = tag.getArtist()
    song_album = tag.getAlbum()
    song_title = tag.getTitle()
    """
    print song_artist
    print song_album
    print song_title
    """
    song_artist=song_artist.title()
    song_album=song_album.title()
    song_title=song_title.title()
    
    tag.setArtist(song_artist)
    tag.setAlbum(song_album)
    tag.setTitle(song_title)
    tag.update()
    """
    print song_artist
    print song_album
    print song_title
    """

directory = os.path.dirname(os.path.realpath(__file__))
print "Directory => ",directory 
files_list = os.listdir(directory)
print "Starting Tag Modification"
for x in files_list:
    if fnmatch.fnmatch(x, '*.mp3'):
        print "File:",x
        modifyEyeD3Tags(x)
print "Finished Tag Modification"

        The program first obtains the path of the directory in which the python source file resides. Then it fetches names of all the files in that directory into a list. Then it traverses the list and passes mp3 filenames to modifyEyeD3Tags() function. The modifyEyeD3Tags() function performs the actual case change operation.

        The programs performs very trivial task and perhaps has no practical use (apart from aesthetically enhancing ID3 tags :p ) but I learned a few things in python and that's all that matters!


References:

Friday, March 23, 2012

Cows and Bulls in Java


A basic implementation of Cows and Bulls game in Java. In this program I have used Absolute Positioning concept (No specific reason for using absolute positioning, just wanted to try it). JTable is used to display entered words and score (number of cows and bulls).



External links:
Source code [pastebin]
Source code with data file [.zip]

Saturday, March 10, 2012

Hangman in Java



    We all have played  Hangman at some point of time in our lives. This is a basic implementation of Hangman game in Java using Canvas. In this implementation, the word to guess is chosen randomly from a file. Hangman diagram is represented by a series of images. Each time player makes a false guess a new image is loaded containing one more element of the hangman than in the previous image. Images are loaded using Java Image I/O API. Rest of the logic can be understood by reading the code and comments. The entire code and required resources can be downloaded from here. Also, I have uploaded the source file on pastebin for quick reference.

Happy Coding! 


Screenshot

Wednesday, February 1, 2012

Low Power Consumption Mode for ATI cards in Fedora


     I have ATI Radeon HD 4570 graphics card. Ever since I started using Fedora, I couldn't properly install drivers for my graphics card. It's hell lot of difficult to get ATI drivers working on Fedora. Last week, when I installed proprietary ATI drivers on F16, the OS just stopped working. It would stop loading just before the login-prompt. This time I decided not to pursue the problem & did a rollback.

Somewhere on the internet I found this:
echo low > /sys/class/drm/card0/device/power_profile
   
    It sets the graphic card to low power consumption mode. Upon using my laptop in this mode for a few days I noticed that this has solved the overheating problem to some extent. So I would definitely recommend it over installing crappy ATI drivers. Still its far from what I get on Windows Vista so I am considering switching to Ubuntu when its next LTS version is released.