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.

 

Advertisement

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 .

Installation:

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

db.mycollection.remove()

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->y

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..:)

 

 

 

 

 

 

 

 

 

 

 

 

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 http://127.0.0.1:8000/
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   http://127.0.0.1:8000/

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)),
    url(r'hello/$',myfunction),
)

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 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

OPEN THAT FILE WITH YOUR  WEB BROWER

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

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