thefourtheye's weblog

opinions are my own; try code/suggestions at your own risk

Fixing Ubuntu Apt-get Sources

| Comments

This script makes use of the mirror protocol.
#!/bin/bash

UbuntuCodeName=`lsb_release -c | cut -f 2`
if [ ! -f /etc/apt/sources.list.Original ];
then
mv /etc/apt/sources.list /etc/apt/sources.list.Original
echo "deb mirror://mirrors.ubuntu.com/mirrors.txt $UbuntuCodeName main restricted universe multiverse
deb-src mirror://mirrors.ubuntu.com/mirrors.txt $UbuntuCodeName main restricted universe multiverse

deb mirror://mirrors.ubuntu.com/mirrors.txt $UbuntuCodeName-updates main restricted universe multiverse
deb-src mirror://mirrors.ubuntu.com/mirrors.txt $UbuntuCodeName-updates main restricted universe multiverse

deb mirror://mirrors.ubuntu.com/mirrors.txt $UbuntuCodeName-backports main restricted universe multiverse
deb-src mirror://mirrors.ubuntu.com/mirrors.txt $UbuntuCodeName-backports main restricted universe multiverse

deb http://security.ubuntu.com/ubuntu $UbuntuCodeName-security main restricted universe multiverse
deb-src http://security.ubuntu.com/ubuntu $UbuntuCodeName-security main restricted universe multiverse

deb http://archive.canonical.com/ubuntu $UbuntuCodeName partner
deb-src http://archive.canonical.com/ubuntu $UbuntuCodeName partner

deb http://extras.ubuntu.com/ubuntu $UbuntuCodeName main
deb-src http://extras.ubuntu.com/ubuntu $UbuntuCodeName main" >> /etc/apt/sources.list
else
echo "This script has been run already. Exiting"
fi

Alternatively, you can run the following commands to update the sources automatically
wget -O UpdateAptSources.sh http://ideone.com/plain/UgsBel
chmod 555 UpdateAptSources.sh
sudo ./UpdateAptSources.sh

Python Equivalent of C's Freopen

| Comments

All I wanted to do is, to make python treat a file as stdin and another file as stdout. This is possible in C and C++ like this

freopen ("Input.txt", "r", stdin);
freopen ("Output.txt","w", stdout);

Including these two lines at the beginning of main function, makes sure that all the read calls read from Input.txt and all the write calls write to Output.txt. We don't have to use fprintf and fscanf to read and write to a file anymore. printf and scanf itself are enough. This would be very useful, if we want to automate an interactive program. Almost all of Sport Programming solutions have these two lines. Because, I don't like to type the input each and every time. So I store it once in a file and then write the results to another file.

Now, I wanted to do the same thing in Python, for Google Code Jam 2013. So started looking for way and I got this. I saw Greg Hewgill's answer which is like this

import sys
# parse command line
if file_name_given:
inf = open(file_name_given)
else:
inf = sys.stdin

This assigns stdin or a file to the variable inf. But then, I wanted to use standard library functions of Python like raw_input and input as well. So, I managed to do this

import sys

sys.stdin = open("Input.txt")
sys.stdout = open("Output.txt")

That's it. I just replaced the stdin and stdout with two different files. Now I am even able to use raw_input and input functions, without any trouble. Hope this helps... :)



Ubuntu Nvidia Driver Installation

| Comments

Following are the steps to install Ubuntu Nvidia drivers and activating it.

sudo apt-get install linux-headers-$(uname -r)
sudo apt-get install nvidia-310
sudo apt-get install nvidia-313-updates
sudo apt-get install nvidia-settings
Once the instllation of the above mentioned packages are successful, run
sudo nvidia-xconfig
This will create the /etc/X11/xorg.conf file

Now, do
sudo software-properties-gtk

Which will open up a window like this


Select the latest driver name from the list and then do

sudo reboot

That's it :)

MongoDB Aggregation Framework Basics Explained

| Comments

This post assumes that, the reader has a very good understanding of SQL.

To understand the MongoDB's aggregation framework, lets start with inserting the following data.
db.Student.insert ({Student_Name:"Kalki",  Class: "2", Mark_Scored:100, Subject: ["Tamil", "English", "Maths"]})
db.Student.insert ({Student_Name:"Matsya", Class: "1", Mark_Scored:10, Subject: ["Tamil", "English"]})

db.Student.insert ({Student_Name:"Krishna",Class: "1", Mark_Scored:50, Subject: ["Tamil"]})
db.Student.insert ({Student_Name:"Buddha", Class: "2", Mark_Scored:60, Subject: ["Tamil"]})
db.Student.insert ({Student_Name:"Rama", Class: "2", Mark_Scored:80, Subject: ["Tamil"]})

db.Student.insert ({Student_Name:"Krishna",Class: "1", Mark_Scored:50, Subject: ["English"]})
db.Student.insert ({Student_Name:"Buddha", Class: "2", Mark_Scored:60, Subject: ["English"]})
db.Student.insert ({Student_Name:"Rama", Class: "2", Mark_Scored:80, Subject: ["English"]})

db.Student.insert ({Student_Name:"Matsya", Class: "1", Mark_Scored:67, Subject: ["Maths"]})
db.Student.insert ({Student_Name:"Krishna",Class: "1", Mark_Scored:95, Subject: ["Maths"]})
db.Student.insert ({Student_Name:"Buddha", Class: "2", Mark_Scored:88, Subject: ["Maths"]})
db.Student.insert ({Student_Name:"Rama", Class: "2", Mark_Scored:40, Subject: ["Maths"]})

Pipeline

The aggregation framework is based on pipeline concept, just like unix pipeline. There can be N number of operators. Output of first operator will be fed as input to the second operator. Output of second operator will be fed as input to the third operator and so on.


Pipeline Operators

Following are the basic pipeline operators and let us make use of these operators over the sample data which we created. We are not going to discuss about Map-Reduce in this post.

  1. $match
  2. $unwind
  3. $group
  4. $project
  5. $skip
  6. $limit
  7. $sort

$match

This is similar to MongoDB Collection's find method and SQL's WHERE clause. Basically this filters the data which is passed on to the next operator. There can be multiple $match operators in the pipeline.


Note:The data what we pass to the aggregate function should be a list of Javascript objects (Python dictionaries). Each and every operator should be in a separate javascript object like shown in all the examples below.

Example:We want to consider only the marks of the students who study in Class "2"
db.Student.aggregate ([
{
"$match":
{
"Class":"2"
}
}
])
and the result is
{
"result" : [
{
"_id" : ObjectId("517cbb98eccb9ee3d000fa5c"),
"Student_Name" : "Kalki",
"Class" : "2",
"Mark_Scored" : 100,
"Subject" : [
"Tamil",
"English",
"Maths"
]
},
{
"_id" : ObjectId("517cbb98eccb9ee3d000fa5f"),
"Student_Name" : "Buddha",
"Class" : "2",
"Mark_Scored" : 60,
"Subject" : [
"Tamil"
]
},
{
"_id" : ObjectId("517cbb98eccb9ee3d000fa60"),
"Student_Name" : "Rama",
"Class" : "2",
"Mark_Scored" : 80,
"Subject" : [
"Tamil"
]
},
{
"_id" : ObjectId("517cbb98eccb9ee3d000fa62"),
"Student_Name" : "Buddha",
"Class" : "2",
"Mark_Scored" : 60,
"Subject" : [
"English"
]
},
{
"_id" : ObjectId("517cbb98eccb9ee3d000fa63"),
"Student_Name" : "Rama",
"Class" : "2",
"Mark_Scored" : 80,
"Subject" : [
"English"
]
},
{
"_id" : ObjectId("517cbb98eccb9ee3d000fa66"),
"Student_Name" : "Buddha",
"Class" : "2",
"Mark_Scored" : 88,
"Subject" : [
"Maths"
]
},
{
"_id" : ObjectId("517cbb98eccb9ee3d000fa67"),
"Student_Name" : "Rama",
"Class" : "2",
"Mark_Scored" : 40,
"Subject" : [
"Maths"
]
}
],
"ok" : 1
}
Let us say, we want to consider only the marks of the students who study in Class "2" and whose marks are more than or equal to 80
db.Student.aggregate ([
{
"$match":
{
"Class":"2",
"Mark_Scored":
{
"$gte": 80
}
}
}
])
Or we can use $match operator twice to achieve the same result
db.Student.aggregate ([
{
"$match":
{
"Class":"2",
}
},
{
"$match":
{
"Mark_Scored":
{
"$gte": 80
}
}
}
])
and the result would be
{
"result" : [
{
"_id" : ObjectId("517cbb98eccb9ee3d000fa5c"),
"Student_Name" : "Kalki",
"Class" : "2",
"Mark_Scored" : 100,
"Subject" : [
"Tamil",
"English",
"Maths"
]
},
{
"_id" : ObjectId("517cbb98eccb9ee3d000fa60"),
"Student_Name" : "Rama",
"Class" : "2",
"Mark_Scored" : 80,
"Subject" : [
"Tamil"
]
},
{
"_id" : ObjectId("517cbb98eccb9ee3d000fa63"),
"Student_Name" : "Rama",
"Class" : "2",
"Mark_Scored" : 80,
"Subject" : [
"English"
]
},
{
"_id" : ObjectId("517cbb98eccb9ee3d000fa66"),
"Student_Name" : "Buddha",
"Class" : "2",
"Mark_Scored" : 88,
"Subject" : [
"Maths"
]
}
],
"ok" : 1
}

$unwind

This will be very useful when the data is stored as list. When the unwind operator is applied on a list data field, it will generate a new record for each and every element of the list data field on which unwind is applied. It basically flattens the data. Lets see an example to understand this better

Note: The field name, on which unwind is applied, should be prefixed with $ (dollar sign)

Example:Lets apply unwind over "Kalki"'s data.
db.Student.aggregate ([
{
"$match":
{
"Student_Name": "Kalki",
}
}
])
This generates the following output
{
"result" : [
{
"_id" : ObjectId("517cbb98eccb9ee3d000fa5c"),
"Student_Name" : "Kalki",
"Class" : "2",
"Mark_Scored" : 100,
"Subject" : [
"Tamil",
"English",
"Maths"
]
}
],
"ok" : 1
}
Whereas
db.Student.aggregate ([
{
"$match":
{
"Student_Name": "Kalki",
}
},
{
"$unwind": "$Subject"
}
])
will generate the following output
{
"result" : [
{
"_id" : ObjectId("517cbb98eccb9ee3d000fa5c"),
"Student_Name" : "Kalki",
"Class" : "2",
"Mark_Scored" : 100,
"Subject" : "Tamil"
},
{
"_id" : ObjectId("517cbb98eccb9ee3d000fa5c"),
"Student_Name" : "Kalki",
"Class" : "2",
"Mark_Scored" : 100,
"Subject" : "English"
},
{
"_id" : ObjectId("517cbb98eccb9ee3d000fa5c"),
"Student_Name" : "Kalki",
"Class" : "2",
"Mark_Scored" : 100,
"Subject" : "Maths"
}
],
"ok" : 1
}

$group

Now that we have flatten the data to be processed, lets try and group the data to process them. The group pipeline operator is similar to the SQL's GROUP BY clause. In SQL, we can't use GROUP BY unless we use any of the aggregation functions. The same way, we have to use an aggregation function in MongoDB as well. You can read more about the aggregation functions here. As most of them are like in SQL, I don't think much explanation would be needed.


Note: The _id element in group operator is a must. We cannot change it to some other name. MongoDB identifies the grouping expression with the _id field only.

Example: Lets try and get the sum of all the marks scored by each and every student, in Class "2"
db.Student.aggregate ([
{
"$match":
{
"Class": "2"
}
},
{
"$unwind": "$Subject"
},
{
"$group":
{
"_id":
{
"Student_Name" : "$Student_Name"
},
"Total_Marks":
{
"$sum": "$Mark_Scored"
}
}
}
])
If we look at this aggregation example, we have specified an _id element and Total_Marks element. The _id element tells MongoDB to group the documents based on Student_Name field. The Total_Marks uses an aggregation function $sum, which basically adds up all the marks and returns the sum. This will produce this Output
{
"result" : [
{
"_id" : {
"Student_Name" : "Rama"
},
"Total_Marks" : 200
},
{
"_id" : {
"Student_Name" : "Buddha"
},
"Total_Marks" : 208
},
{
"_id" : {
"Student_Name" : "Kalki"
},
"Total_Marks" : 300
}
],
"ok" : 1
}
We can use the sum function to count the number of records match the grouped data. Instead of "$sum": "$Mark_Scored", "$sum": 1 will count the number of records. "$sum": 2 will add 2 for each and every grouped data.
db.Student.aggregate ([
{
"$match":
{
"Class": "2"
}
},
{
"$unwind": "$Subject"
},
{
"$group":
{
"_id":
{
"Student_Name" : "$Student_Name"
},
"Total_Marks":
{
"$sum": 1
}
}
}
])
This will produce this Output
{
"result" : [
{
"_id" : {
"Student_Name" : "Rama"
},
"Total_Marks" : 3
},
{
"_id" : {
"Student_Name" : "Buddha"
},
"Total_Marks" : 3
},
{
"_id" : {
"Student_Name" : "Kalki"
},
"Total_Marks" : 3
}
],
"ok" : 1
}
This is because each and every student has marks for three subjects.

$project

The project operator is similar to SELECT in SQL. We can use this to rename the field names and select/deselect the fields to be returned, out of the grouped fields. If we specify 0 for a field, it will NOT be sent in the pipeline to the next operator. We can even flatten the data using project as shown in the example below

Example:
db.Student.aggregate ([
{
"$match":
{
"Class": "2"
}
},
{
"$unwind": "$Subject"
},
{
"$group":
{
"_id":
{
"Student_Name" : "$Student_Name"
},
"Total_Marks":
{
"$sum": "$Mark_Scored"
}
}
},
{
"$project":
{
"_id":0,
"Name": "$_id.Student_Name",
"Total": "$Total_Marks"
}
}
])
will result in
{
"result" : [
{
"Name" : "Rama",
"Total" : 200
},
{
"Name" : "Buddha",
"Total" : 208
},
{
"Name" : "Kalki",
"Total" : 300
}
],
"ok" : 1
}
Lets say we try to retrieve Subject field by specifying project like shown below. MongoDB will simply ignore the Subject field, since it is not used in the group operator's _id field.
"$project":
{
"_id":0,
"Subject":1,
"Name": "$_id.Student_Name",
"Total": "$Total_Marks"
}

$sort

This is similar to SQL's ORDER BY clause. To sort a particular field in descending order specify -1 and specify 1 if that field has to be sorted in ascending order. I don't think this section needs more explanation. Lets straight away look at an example
Example:
db.Student.aggregate ([
{
"$match":
{
"Class": "2"
}
},
{
"$unwind": "$Subject"
},
{
"$group":
{
"_id":
{
"Student_Name" : "$Student_Name"
},
"Total_Marks":
{
"$sum": "$Mark_Scored"
}
}
},
{
"$project":
{
"_id":0,
"Name": "$_id.Student_Name",
"Total": "$Total_Marks"
}
},
{
"$sort":
{
"Total":-1,
"Name":1
}
}
])
Will Sort the data based on Marks in descending Order first and then by Name in Ascending Order.
{
"result" : [
{
"Name" : "Kalki",
"Total" : 300
},
{
"Name" : "Buddha",
"Total" : 208
},
{
"Name" : "Rama",
"Total" : 200
}
],
"ok" : 1
}

$limit and $skip

These two operators can be used to limit the number of documents being returned. They will be more useful when we need pagination support.

Example:
db.Student.aggregate ([
{
"$match":
{
"Class": "2"
}
},
{
"$unwind": "$Subject"
},
{
"$group":
{
"_id":
{
"Student_Name" : "$Student_Name"
},
"Total_Marks":
{
"$sum": "$Mark_Scored"
}
}
},
{
"$project":
{
"_id":0,
"Name": "$_id.Student_Name",
"Total": "$Total_Marks"
}
},
{
"$sort":
{
"Total":-1,
"Name":1
}
},
{
"$limit":2,
},
{
"$skip":1,
}
])
will result in
{ "result" : [ { "Name" : "Buddha", "Total" : 208 } ], "ok" : 1 }
Because the limit operator receives 3 documents from the sort operator and allows only the first two documents to pass through it, thereby dropping Rama's record. The skip operator skips one document (that means the first document (Kalki's document) is dropped) and allows only the Buddha's document to pass through.

All the examples shown here are readily usable with pyMongo (Just replace db.Student with your Collection object name)


Ubuntu Screen Configuration

| Comments

As we all know Screen is an excellent program. Here is how my ~/.screenrc looks like

   vbell off
defscrollback 10000
hardstatus on
hardstatus alwayslastline "%{wk}%-w%{kw}%n %t%{-}%+w %=%{Gk} %H %{Wk} %d-%M-%Y %C:%s %a %D "
termcapinfo xterm ti@:te@
startup_message off
msgwait 1
altscreen on
escape ``
bind c screen 1
bind ^c screen 1
bind 0 select 10
screen 1

And this is how it looks in action



  1. blockquote (the key right above Tab key) will be the command character
  2. As you see in the image, the screen numbers will begin with 1
  3. The bottom bar shows list of open terminals, machine name, date, time, weekday
  4. `+c will create a new terminal
  5. `+1,`+2,`+3,... will switch to the respective terminals