Archive for October, 2009

Getting Distinct Result From Google App Engine GQL Query

October 24th, 2009

One of the big limitations of Google App Engine is query syntax. Like SQL we don’t have all freedom in GQL. As an example, we cannot get DISTINCT result like following SQL query…

“Select DISTINCT name from users”

Let’s say we want to select distinct city from our datastore in Google App Engine application
How can we do it ?
One of the solutions could be using set() function
Example:

>>> basket = ['apple', 'orange', 'apple', 'pear', 'orange', 'banana']
>>> fruit = set(basket) # create a set without duplicates
>>> fruit
set(['orange', 'pear', 'apple', 'banana'])


This is possible in Python and GAE….but remember first you have to create an array which required a loop to run and then use the set() function. Certainly, it can’t be a good solution. 

Here is a better way, let’s create a custom function that will return unique result or distinct result. 

  def unique_result(array):
    unique_results = []
    for obj in array:
        if obj.city not in unique_results:
            unique_results.append(obj.city)
    return unique_results


This function will return distinct result from an array result. 

Example : (how to call it…)

result = db.GqlQuery(“SELECT * FROM table_name order by date DESC limit x”)
distinct_result = unique_result(result)

Well, so far i think this is the better way to get distinct data in GAE GQL query. Let me know if you have any better way for it. I will update it here :)

Google App Engine Server Side Validation Examples

October 21st, 2009
Google app engine uses “Django Form Validation Framework” for server side validation. I would like to show some examples on server side validation for Google app engine. So let’s have a look at the following validation rules we are going to try…
  • Email validation 
  • Number validation 
  • Empty check
  • Input length  
  • Login check

We will use default values and property class in the datastore model and “is_valid()” will make sure there is no input errors by users. First, we specify the Python module imports and Google App Engine imports for our application. Please note that we must import google.appengine.webapp.template before importing any Django modules
Example: 

import cgi
from django.core.paginator import InvalidPage
from django.core.paginator import ObjectPaginator
from google.appengine.api import users
from google.appengine.ext import db
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
from google.appengine.ext.db import djangoforms

and next to create the Database Model….

class Test(db.Model):
   name = db.UserProperty()
   message = db.StringProperty(required=True)
   quantity = db.IntegerProperty(default=1)
   date = db.DateTimeProperty(auto_now_add=True)
   price = db.FloatProperty()
   email = db.EmailProperty()

Now, open main.py where you are saving your data. we need to create the form object based on the model….

class TestForm(djangoforms.ModelForm):
  class Meta:
    model = Test

 Before saving data, i’m assuming you have created your request handler form (e.g test.html)

def post(self):
    data = TestForm(data=self.request.POST) 

  if data.is_valid():
      save()
      self.redirect(‘/’)

            else:
                url = users.create_login_url(‘/’)
                self.response.out.write(‘You suck! enter some valid data’)

So what we are doing before inserting our input data in the datastore? As an example, we said quantity is an integer so it’s input value must be a number, message field cannot be empty as we set “required=true”. Like these we have set email property, date etc that will be checked by is_valid as it defined. But what about chars length and user logged in check?

we can do it like the following….

def post(self):
        user = users.get_current_user()
        msg = self.request.get(‘msg’)
        if len(msg)<140 and user:
            do_something()
            self.redirect(‘/’)
        else:
            url = users.create_login_url(‘/’)
            self.response.out.write(‘Input length checked and loggied in user not found!’)

to check types and property classes have a look here 

I’m Not Using Posterous to Blog on WordPress as It Creating Duplicate Content

October 17th, 2009

I was exited when i figured out that i could use email to post blog on my WP blog using posterous. Though i could have used email to post on WP from it’s own setting but i noticed that it was too complicated and there weren’t all features available like a rich text editor could provide. As an example, you can’t use inline image and you always have to use plain text if you post via email from WP’s email settings. That sucks! But why i was so interested to use email instead of WP’s own editor? Well, i just don’t like that editor and i feel more comfortable to use email or a desktop blog editor client. If i was a windows user i could use “Windows Live Writer” for sure. But linux doesn’t have that much good desktop blog editor client. Anyways, let’s come to the point….

So, why i’m not gonna use Posterous to post on WP blog anymore? First i thought maybe it just posts on my WP blog when i use it like #junalontherun@posterous.com but no, i was wrong. It posted on my posterous blog as well. I searched their help forum to get rid of this duplicate content but didn’t find any solution. So, i emailed to their help center and they replied immediately saying that posterous will always post the blog or whatever on it’s own site and i can’t remove it. What the hell? 
Why i’m worried about this duplicate content? Well, i don’t want to destroy my own blog page rank for using a cool service. I searched google web master forums and tried to understand how Google Crowler actually handles duplicate contents. Interesting thing is, i found 100s posts related to duplicate content …seems like webmasters are really worried about it? But this post says ”There’s no such thing as a “duplicate content penalty. There are some penalties that are related to the idea of having the same content as another site—for example, if you’re scraping content from other sites and republishing it, or if you republish content without adding any additional value. These tactics are clearly outlined (and discouraged) in our webmaster guide
So this is something discouraged by Google eh. and well, don’t forget duplicate content can make your readers confused specially when you are posting exact thing in 2 different locations. I just wanted to find an alternative of posterous email service and found this cool tool called “Thingamablog“,  I haven’t tried it yet but i believe it will meet lots of fetures that Windows Live Writer provides. But for me i found another solution
What is that?
Good docs! Screenshot is kinda self explanatory but still if you want to know more about it then have a look here 

Now what, i can use all the features of Google Docs? Or you think i have better solution? Then tell me :)  

Skype 2.1 Sound Setup for Ubuntu:Jaunty

October 11th, 2009

I had almost similar problem on Intrpid Ibex, and now faced it on Jaunty to setup sound properly for Skype 2.1. What was the problem? I wasn’t able to hear though receiver was able to hear me clearly. New Skype for Linux 2.1 supports PulseAudio. This is certainly a blessings as PulseAudio is known as a standard sound server. But that was my main problem! In skype sound device option, i had only one option for sound In/Out and that was “PulseAudio server (Local)”. I wanted to get rid of that (PulseAudio) so that i could use ALSA alternatively. Thought that would fix the problem and it did!

So i had 2 options to get rid of PulseAudio, i have to uninstall or stop it to get automatically selected on Skype. Instead of uninstalling i did stop the auto selection…

gedit ~/.pulse/client.conf

#and then set

autospawn = no

save and exit. We are safe from pulseaudio now. And new Skype when it doesn’t get the pulseaudio it uses alsa. Are we done here to get rid of this problem? Not really, if you are using a fresh copy of Jaunty then have a look here and do some important updates/install. Then we are fine .. (well, at least for me it worked)
Let’s have a look at my sound settings from the screenshots. Hope it helps if you are facing same problem like me…..

Some helpful links:
http://ubuntuforums.org/showthread.php?t=789578
http://idyllictux.wordpress.com/2009/04/21/ubuntu-904-jaunty-keeping-the-beast-pulseaudio-at-bay/
Some helpful links:
http://ubuntuforums.org/showthread.php?t=789578
http://idyllictux.wordpress.com/2009/04/21/ubuntu-904-jaunty-keeping-the-beast-pulseaudio-at-bay/

Posted via email from junal’s posterous

Web is My Computer

October 10th, 2009

Day before yesterday, i lost all data from my computer that i saved by last 5/6 years! How? I was using this tool called GParted on Ubuntu to add some free space on existing drive. That wasn’t logical at all, but why i had curiosity? if you can do that in Vista or XP then why can’t you do the same on Linux. There was a solution, that you copy the existing drive to an unallocated space and then partition the original drive and then add the space and later get the files back from where you copied files to. Instead of doing that, unintentionally, i clicked on “partition table” and clicked on “Apply” when it clearly says “this will erase ALL the data from your /dev/sda” drive! I don’t know maybe i was drunk or something….i read it! That was all my fault. No blame to anyone.

Then what happened? My whole drive became “Unallocated” – meaning i broke the whole partition of the HD. I stared at the screen for at least 2 minutes, what? that means i lost all the data? Maybe, if i tried i could have recovered some data…but then waited and i thought what i lost from my computer…..
I had, important documents, my projects! tools, Music, tutorials etc….
Now, let me tell you why i said “Web is my computer


Documents:: I saved most of the important documents on Google Docs. Documents that i couldn’t upload for limit on Google docs, i uploaded them on Windows Live SkyDrive. Same goes for Pictures, my all memorable pictures are saved on SkyDrive.


My projects:: Most of them are stored on remote servers. Just needed to get the FTP/SFTPs access info that i saved on Gmail.


Tools: As i’m a linux user, i didn’t worry about any saved softwares or anything. Just reinstalled Ubuntu and used Synaptic or command prompt to get all the software i used. But some tool like Tweetdeck where i had columns of groups? yes, thankfully i had account on tweetdeck where i synchronized my columns. Just used the accounts and got all columns back :)


Music:: Unfortunately, i dint save them anywhere. There were around 10 Gb music in my HD, this is one think that made me upset cuz i had my all favorite musics. But again, it doesn’t let me worry that much when i think that i can listen to music online from Internet radio channel like radiodhaka.net. Plus i can get these songs from my friends, of course :)


Tutorials/Links:: My twitter/Friendfeed and bookmarking tools are there to save my important links. And when i get something interesting i Favorite them on twitter which is kinda like special bookmark for me :)  There wasn’t any worries for my important links.


Ok….just spent 2/3 hrs to get all these downloaded and updated. Other then that, i actually didn’t loss anything big.
Lesson learned? Do stuff more on web, save them online…..there are lots of tools to do that. Such as, DropBox, SkyDrive, Box…etc. But personally, im waiting for Google’s GDrive. Probably it will come out with Google Chrome OS? i’m not sure but i’m preparing myself for Google Chrome OS, what about you?

Posted via email from junal’s posterous

Get Adobe Flash playerPlugin by wpburn.com wordpress themes