How to create you own Gmail like loader / progress bar with Jquery

Sometimes, It is necessary to create progress bar or loader to show user that something happening in the background. Loader/loading icons are good way to do this but If you want your user to show actual progress then something else is needed. A progress bar which shows how much task has been completed and how much left.

Gmail has done some remarkable job in this. Since, Gmail loader is simple and elegant in looks. And, It is really simple to create a Gmail like loader.

What you need is two divs. One div will treated like container, which will filled by other div to show progression of the task. So, outer div will contain border only and shows totality of the task, where as internal div will contain background color, it will give sense of filling the outer div. And, when some part of task happened we will increase this inner div’s size. So that, it fills outer div and give sense to user that we had done something and hence increase the progress.

Here is the example to do Gmail like loading feature,

<!DOCTYPE>
<html>
    <head>
        <title>Gmail like loader</title>
		<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
		<script>
			function callMe(inc)
			{
				inc</span> = inc || 10
				var w = parseInt((parseInt($("#inner").css("width"))/parseInt($("#inner").parent().css("width")))*100)+ inc;
				$("#inner").width(w+"%");
				if(w>100)$("#inner").width(0);
			}
			
			$(document).ready(function(){
				var id = setInterval(callMe,1000);
			});
			
		</script>
    </head>
    <body>
		loading...
		<div id="outer" style="border:1px solid;width:200px;height:10px;overflow:hidden;">
			<div id="inner" style="background-color:#0F67A1;height:100%;width:0%;"/>
		</div>
    </body>
</html> 

I had two divs, outer and inner. outer had border and inner had background color. In outer we had used overflow:hidden to hide inner div progression if it increases beyond 100% (length/width of the outer div). I had to use interval to show some progress.

Update: Demo
http://jsfiddle.net/gagan/62CZZ/embedded/result/

Converting Simple XML to JSON in Python

Converting simple XML to JSON with some Google like extensions (http://code.google.com/apis/gdata/docs/json.html) like $t for text content and replace ‘:’ in element name with ‘$’. It is fairly simple here is how I do it.

import xml.parsers.expat
import sys,string

# 3 handler functions
def start_element(name, attrs):
    #sys.stdout.write 'Start element:', name, attrs
	sys.stdout.write('{"'+string.replace(name,':','$')+'":{')
	for attr in attrs.keys():
		sys.stdout.write('"'+attr+'":"'+attrs[attr]+'",')
def end_element(name):
    #sys.stdout.write 'End element:', name
	sys.stdout.write('}')
def char_data(data):
    #sys.stdout.write 'Character data:', repr(data)
	if data and (data!=""):
		sys.stdout.write( '"$t":{"'+data+'"}' )

p = xml.parsers.expat.ParserCreate()

p.StartElementHandler = start_element
p.EndElementHandler = end_element
p.CharacterDataHandler = char_data

p.Parse("""<?xml version="1.0"?>
<ns:parent id="top"><child1 name="paul">Text goes here</child1><child2 name="fred">More text</child2></ns:parent>""", 1)

Google App Engine : Full Text Search support in pre-release version

Full Text Search in Google’s app engine is what limiting me from adopting app engine at a first place, may be others feels this as limitation too. But, Then something interesting happened two Google guys presented in Google IO the upcoming feature of the App Engine – Full Text Search. They presented what I needed most. They are Google they had to provide search capabilities, that’s what issues reported by one of user. I didn’t hear any word from them since then about Full Text Search. May be they are planning big opening for this big feature.

But,  2-3 days earlier they released pre-released version in their opensource app engine SDK. But, they didn’t mention anywhere in the changes or release notes that it contain search capabilities. Yesterday, I explored the python SDK and found that there is new search libraries and apis in ext folder (google/appengine/ext/search). Then I created the document for python SDK through epydoc. And found this doc of search package (google.appengine.ext.search),
Full text indexing and search, implemented in pure python.
Defines a SearchableModel subclass of db.Model that supports full text indexing and search, based on the datastore's existing indexes.

Don't expect too much. First, there's no ranking, which is a killer drawback. There's also no exact phrase match, substring match, boolean operators, stemming, or other common full text search features. Finally, support for stop words (common words that are not indexed) is currently limited to English.

To be indexed, entities must be created and saved as SearchableModel instances, e.g.:


class Article(search.SearchableModel):
text = db.TextProperty()
...

article = Article(text=...)
article.save()

To search the full text index, use the SearchableModel.all() method to get an instance of SearchableModel.Query, which subclasses db.Query. Use its search() method to provide a search query, in addition to any other filters or sort orders, e.g.:


query = article.all().search('a search query').filter(...).order(...)
for result in query:
...

The full text index is stored in a property named __searchable_text_index.

Specifying multiple indexes and properties to index
---------------------------------------------------

By default, one index is created with all string properties. You can define multiple indexes and specify which properties should be indexed for each by overriding SearchableProperties() method of model.SearchableModel, for example:


class Article(search.SearchableModel):
@classmethod
def SearchableProperties(cls):
return [['book', 'author'], ['book']]

In this example, two indexes will be maintained - one that includes 'book' and 'author' properties, and another one for 'book' property only. They will be stored in properties named __searchable_text_index_book_author and __searchable_text_index_book respectively. Note that the index that includes all properties will not be created unless added explicitly like this:


@classmethod
def SearchableProperties(cls):
return [['book', 'author'], ['book'], search.ALL_PROPERTIES]

The default return value of SearchableProperties() is [search.ALL_PROPERTIES] (one index, all properties).

To search using a custom-defined index, pass its definition in 'properties' parameter of 'search':

Article.all().search('Lem', properties=['book', 'author'])

Note that the order of properties in the list matters.

Adding indexes to index.yaml
-----------------------------

In general, if you just want to provide full text search, you *don't* need to add any extra indexes to your index.yaml. However, if you want to use search() in a query *in addition to* an ancestor, filter, or sort order, you'll need to create an index in index.yaml with the __searchable_text_index property. For example:

- kind: Article
properties:
- name: __searchable_text_index
- name: date
direction: desc
...

Similarly, if you created a custom index (see above), use the name of the property it's stored in, e.g. __searchable_text_index_book_author.
Note that using SearchableModel will noticeable increase the latency of save() operations, since it writes an index row for each indexable word. This also means that the latency of save() will increase roughly with the size of the properties in a given entity. Caveat hacker!

Hoping this will help others and also encourage others to think and adopt Google app engine as more capable system to handle real world problems.

Python : Create directory if directory/folder(s) don’t exists already

In Python, I have to make sure, for quite a lot time, that some directory path exists before I can do some operation on that. So, here is the code in python that make sure some directory path or folder path exists before we do something on that directory.

It will create all the directories in the path, if they are not exists

import os, errno

def make_sure_dir_exists(path):
    if not os.path.exists(dir):
        try:
            os.makedirs(path)
        except OSError as exc: # Python &gt;2.5
            if exc.errno == errno.EEXIST:
                pass
            else: raise

Why big internet companies (Google, Facebook, Twitter, Yahoo, etc) going so much open source ?

If you were ever wondering why these big comapanies (Google, Facebook, Twitter, Yahoo, etc) going so much open source. There are various factors that contributed to this attitude of these new corporates. These factors are Cost of training, Delivery Time & Quality of delivery and ultimately assurance about replicating infrastructure at thier level needs lots of capital. Now each fators in detail, 
Cost of Training – Any big company goes into phases of attrition then new hires as norm not exception. So, they want new people to at least familiar with technologies they are working with, if not the whole process.  
Delivery Time & Quality of delivery – For these big internet brand names delivery time and Quality of any service or feature and addressing any critical issue in short span of time are crucial for their brand name. They are earning because of their brand name otherwise their are many pirates are out there. So, they need developes, who can write quality code in short time. And, these two things increase over time of developer association with technology used. Therefore, this is most important factor why these companies want developers to know about what they are working upon.
Assurace – BIG Internet companies are more about scale and process rather than trade secrets and softwares. they know their most of the client side code can be easily be duplicated. So, they concentrate on not on how to protect that but concentrate on scaling each product to a level where others can’t do that much of scaling. Even, scaling to that level is also possible but that is certainely very capital intensive. So, they are almost assure that their scaling cannot be challenged in near future.

Update: I asked same question to stackoverflow community and got pretty good answers http://programmers.stackexchange.com/questions/53788/why-big-internet-companies-google-facebook-twitter-etc-going-open-source-way

Python : Create path or directories, if not exist

I came across to this question very often, that how to create a directory structure, if I know the exact path of file or directory hierarchy. That is not difficult but very time consuming if you don’t know where to find the resources. Let go to code we need,


def assure_path_exists(path):
        dir = os.path.dirname(path)
        if not os.path.exists(dir):
                os.makedirs(dir)

first line define python function/module assure_path_exists, which take one argument “path” of file or directory.
second line, ensure path contain directory only. it strips filename, if there is any.
third line, checks for whether this path (pointed by dir), exists or not
forth line, if path pointed by dir not exists, we create that path through os.makedirs

Done!!!

Within instant Google forgot basics of its embraced AJAX

I love Google, that is most of the developers says. Their day starts with Googling. Google defined standards of search engine industry. They embraced AJAX technology with Gmail, Google Docs etc.  They define standards of how to use AJAX effectively and efficiently. But, unfortunately today I found a usage of improper usage of AJAX in their webpage and that webpage is nothing but their Home page. Firstly, I didn’t believe my eyes, I redo it 4-5 times with different browsers then I believed it. They missed handling back button. They didn’t clear the result if back button reaches to their home page.

Here are the steps to reproduce it ( if after this post, they still didn’t correct it).

1. Go to Google.com, and type whatever query you have and press enter. In my case, I typed “Blender” as I am learning 3D now a days

2. You reach result page, everything remain normal

3. Press back button of your browser. OOPS!!! I reached google home page with background contains previous result.

After this, I thought might be this is problem with Chromium alone as it is nightly build of Chromium. So, I tried it in Firefox also. Thats test is also turn positive thats why I writing this 😉

1.

2.

3.

Note: It is big because it is Google’s Home page. Otherwise, who cares.
I feel sorry for Google guys as they have to come and fix this and its weekend already. sorry guys.

The speech by Steve Jobs at Standford’s in 2005 : Highly Motivational

This speech is highly motivational to me, when I first watched it in 2007. May be this could help you also. May be you need some information about Steve Jobs to understand this video.
Some takes for me from the speech,
  • Never stop on hurdles, it leads us to match dots when we look back to our life.
  • Love your work, and
  • Send positive energy to others and it also returned back to you.

Planned Security attack on Google and other companies, May lead to cease Google’s China operation

Yesterday, Google on its blog mentioned that it faced security attack in mid-December. And, some other organizations from different fields were targeted. Google tracked these attackers from China seeking gmail account information of some Human Right Activists of China. As blog mentioned These attacks were not as successful as it is intended to be. Google thinks that further restrict free speech on web for Chinese people. So, Now they will talk to Chinese officials to see how Google.cn can provide uncensored content to the people of China without violating Chinese laws. If there is no way out then Google may cease its Chinese operation Google.cn .

Some comments on Techcrunch says that Google is very far behind some Chinese search site Baidu.com. on Alexa’s Top sites, Baidu.com comes 8th and Google.cn comes 15th in the list. Baidu.com comes above Twitter and Myspace and many more. So, that lead to one another conclusion that Google may be tricking readers to get some extra market in China.
Whatever, may be the cause now Google cannot take a back from what they had done. If they
not backed what they write then it will definitely tarnish its image of good boy. But, if they not backed out and stop china operations that will hurt because China is very large market to any of the company and specially for Google.
Only, time will tell what is the behind the scene story.