Archive for the ‘Python’ category

Welcoming All Bangladeshis to “Amra Positive”

November 8th, 2009

I promised to release “Amra Positive” soon, today i’m announcing the release of this small application that is developed for Bangladeshis. Let me give you some ideas about the application first…. [i'm just coping the “about” page from the application]

“amra positive” is a place for Bangladeshis to leave good and positive news about Bangladesh, friends, family, events etc. no pressure, but it’s up to you to leave something good behind within 150 characters. there is no moderator to modulate your posts in this application. if you don’t like to post or don’t have anything to say, you can still vote on others; posts. so let’s have a look what you can do here…

  • you can post a message about the country, your friends or family, events that you liked or think is positive

  • you can vote on your own posts and others’ posts. currently there is no commenting system. you can only vote positive or negative about a post.

  • you can tweet a post if you like. links will be automatically shortened, you don’t have to worry about that.

  • you can check posts that were voted the most positive from users from “top10″ tab

  • as an owner of the post, you have access to delete it

  • you can check all your past/previous posts by clicking the “authors” tab or by clicking your nick name

  • clicking on a message will show that specific post in an individual page

  • you have to use your gmail id to sign in. please note: that password is not shared with the admin of this application, only the gmail id is shared. so there is nothing to worry about

  • all text in this site in lowercase to make it look a little bit more different.

    Home page of Amra Positive

    Home page of Amra Positive

in addition, you can subscribe to the most recent and top 10 posts by clicking on rss button. there is currently no feedback system right now. but if you have any feedback, comments, suggestions or concerns just email me at junal@junalontherun.com

p.s : this is a beta version of this application. looking forward to hearing from you. report any bugs in the application and let me know whatever is on your mind by email. thanks for your patience and support with this idea!

Link : http://amrapositive.appspot.com/

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 

Installing Google App Engine SDK on Ubuntu

January 12th, 2009

Installing Google app engine SDK on Windows and Mac OS X is very easy as they have installation launcher, but for Linux and other system we have Google App Engine SDK in a zip format which has to be installed manually. Not a big deal but few step that requires to get it done.

Let’s get them step by step

  1. Download Google App Engine for linux from here and Unzip It in your selected drive (for me the unzipped location is here /opt/lampp/htdocs/google_appengine)
  2. Create a new folder inside the directory and give it a name (for me /junaltest)
  3. Create 2 files with the following name app.yaml & yourfilename.py (for me junaltest.py). Yaml file describes which App Engine runtime environment the application uses. My app.yaml file contains the following lines
application: junaltest
version: 1
runtime: python
api_version: 1
handlers:
- url: /.*
  script: junaltest.py

and junaltest.py looks like the follow [for a simple hello world]

print 'Content-Type: text/plain'
print ''
print 'Hello, World'
  1. Open the command prompt and run the following command dev_appserver.py [options] <application root> (i.e. python appcfg.py /opt/lampp/htdocs/google_appengine/junaltest/)
  2. Once you successfully run the command you can visit here on your local machine  http://localhost:8080/

now, you are supposed to see the text “Hello, World” on your browser. We are done here but for further clearance let’s open and account on Google App Engine from here and let’s upload a file using command line. (assuming you have a gmail id)

please note that, application identifier is the application id. So give it a name that you can use it in the yaml file. Im keeping the same name for application identifier as “junaltest”.

Now, let change or do something on junaltest.py file and let’s upload it to the Google Server using the following command

dev_ appcfg.py update <application root>

(i.e. python appcfg.py update /opt/lampp/htdocs/google_appengine/junaltest/)

now it will ask you for your gmail id and password. Give it correctly and then check your application page from here http://appengine.google.com/

bingo! One file is uploaded! Check it, see if it’s showing what you gave :) ask me if you have any questions.

Get Adobe Flash playerPlugin by wpburn.com wordpress themes