Sunday, 16 December 2012
Monday, 10 December 2012
Python Webapp2 framework and Users Services
Webapp2 is a simple web application framework included in the App Engine.
User Services
A webapp2 application has two parts:
* RequestHandler classes that process requests and build responses.
* WSGIApplication instance that routes incoming requests to handlers based
on the URL.
This is a code which defines a simple request handler.
import webapp2
class MainPage(webapp2.RequestHandler):
def get(self):
self.response.headers['Content-Type'] = 'text/plain'
self.response.out.write('Hello, webapp World!')
app = webapp2.WSGIApplication([('/', MainPage)],
debug=True)
Here MainPage is mapped to the root URL (). The get method sets properties on self.response to prepare the response, then exits. webapp2 sends a response based on the final state of the MainPage instance.
import webapp2
from google.appengine.api import users
class MainPage(webapp2.RequestHandler):
def get(self):
user = users.get_current_user()
if user:
self.response.headers['Content-Type'] = 'text/plain'
self.response.out.write('Hello, ' + user.nickname())
else:
self.redirect(users.create_login_url(self.request.uri))
app = webapp2.WSGIApplication([('/', MainPage)],
debug=True)
If the user is already signed in to your application, get_current_user() returns the User object for the user. Otherwise, it returns None. If the user has signed in, display a personalized message using the user.nickname(). If the user has not signed in, tell webapp2 to redirect the user's browser to the Google account sign-in screen.
[Expecting Your Valuable Comments]
Thank You
Iterator, Generator and List Compression
Iterator
An iterator is an object representing a stream of data and this object returns the data one element at a time. A Python iterator needs to support a method called __next()__ .This next () method takes no arguments and always returns the next element of the stream. If there are no more elements in the stream, __next()__ must raise StopIteration exception.
Here is the example:
num= []
i = 0
for item in range(10):
i = i + 4
num.append(i)
print num
a = iter(num)
print a.next()
print a.next()
Output
[4, 8, 12, 16, 20, 24, 28, 32, 36, 40]
4
8
>>> a.next()
12
>>> a.next()
16
>>> a.next()
20
Generator is a function which controls the iterations, stop at a point, save the context and resume from where it stopped last. A generator can generate a sequence of numbers and yields the result one at a time. Since generators return one value at a time they take up less memory and behave similar to an iterator. Generators are usually evoked in a loop.
Here is the simple example:-
def fibonacci():
a = 0
b = 1
yield a
yield b
while 1:
c = a + b
a, b = b, c
yield c
d = fibonacci()
List Compression
List comprehension is a concise way of creating lists. We use list compression when we need to define operations that
are to be applied to each element in the list. It simplifies the code,
where we have to use multiple statements to get desired output to a more
shorter form.
for a in range(10):
square.append(a*a)
Here, this can be also done with single line code as given below:
square = [a*a for a in range(10)]
[Expecting Your Valuable Comments]
Thank You
Saturday, 8 December 2012
Functional Programming
Functional programming is a programming paradigm that treats calculations and computations as the evaluation of functions rather than state.
Functional languages, I/O, assignments, etc., are completely avoided in this FP. In Python, however, we do not avoid them completely. Instead, a functional like interface is provided . For example, local variables are used inside the function, but global variables outside the function will not be modified.
Functional programming can be regarded as the opposite of object-oriented programming. Objects are entities that provide functions to modify internal variables. Functional programming aims at the complete elimination of state, and works with the data flow between functions.
In Python, it is possible to combine object-oriented and functional programming. For example, functions could receive and return instances of objects.
Theoretical and practical advantages to the functional style:
1. Formal provability.
2. Modularity.
3. Composability.
4. Ease of debugging and testing.
1. Formal provability.
2. Modularity.
3. Composability.
4. Ease of debugging and testing.
[Expecting Your Valuable Comments]
Thank You
Friday, 7 December 2012
Google App Engine
Google App Engine lets us to run web applications on Google's infrastructure.
We can serve our app from our own domain name using Google Apps. Or, we can serve our app using a free name on the appspot.com domain. Through this Google Apps, we can share our application with our friends, organizations and with the whole world.
App Engine Features:
1. Dynamic web serving, with full support for web technologies
2. Persistent storage with queries, sorting and transactions
3. Automatic scaling and load balancing.
4. APIs for authenticating users and sending email using Google Accounts.
5. A fully featured local development environment that simulates Google App
Engine on your computer.
6. Task queues for performing work outside of the scope of a web request.
7. Scheduled tasks for triggering events at specified times and regular intervals.
Google Apps can run in one of three runtime environments: the Go environment, the Java environment, and the Python environment.
Python Runtime Environment
With Google App Engine's Python runtime environment, we can implement our app using the Python programming language. App Engine includes rich APIs and tools for Python web application development. We can also take advantage of a wide variety of mature libraries and frameworks for Python web application development.
Create your first Google App engine project
Before we do anything else, you’ll need to install the Google App Engine SDK. You’ll need Python 2.5 too. You won’t be writing any Python code but the App Engine SDK will need it to run on your computer.
You’ll need to choose a unique ‘application id’ nothing more than a name for your project. Make sure it consists only of lowercase letters and numbers.
On your computer, create a folder named after your application id.
Create a file app.yaml in that folder. It tells App Engine what to do with your files.And we need to add these lines.
application: hunterstimer
version: 1
api_version: 1
runtime: python
handlers:
- url: /.*
script: main.py
Here main.py is the python file where we access our web application file, for example "hunter.html".
For this, we need to add these lines to main.py file.
import wsgiref.handlers
from google.appengine.ext import webapp
from google.appengine.ext.webapp import Request
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext.webapp import template
class main1(webapp.RequestHandler):
def get(self):
self.response.out.write(template.render("hunter.html",{}))
def main():
app = webapp.WSGIApplication([
(r'.*',main1)], debug=True)
wsgiref.handlers.CGIHandler().run(app)
if __name__ == "__main__":
main()
Where the hunter.html file can be replaced with your application file and place that file in the application id folder.
Registering the Application
Sign in to App Engine using your Google account here.
To create a new application, click the "Create an Application" button. Follow the instructions to register an application ID, a name unique to this application. If you elect to use the free appspot.com domain name, the full URL for the application will be
http://your_app_id.appspot.com/.
Edit the app.yaml
file, then change the value of the application:
setting to your registered application ID.
Testing the added appllication
Start the web server with the following command:
google_appengine/dev_appserver.py "path to applicationid folder"
Now you can test the application by visiting the following URL :
http://localhost:8080/
Uploading the Application
appcfg.py update "path to applicationid folder"
Enter your Google username and password at the prompts.
You can now see your application running on App Engine with the url http://your_app_id.appspot.com.
Click here to see an example:
[Expecting Your Valuable Comments]
Thank You
Wednesday, 5 December 2012
CountDown Timer
A countdown is a sequence of counting backward to indicate the seconds, days, or other time units remaining before an event occurs or a deadline expires.
Here is the countdown Timer:
Enter the minute and second in the text box and click set to Set it as the initial value and click Start.
If its not working in your browser properly, Try this timer.
Click here to get the code.
[Expecting Your Valuable Comments]
Thank You
Subscribe to:
Posts (Atom)