Web programming with Python - Django Introduction
Introduction Python’s CGI library is good enough for writing websites from ground up. But we need to put a lot of effort into it to build a usable and secure website. When building a web application we often come across some common tasks like authenticating users, connecting to a database, sending emails to users, a reliable admin interface to manage and add content to our site, etc. If we solve all of these problems ourselves, it is time-consuming.
Deploying Django Application with Nginx and uwsgi
Introduction In this post we will learn about deploying Django applications. There are many ways to deploy a Django application. we can use either nginx or apache web server. Nginx is just a web server. It doesn’t know how to run Python programs. For this, we need a server which understands how to run python programs. This server is called application server. To run a application server, we can use uwsgi, gunicorn and etc.
Web programming with Python - Part 2 - CGI
If you have read the previous post, we stopped after introducing web servers. In this post, we write very simple web application using cgi programming.
CGI stands for Common Gateway Interface. Let’s say we received a web request, and we want to process that request with Python. CGI is basically a script that is run by the web server whenever a request is received. The script can be a Python script or a Shell script or Ruby script.
Web programming with Python - Part 1 - Web Servers
Introduction Web programming or web development is a term that broadly means developing websites. Developing websites mainly consists of two parts.
Front end development. Back end development. Front end includes html, javascript and css. We can also say it is the creativity part. If you are more interested in creating beautiful websites, this is the path you should take. Backend involves in server side development including application business logic, database design and development, server deployment and maintenance and etc.
Reduce memory usage in Python using slots
slots is a kind of Python magic that reduces memory usage of a program. It’s useful only when we have a lot objects with fixed number of attributes. For example let’s assume we have a online shopping website. whenever a user adds a item to the cart, we create a object for that item. For example,
1 2 3 4 5 class CartItem(object): def __init__(self, item_id, added_time): self._item_id = item_id self.
Python List Comprehensions and generator expressions
Comprehensions are one of the prominent features of Python. Once you understand them, it helps avoiding lots of redundant and repetitive code. It helps creating lists, dictionaries and generators from other iterable objects with a single statement.
List Comprehensions Let’s take the below tuple as an example,
1 2 3 4 5 6 scores = ( ('Science', 85), ('Phisics', 65), ('Mathematics', 89), ('Computer Science', 34) ) To get all the subjects from this tuple,
Crawling web pages using Python and Scrapy - Tutorial
In this post, let us walk through how we can crawl web pages using Scrapy.
For this tutorial, we will download all the excerpts and ebooks available in https://www.goodreads.com/ebooks?sort=popular_books. This page is paginated. Let’s download books from first page only. At the end of this post, you will know how to follow and crawl other pages too.
First lets create a python virtual environment called goodreads.
1 2 mkvirtualenv goodreads workon goodreads To know more about how mkvirtualenv and workon commands work, visit and install virtualenvwrapper
Static Site Generators
Static site generators are tools that generates static sites i.e. pure html websites without backend data processing, session management or user authentication system. It should be well enough for running a blog.
How static site generators work?
Write your posts in markdown or rst. Run the static site generator to parse these posts into html. Upload the generated output to any server that serves html (or even host it in github pages).
Python profiling
There are three main modules that provides time benchmarking in python.
timeit - call it for profiling small python statements. (check out timeit module) cProfile - c extension for profiling. Profile - pure python implementation of cProfile. It exposes the same functions and classes of cProfile. code:
:::python import cProfile from collections import deque p = cProfile.Profile() # create a new profile object d = deque() li = [] p.enable() # start profiling.