Dictionary in Python3

Hi  all . Today we are gonna see dictionary in python3.

In python2.7

d= {"a": "b", "b": "c", "c": "d", "d": "e", "e": "f"}

d.keys() —> [‘a’, ‘c’, ‘b’, ‘e’, ‘d’]

d.values() —> [‘b’, ‘d’, ‘c’, ‘f’, ‘e’]

Here d.keys() returns list of keys, d.values() returns list of values respectively.

In Python3 . These functions return dictkeys and dict values

d= {"a": "b", "b": "c", "c": "d", "d": "e", "e": "f"}
d.keys() ---> dict_keys(['d', 'c', 'b', 'e', 'a'])
d.values() ---> dict_values(['e', 'd', 'c', 'f', 'b'])

However if we want same functionality as 2.7 we can get like this

list(d.keys()) ---> ['d', 'c', 'b', 'e', 'a']
list(d.values()) ---> ['e', 'd', 'c', 'f', 'b']

There is no dict.iteritems(), d.has_key() function in python3

d.items() ---> dict_items([('d', 'e'), ('c', 'd'), ('b', 'c'), ('e', 'f'), ('a', 'b')])
list(d.items()) ---> [('d', 'e'), ('c', 'd'), ('b', 'c'), ('e', 'f'), ('a', 'b')]

Instead has_key() function we can check like this

'a' in d.keys() --> True
'z' in d.keys() --> False

If key doesn’t exists it will return false.


d.pop(‘a’) –> pop item from dict by key

d.popitem() –> pops most recently inserted item in dict

d.clear() –> clears entire dictionary

d.setdefault(‘z’, 10) –> sets default value for ‘z’ to 10

d.get(‘z’) –> 10 # gets value of key ‘z’, returns none if key doesn’t exist.



How to get video files duration and resolution in python

Hi all,

Today i was about to find video file duration and resolution and few more info in python.

In debain based systems type this command in terminal


If this shows version , like this

avconv version 9.18-6:9.18-0ubuntu0.14.04.1, Copyright (c) 2000-2014 the Libav developers
built on Mar 16 2015 13:19:10 with gcc 4.8 (Ubuntu 4.8.2-19ubuntu1)
Hyper fast Audio and Video encoder
usage: avconv [options] [[infile options] -i infile]… {[outfile options] outfile}…

Use -h to get full help or, even better, run ‘man avconv’

If it says commond or package not found, then install this by

sudo apt-get install libav-tools

After successful installation, In terminal

avconv -i "filepath"  # give path to your video file

You will see output like this
    major_brand     : mp42
    minor_version   : 1
    compatible_brands: mp42mp41
    creation_time   : 2015-10-21 16:41:08
  Duration: 00:00:30.00, start: 0.000000, bitrate: 748 kb/s
    Stream #0.0(eng): Video: h264 (Constrained Baseline), yuv420p, 480×240, 488 kb/s, 25 fps, 25 tbr, 25 tbn
      creation_time   : 2015-10-21 16:41:08
    Stream #0.1(eng): Audio: aac, 48000 Hz, stereo, fltp, 255 kb/s
      creation_time   : 2015-10-21 16:41:08

You can see the resolution and duration highlighted my result.

Now lets see how to use this in python to get video resolution, duration.

The idea is simple , am gonna use subprocess and pipe the output

Here is the python code.

from subprocess import Popen, PIPE
import re

def getvideodetails(filepath):
    cmd = "avconv -i %s" % filepath
    p = Popen(cmd, shell=True, stdout=PIPE, stderr=PIPE)
    di = p.communicate()
    for line in di:
        if line.rfind("Duration") > 0:
            duration = re.findall("Duration: (\d+:\d+:[\d.]+)", line)[0]
        if line.rfind("Video") > 0:
            resolution = re.findall("(\d+x\d+)", line)[0]
    return duration,resolution

# call function with file path

This code can be found in gist too here

Thats it.

Happy coding!!!
Happy times !!!

MongoDB Basics

Hi all. Today we are going to see about mongodb basics.

what is it?

Mongodb is a No-Sql Database , document based database (closely dictionary in python , ruby).  Here you can read about it official documentation .

why is it used for?

It’s used when you no need to care about your schema . It means when you are using mysql , we have to have ID field integer, name varchar . Here it’s not needed .


open terminal (cntl+alt + T)

sudo apt-key adv –keyserver hkp://keyserver.ubuntu.com:80 –recv 7F0CEB10

sudo apt-get update

sudo apt-get install -y mongodb-org

Hope you have successfully installed mongo in your system.

if you’re having trouble see official site for trouble shooting here

Time to play with it :

type mongo and enter in your shell

you will see something like this

MongoDB shell version: 2.4.9
connecting to: test


To see all database:

show dbs 

it lists all databases. This is equal to show databases in MYSQL

To select a particular database

use databasename

To see all collections inside a database.

Note: In mongodb collections refers tables. we won’t call them as tables . It’s collections here.

show collections

this is equals to show tables in MYSQL

Let’s create a database called bala .

use bala

unlike MYSQL it won’t throw you error unknown database.

to check in which db you’re issue db command  in mongo shell.

it shows bala

now create a document in it.

db.mycollection.insert({“name”: “bala”})

what i did here?

db => bala

mycollection => our collection name

insert => operation name

{“name”: “bala”}  => data we are inserting into our collection

To see a document is inserted into our db , type this command

> db.mycollection.find()
{ “_id” : ObjectId(“55a6070cab3e0cf5c31434cf”), “name” : “bala” }

see our document is inserted, find returns all documents in a collection(table) .

Have you noticed _id in above output. Yes that’s automatically created by mongo . Its alpha numeric . Similar to id primary key in MYSQL. 

To see one document in a collection

> db.mycollection.findOne()
{ “_id” : ObjectId(“55a6070cab3e0cf5c31434cf”), “name” : “bala” }

Now try inserting other document

db.mycollection.insert({“name”: “hari”, “country”: “india”, “state”: “Haryana”, “age”: 20})

what we did ?

we gave country , state, haryana . so many keys .  what happened?

As i said it’s schema less database , it doesn’t care about schema.

now let’s go and check it out.

> db.mycollection.find()
{ “_id” : ObjectId(“55a6070cab3e0cf5c31434cf”), “name” : “bala” }
{ “_id” : ObjectId(“55a60976ab3e0cf5c31434d0”), “name” : “hari”, “country” : “india”, “state” : “Haryana”, “age” : 20 }

see it shows two documents in our collection. how cool is this .

To find a document by condition (by name , age or any field)

let’s see i want to see all document’s which have name “hari”

> db.mycollection.find({“name”: “hari”})
{ “_id” : ObjectId(“55a60976ab3e0cf5c31434d0”), “name” : “hari”, “country” : “india”, “state” : “Haryana”, “age” : 20 }

here i have only one document having name “hari”, if I ‘ve many then it will show all documents.

We can see particular field value also by condition

let’s say i want to see all names alone in my collection

> db.mycollection.find({}, {“name”:1})
{ “_id” : ObjectId(“55a6070cab3e0cf5c31434cf”), “name” : “bala” }
{ “_id” : ObjectId(“55a60976ab3e0cf5c31434d0”), “name” : “hari” }

{} => empty dict to select all documents

“name”:1 => select only name from all documents

we can select  as many documents as possible

fieldname: 1  => selects the field to result.

fieldname:0  => ignores the filed to result.

Here instead of { } , we can give condition also .

Try it out those commands also.

Let’s update a document named “bala” , adding age field into it

> db.mycollection.update({“name”: “bala”}, {$set : {“age”: 20}})

$set is used to update field

let’s see it’s updated or not.

> db.mycollection.find({“name”: “bala”})
{ “_id” : ObjectId(“55a6070cab3e0cf5c31434cf”), “age” : 20, “name” : “bala” }

its updated , age field added into it. if we have many documents named “bala” then all got updated by above command.

till now we seen creation,insertion, updation.

Let’s move on to deletion of a document

To delete a document in collection

> db.mycollection.remove({“name”: “bala”})
> db.mycollection.find({“name”: “bala”})

first command deleted the document, second one verifies that we don’t have document name “bala”.

To remove all documents in a collection


This command removes all document in a collection.

That’s all about mongodb basic (curd operations) .  Try playing with that , we will see with advanced commands or integration with any language (python or ruby) later .


Thanks for reading . Happy hacking .




Nginx to serve static files example

Hi all .. Today we ll see how to use nginx to serve static files .

what is Nginx?

Niginx is a high performence web server software. Its more flexible and lightweight than apache. Niginx is a reverse proxy protocol.

wait a minute.. what is meant by reverse proxy ?

lets say x- a client and y –  a  server


x sends request to server [this is direct proxy]

in reverse proxy

x sends request to server but request is sent to some internal proxy and that proxy passes request to server and sends response from server to the client. The client may not be aware of this.

In short, requests are not directly processed by server some intermediate guy will process requests and responses.

x->z->y   [here z is that intermediate guy]

Got something ??

Lets see the installation of nginx

1. open Terminal (cntrl + alt +T)

2. sudo apt-get install nginx

Lets play with nginx now .

To play ,lets create one directory

mkdir /cache

cd /cache/

chmod 777 /cache/   

lets create some dummy files in this directory

 cat >>hi.txt

hi this is test file

this is line

cntrl +  d  [press contrl + d to save this file]

cat >> second.txt

this is my second file

this is testing second file

press contrl + d to save this file .

Now ,its time create a sample configuration file to server this directory.

Before we write ,its useful  to know about two import directory in nginx

1. sites-enabled

cd /etc/nginx/sites-enabled/  

This directory contains symlink files from sites-available directory

[we can create files here but don’t do that it’s not a good way to do]

2. sites-available

cd /etc/nginx/sites-available/

here all the config files are there for nginx

make sure your inside sites-available directory else

do cd /etc/nginx/sites-available/

touch sample.conf

open this sample.conf in your favorite  editor  and type the below code

server {
    listen                *:9000;
    server_name          _;
    access_log            /var/log/sample.access.log;  # this line is optional for log purpose
    error_log             /var/log/sample.error.log;  # this line is optional

    location /test/ {
        alias /cache/;
        autoindex on; 
        expires off;
        default_type application/octet-stream;

Lets see ,

listen *:9000  –> server listens to the port 9000

server name  – is optinal [you can give your domain name like http://www.example.com,]

next two lines are to log who accesses files and error details

next loctaion /test/{

telling nginx -x to server this location

alias  /cache/     —> /cache directory can  be accessed using /test/

auto index on —> to list all the files inside directory [ not recommended ,because user will know all the files inside your directory ]

expires off  [ this disables caching files ]

default_type application/octet-stream   –> this sets ,whenever the user visits this page it serves the content of the file .[asks for open or download]. It never opens in browser

if you want to just view in browser not to download

just comment that last line which contains octet-stream

just save this file now

now lets create a symlink to sites-enable

It will be like this

sudo ln -s   /etc/nginx/sites-available/sample.conf    /etc/nginx/sites-enabled/

Note: Always use absolute path . don’t use relative path like this sudo ln -s sample.conf /etc/nginx/sites-enabled/  — this will not work . Always use absolute path

ok lets restart nginx server

sudo service nginx restart

open your favorite browser and visit localhost:9000/test/

you will see like this

nginx auto index on

click on the any file [ note i commented octet-stream]

content view in browser


if you want file to be downloaded uncomment that last line . You browser will ask file to be opened or download


Thats it … Play with it .. Happy coding..:)













Start Using TMUX

Hi …all ,till few days ago I used screen in remote server . Now i switched to TMUX from screen. It has so many advantages and features as compared to screen .

what is Tmux?

Tmux is a software application that multiples several virtual consoles ,that allows us to use multiple terminal session into single terminal window.


apt-get install tmux

Lets start using and play with tmux

To start new session  open terminal and type


To attach to a session

tmux attach-session -t [session name]

To split the window into panes (two terminal session in same window )

cntrl+b  %  (splits vertically)

cntrl+b ”   (splits window horizontally)

To move from one pane to another pane

cntrl +b o

To open new window

cntrl +b c

To go to next window

cntrl +b n

To go to previous window

cntrl +b p

To kill current pane or window

cntrl + b x

To close all other pane expect current

cntrl + b !

To exit from shell

cntrl + b d

To list windows

cntrl +b w

To go to particular window(lets say #)

cntrl +b #

To go to last  active window

cntrl +b l

To list all the sessions

tmux list-sessions

To rename current window

cntrl +b ,

This is how i’m using tmux in my remote server .. In single window i’m using three terminals in it . see the screen shot below..

Tmux demo

Here i’m using three terminal session in single window .

In first pane – i’m running ipython

In second pane – i’m running glances

In third pane – mysql shell

commands i used for these to achieve are:

tmux             – to start new tmux session

cntl +b %    – split vertically

cntrl +b “      -split 1st pane into by horizontally

cntrl + b o       –to switch between panes

See how useful these tool .. Start using this and play with it ..

Thanks for reading .. Please feel free to tell your suggestions ..

Happy coding .!!!!

Hello World in Django !!!!

Hi …After long time i ‘m blogging about django..Lets start our journey to django .Before reading this i would suggest to get some knowledge in python .

My suggestions for python learning  are

  1. A byte of python with pdf
  2. Dive into Python
  3. Learn python the Hard way

What is Django?

Django is a Web framework written in python language .

what is meant by frame work?

After searching about framework, Frameworks are similar to libraries or templates that are already written for you, you can re-use the code to build your system . i.e(Collection of codes that uses some control mechanisum).

still didn’t get???

I explain with my view, lets forget about all technical terms and others. Lets say you want to build your new house ?

To build new house what are the things you needed?

  • money
  • stones
  • cement
  • empty ground (place)
  • water
  • sand and others stuffs etc….

now same thing for building your system(project)

Framework offers everything you needed, the only thing you need to do is ,place the things in correct manner. like(database details in proper place, your logics , representation of your data. etc.)

Hope you understand something !!!!

now coming to our django framework.It follows MVC pattern like Ruby on Rails  .

MVC: => Model View Controller

Model: for Database access.(this contains database,table details)

Views: your logic goes here

Templates: To represent your data(html files)

Why MVC or What is the Advantage of this?

The biggest Advantage of MVC is ,one’s change doesn’t affect other.

for example if you want to change your database details(say db name or table details) you need to change in model file only( no need of find and replace in entire project).Because each and everything are loosely coupled(independent). Same thing applies for view and templates too.

didn’t get it??

while doing sample code i will explain it …

To install django see install

Sorry i ‘m not going to tell about installation . Since i’m an linux user I know about installation in linux only. You can find lot of source for your operating system installations.

Django documentation is fair enough for any os.

After installing ,lets create the sample project.

Its good practice to create working directory for learning any language .

open terminal(cntrl+alt+T)

create directory mkdir djcodes(here djcodes is my directory name ,you can give any name)

cd djcodes(change to working directory)

To start a new django project (i’m using django 1.5 version):

django-admin startproject sample # here sample is a project name

change directory to sample

cd sample/

now issue  ls command ,

manage.py      sample

You have manage.py file and sample folder inside sample project .

manage.py – points to the settings file in your project. (This files are automatically created by django)

Inside sample folder you have 4 files

__init__py  – this will indicate your project as python package to compiler.

urls.py – file to hold the urls of your website .(e.x)http://localhost/hello

here in order to use /hello in our project you have to mention this in urls.py.

(see my below explanation for these concepts)

settings.py – File that will hold all apps,database settings of your information .

(if you open this file means you can see,time zone,templates etc..). You will learn more about these files in my upcoming posts.

Wsgi.py– This file handles our requests and responses .(our django development server)

Ok. Lets start the server by

./manage.py runserver (Note inside project directory)

this is show like this

Validating models…

0 errors found
Django version 1.4.5, using settings ‘sample.settings’
Development server is running at
Quit the server with CONTROL-C.
[17/Jan/2014 11:45:00] “GET / HTTP/1.1” 200 1957

Open your favorite browser and see the link

It worked!

Congratulations on your first Django-powered page.

Of course, you haven’t actually done any work yet. Here’s what to do next:

  • If you plan to use a database, edit the DATABASES setting in sample/settings.py.
  • Start your first app by running python manage.py startapp [appname].

You’re seeing this message because you have DEBUG = True in your Django settings file and you haven’t configured any URLs. Get to work!

This is default page in django .

Our goal is Hello world page in django .

Lets Start creating our app and display hello world

To create an app(inside project directory)

django-admin startapp hello

This will create a hello folder in project directory.It has four files

__init__.py  – this file indicates your app as python package.

models.py – file to hold your database informations

views.py – your functions to hold requests,logics etc.

tests.py – for testing purposes.

Three things to do for our task

1. add url in urls.py with associate function.

2. write code for url in urls.py

3. html file to render a response.

Lets add url in urls.py

gedit  /sample/urls.py

add your like this

from django.conf.urls import patterns, include, url
from hello.views import myfunction

# Uncomment the next two lines to enable the admin:
# from django.contrib import admin
# admin.autodiscover()

urlpatterns = patterns('',
    # Examples:
    # url(r'^$', 'sample.views.home', name='home'),
    # url(r'^sample/', include('sample.foo.urls')),

    # Uncomment the admin/doc line below to enable admin documentation:
    # url(r'^admin/doc/', include('django.contrib.admindocs.urls')),

    # Uncomment the next line to enable the admin:
    # url(r'^admin/', include(admin.site.urls)),

my views.py(In hello/views.py)

# Create your views here.
from django.http import HttpResponse
def myfunction(request,):
	return HttpResponse("Hello")

save your files .
now run server ./manage.py runserver

Now see in browser by typing http://localhost:8000/hello

this will return hello in page . Thats it our goal is done .

What happens behind the scene . I will explain shortly.

when you type localhost:8000/hello —> this will send request to django

this is will read urls.py and look for pattern and url match . if it matches it calls the associated function.

In our case it matches hello in urls.py and calls myfunction in views.py.

In views.py –> we have the code to display the hello as HttpResponse .

Thats it .. Thanks for reading… Happy coding !!!

How to format pendrive or Memorycard or USB in ubuntu by Terminal

Hi…all today ..i tried to to format by pendrive by our file manager. But sometimes it fails to format.So we have to format it by terminal.

After plugging your device(usb or pendrive or memory card).

Open Terminal(cntrl +Alt + T)

type the command as $ dmesg | Tail

it will display like this

[ 1749.108796] sd 9:0:0:0: [sdb] Write Protect is off
[ 1749.108813] sd 9:0:0:0: [sdb] Mode Sense: 43 00 00 00
[ 1749.109803] sd 9:0:0:0: [sdb] No Caching mode page present
[ 1749.109816] sd 9:0:0:0: [sdb] Assuming drive cache: write through
[ 1749.116895] sd 9:0:0:0: [sdb] No Caching mode page present
[ 1749.116906] sd 9:0:0:0: [sdb] Assuming drive cache: write through
[ 1749.118911]  sdb: sdb1    –> Things to be noted
[ 1749.122785] sd 9:0:0:0: [sdb] No Caching mode page present
[ 1749.122800] sd 9:0:0:0: [sdb] Assuming drive cache: write through
[ 1749.123771] sd 9:0:0:0: [sdb] Attached SCSI removable disk

Then Unmount your device by

$ sudo umount /dev/sdb1

Finally Enter this command to format Your device

$ sudo mkfs.vfat -n ‘Ubuntu’ -I /dev/sdb1

Thats it….Now plug out your device and plug in again to use . Your device is now formatted and ready to use now..

Happy coding!!!!

How to fix subtitles delay or ealier with your movies by python code

Hi..all …today i was watching movie with subtitles . I had delay with my subtitles files ,like  subtitles mismatch with  the every scenes of movie.  .

I sloved this issue by python code .Subtitles are in .srt format. (e.x) Pirates of the Caribbean -The Curse of the Black Pearl(2003).srt

I found that i got delay by 2 minutes in my .srt file

Library that  i used : pysrt

Open your terminal (cntrl+alt+t)

type python —> enter into python interpreter mode

import pysrt

if you got any error like –> no moduled named pysrt.  then you need to install pysrt .To install pysrt – sudo easy_install pysrt

for python3 users its available pysrt3

if don’t get any error then you already have this library.

now , if you want to make a delay in .srt file means do like this


>>> subs.shift(seconds=-2) # Move all subs 2 seconds earlier
>>> subs.shift(minutes=1)  # Move all subs 1 minutes later

finally save this file from your terminal by

>>> subs.save('path to ur location/newfilename.srt', encoding='utf-8')

My Entire code which sloved my delay in .srt file

#! usr/bin/python
import pysrt
subs=open("/home/bala/Pirates of the Caribbean -The Curse of the Black Pearl(2003).srt")
subs.shift(minutes=-2) # Move all subs 2 minutes earlier
subs.save('/home/bala/new.srt', encoding='utf-8')#saves file with new.srt in your home directory

This sloved my problem .
Thats it..   Happy coding !!!!

How to install/update firefox in ubuntu

Hi…to all today i updated my firefox.  Its so easy to update in ubuntu..

Here the steps are.

Open terminal and type

sudo add-apt-repository ppa:ubuntu-mozilla-security/ppa

 sudo apt-get update

sudo apt-get install firefox

Thats it….. you successfully installed/updated firefox in your system… To check

$ firefox -v

Screenshot from 2013-01-23 09:16:23

this will show the version of the firefox you installed/updated..


Thanks …:)





How to see the system configuration information in Linux?

Hi to all..Today i learned how to see the system configuration info in linux ..

There are many  Ways to find.

1.System->Administration->Synaptic packageManager

then search the package GNOME DEVICE MANAGER

mark that package for installation and install it..After that go to

Applications->SystemTools->Device Manager

this will list all the informations ….

2.To See the system configuration information by Terminal

type this command  sudo lshw -html >lshw.html

it will store the html file in your home folder of the system.then


It will display all the informations of your system such as version,product,type,memory…etc..

Thats it ..enjoy with linux…………)