Showing posts with label python. Show all posts
Showing posts with label python. Show all posts

Enabling gzip compression in django

GZip compression allows the server to compress data before it is sent (on the fly) and the upon arrival of the data on client side, the browser does the unzipping/decompression of the data and shows it to the user. Enabling gzip compression can help you save about 50% plus bandwidth, thus not only making your site more responsive, but also saving huge amount of cost. Here's how you enable gzip compression in Django:

Open your settings.py file and find the MIDDLEWARE_CLASSES tuple. Add the following at the top:

MIDDLEWARE_CLASSES = (
    'django.middleware.gzip.GZipMiddleware',
    # ...
)

And, that's it! You can also enable gzip compression from your Apache, Ngingx and other servers too, look up their documentation.

Learning Django - 2

This is the second post among a series of posts about my django learning experience.

I'll start clean and set things up for the whole development process. I'll be using, Ubuntu, Python 3 (https://www.python.org/downloads), virtualenv (https://virtualenv.pypa.io) so that I don't have any dependency conflicts with my other projects, pip for installing these tools and obviously django. Considering that I have the latest python 3 installed (pip comes packaged by default), I ran the following command ($ denotes a terminal session):

$ # Use the sudo command if necessary (only applicable for *nix systems, n/a for windows)
$ [sudo] pip install virtualenv
$ virtualenv virtual_folder
$ cd virtual_folder
$ source bin/activate

This installed virtualenv (short for Virtual Environment) and created a directory named virtual_folder for holding the new virtual environment. cd-ed into it and activated the virtual environment. Deactivating is easy, just run deactivate and it'll deactivate itself. Next, I'll install django with this command:

$ [sudo] pip install django

After django is installed, a terminal restart was needed to load in the new binary. Then I created a project named website with this:

$ django-admin startproject website

A new directory named website is created in the current directory. This contains all the necessary backbone for running a simple django server. cd (change the terminal's directory to point to it) into it. Now, the initial configuration is done and its time for a test run! Run this command:

$ python manage.py runserver

And it should start a server in the localhost at port 8000 or whichever is printed in your terminal. Visit: http://127.0.0.1:8000/ to view the new django server up and running! I'll be explaining in short, what each file is and why its necessary in my next post.

This is it for the initial setups.

Learning Django - 1

I'll be writing a series of posts as I continue my journey to learning django from scratch (note, I already know to some extent and use django but I felt like I had a gap somewhere in the middle). So, this is the first post of the a series of posts I'm going to write about django as I continue my journey learning it!

First, what is Django anyway? Django is a web (application) development framework for the Python programming language. It's available at https://www.djangoproject.com
As of writing this, the latest available version is 1.11.3 which I'll be using over the course of my learning path. Some of the prominent features of django include security right from the start, scalability (Instagram runs on Django, although there are other technologies in their stack for managing different parts of the whole infrastructure), built in ORM for database access, built in template system, etc. Django's philosophy towards development is the MVC (Model View Controller) pattern as described here in detail: https://docs.djangoproject.com/en/1.11/faq/general/#django-appears-to-be-a-mvc-framework-but-you-call-the-controller-the-view-and-the-view-the-template-how-come-you-don-t-use-the-standard-names

That's all for the first post!

Increase the volume!!!

If you're running a linux system and having real trouble in hearing the sound perfectly, maybe you wanted to increase the sound beyond what the system allows (i.e more than 100%). This is where, the "setvolume.py" comes in :)

You use the script like so:
First of all, make the script executable with the command: "chmod +x setvolume.py"
Then "setvolume 70" to set the volume to 70% or "setvolume 150" to set the volume to 150%.

Besides, if you're using a terminal, you know, grabbing the mouse is a pain sometimes. You can use this script to change the system volume through the terminal (and possibly making it globally executable from any location) :D

Why wait, grab the script from here: https://github.com/sazid/setvolume

Power function

In C/C++ (and also other languages), we have the built in power function to raise a number to a given power. What if we need to calculate ...