Showing posts with label code-snippets. Show all posts
Showing posts with label code-snippets. Show all posts

Sunday, October 11, 2009

Uploading Data to App Engine

Pre-requisite:
  1. You know some Django
  2. Your Django App Engine app is working properly on your dev server
Here are the relevant lines in the app.yaml file that will ensure data upload to the live server:

handlers:
- url: /load
script: $PYTHON_LIB/google/appengine/ext/remote_api/handler.py
login: admin
Make sure the above is the very first item in the handlers section because yaml file is parsed top to bottom.
  • Upload your latest source file to your app engine account
  • Test to see if the following url is accessible:
    http://yourapp.appspot.com/load
    Your should either be prompted to authenticate into your Google Account or get the following message if you are already authenticated:
    This request does not contain a necessary header
The above confirms that the upload can take place. The app.yaml in some ways behave as a Django's urls.py or Routes in Rails. Proceed with the usual data upload command:


appcfg.py upload_data --config_file=yourapp/module/module_loader.py --filename=yourapp/fixtures/data.csv --kind=Module yourapp

Monday, September 14, 2009

My first Rails App!

After a few days of pouring my mind over Ruby and Rails, I have finally completed my very first web application in an afternoon. It is called Podcast Lookup which aggregates the podcasts I listen to on a regular basis. The web application simply shows the publish date of the latest episodes. It is quite useful if you listen to a large number of podcasts and need a way to view them all when jumping across different OS. The project still needs a lot of improvements which I can iterate on from now on, isn't that what the whole agile development hotness is these days?

Some thoughts on Rails

Functionality drop out of the sky into your lap. This is mostly how the Rails framework is implemented, it tries to make the developer life easy and lazy by achieving a lot with the least amount of code and without having to know where methods or variables come from. Not that it's a bad thing for someone coming from C++, Java or Python but I'll be very worried on how someone who only knows Ruby and Rails can move outside their comfort zone.

Ruby on Rails will hold both your hands and spoon feed you to make your life as a Web Developer easy and productive. In some ways, it feels like Perl because I am not sure what some of the minimal code is trying to do. Having a reference manual nearby seems invaluable. Maybe it's too early for me to judge the framework after only playing with it over the last 3 weeks. I'll be working on it a bit more over the next few months to get a better feel of it.

Rails for newbies

If you want to learn Ruby on Rails, DO NOT DEVELOP ON WINDOWS. Do yourself a favour and save some time by writing codes on Linux or Mac. You will avoid a lot of Yak shaving that way. Ruby 1.9.1 and Rails 2.3.3 simply do not play well with Windows when you go beyond the out of the box features. Some gems will just refuse to install on Windows unless you do some configure and build gymnastics on them. You are here to learn the language and framework, not building source code unless that is your motivation, I have done enough of that for the last 4 years.

The Rails Guide is a stepping stone to get yourself familiar with the technology. Once you have exhausted this resource, borrow Simply Rails 2 from your local library if it is available and do the example project. It will really help you develop your pet project if you have one. Other necessary tools:
  • Optional - Balsamiq Mockup for fast prototyping on how your web app will look like.
  • Github for SCM and to show off your source code. It is free for public repositories. Git is also the revision control of choice if you want to become a Ruby on Rails acolyte.
  • Heroku provides free Ruby on Rails web hosting. Their web site doesn't say it explicitly but you can host multiple small Rails web app over there. However, your application won't be able to write to disk when hosted on their server, at least for the free plan. This may or may not be an issue depending on the nature of your project.

    Advanced developer: Pushing data from your local machine to the Heroku server can be tricky because the schema_migrations table has already been created and populated by the time you call:
    rake db:push
    The stupid way to handle this at the moment is to quickly prefix your local schema_migrations with z to become zschema_migrations and invoke the above command. It will fail when it reaches zschema_migrations turn but data from other tables will be uploaded.
This is my opinion on Ruby and Rails and I think developers who are interested of the current buzzword should consider giving it a try. Even if you don't like the Ruby language or Rails framework, you might learn some new approach from the experience. It's the journey that makes it exciting, not the end goal.

Monday, September 7, 2009

Rails Magic - Part 1

I have heard a lot about the magic in Rails without really experiencing it until today. In Django, url names are explicitly defined in the urls.py file, for instance stories-index is the url name for stories/:
url(r'stories/$', stories_view, name='stories-index')
In Rails, there is a built-in url helper which will automatically generate those route names, my routes.rb is:
ActionController::Routing::Routes.draw do |map|
map.resources :stories
map.connect ':controller/:action/:id'
map.connect ':controller/:action/:id.:format'
end
The route name to stories is stories_path, the module ActionController::Resources take care of that. Here's a sample usage of the stories route name, stories_path:
def create
@story = Story.new(params[:story])
@story.save
redirect_to stories_path
end
You can check those route names with the following command at the CLI:
rake routes
You still have to suffix the displayed names with _url or _path. It's not a bad thing to see some automatic name generation but the rails developer will have to be aware of it.

Sunday, July 5, 2009

Display n number of stars in HTML/CSS

This weekend I was trying to figure out how to display a specific number of star images given an integer. I could have looped through n images but then my html code will look messy. I decided to do it with CSS. Another requirement will be to place the stars as an inline element with my text. Using span and displaying it as inline-block seems to have solved this issue. I noticed that Amazon uses a more elegant technique by only showing chunk of a larger image of stars.


Saturday, June 13, 2009

Retrieve url from keywords in Python

I had an interesting problem at work this week. I was given a list of 1400+ store names in a spread sheet but they did not have their corresponding web site urls which I needed to perform a task. I could have waited for one of the writers to complete this manual and tedious task but I was adamant of a programmatic way to achieve this repetitive work.

The above was solved in PHP with the help of Google AJAX Search API. Since I am a big proponent of Python, here is the equivalent Pythonic version:

import json, urllib

BASE = 'http://ajax.googleapis.com/ajax/services/search/web?v=1.0&'
keywords = 'django'

url = BASE + urllib.urlencode({'q': keywords})
results = json.load(urllib.urlopen(url))
response_data = results['responseData']
results = response_data['results']

# Show the url from the first search result
print results[0]['visibleUrl']

Not all of the search results gave me the exact url for the store names but 98% of the job was done automatically, the suspicious ones can be eyeballed and corrected.