Friday, September 26, 2014

Why the password filed data need to be stored in the char array

It is a recommended practice to “empty” the read password string once its use is over. This is a secure programming practice to avoid malicious reads of program data to discover password strings.With a char array, as soon as the password is validated, it is possible to empty it and remove the trace of the password text from memory; with a String object, which is garbage collected, it is not as easy as
with a char array.

Monday, September 22, 2014

Javascript Custom Utility Method to check a variable is undefined

var obj=[];
if(Object.prototype.toString.call(obj)=="[object Array]")
{
console.log("as");
}

console.log(obj.toString());

Sunday, September 21, 2014

How to access “Applications” menu in Ubuntu Unity Desktop

open terminal
    or

type  ctrl+ alt+T

and type the following commands

sudo apt-add-repository ppa:diesch/testing
sudo apt-get update
sudo apt-get install classicmenu-indicator

Saturday, September 20, 2014

Think before running these linux commands

wget http://example.com/something -O – | sh


this has two commands in series to execute 

1 . download something from the http://example.com/something  using wget 
2. just pass this downloaded to sh command where sh is just execute the shell script if the downloaded content is shell script 

do be aware when you are running such type of command above .

Thursday, August 21, 2014

send email with body as html

'''
Created on 21-Aug-2014

@author: bambo
'''
from email.MIMEMultipart import MIMEMultipart

from email.MIMEText import MIMEText

import smtplib

'''
    prepare Header Of Message - Start
'''
fromAddr = "bambobabu@gmail.com"
toAddr = "bambobabu@gmail.com"
msg = MIMEMultipart()
msg['From'] = fromAddr
msg['To'] = toAddr
msg['Subject'] ="Python EMail"

body = "<h1>Python Body</h1>"

msg.attach(MIMEText(body,'html'))


'''
    prepare Header Of Message - End
'''

server = smtplib.SMTP('smtp.gmail.com', 587)
server.ehlo()
server.starttls()
server.ehlo()
server.login("bambobabu@gmail.com", "Mca_503911")
text = msg.as_string()
print text
server.sendmail(fromAddr, toAddr, text)

Sending Email Using Python

#-------------------------------------------------------------------------------
# Name:        module1
# Purpose:
#
# Author:      Ramesh
#
# Created:     21-08-2014
# Copyright:   (c) Ramesh Babu Y2014
# Licence:     Sky
#-------------------------------------------------------------------------------

import smtplib
fromaddr = 'From Email Id'
toaddrs  = 'To EMail Id'
msg = 'Enter you message here'

server = smtplib.SMTP("smtp.gmail.com:587")

server.starttls()

server.login("<<Your GMail Id>>","GMail Id Password")

server.sendmail(fromaddr, toaddrs, msg)

Thursday, July 17, 2014

Features of best sorting algorithm

The ideal sorting algorithm would have the following properties:
  • Stable: Equal keys aren't reordered.
  • Operates in place, requiring O(1) extra space.
  • Worst-case O(n·lg(n)) key comparisons.
  • Worst-case O(n) swaps.
  • Adaptive: Speeds up to O(n) when data is nearly sorted or when there are few unique keys.

Wednesday, July 9, 2014

Final: Question 1 , MongoDB

  1. db.messages.find({"headers.From":"andrew.fastow@enron.com","headers.To":{$in:["jeff.skilling@enron.com"]}}).count()



  2. Question : 

  1. Please download the Enron email dataset enron.zip, unzip it and then restore it using mongorestore. It should restore to a collection called "messages" in a database called "enron". Note that this is an abbreviated version of the full corpus. There should be 120,477 documents after restore.

    Inspect a few of the documents to get a basic understanding of the structure. Enron was an American corporation that engaged in a widespread accounting fraud and subsequently failed.

    In this dataset, each document is an email message. Like all Email messages, there is one sender but there can be multiple recipients.

    Construct a query to calculate the number of messages sent by Andrew Fastow, CFO, to Jeff Skilling, the president. Andrew Fastow's email addess was andrew.fastow@enron.com. Jeff Skilling's email was jeff.skilling@enron.com.

    For reference, the number of email messages from Andrew Fastow to John Lavorato (john.lavorato@enron.com) was 1.

Sunday, June 29, 2014

Saturday, June 28, 2014

week 5, , Homework: Homework 5.1 (Hands On) , aswers

db.posts.aggregate([{$unwind:"$comments"},{$group:{"_id":"$comments.author",total_comments:{$sum:1}}},{$sort:{"total_comments":-1}},{$limit:1}])

Saturday, June 7, 2014

Write a program in the language of your choice that will remove the grade of type "homework" with the lowest score for each student from the dataset that you imported in HW 2.1. Since each document is one grade, it should remove one document per student.

package com.tengen;

import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.mongodb.AggregationOutput;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.MongoClient;
import com.mongodb.ServerAddress;

public class Homework23 {

public static void main(String[] args) throws UnknownHostException {
MongoClient client = new MongoClient(new ServerAddress("localhost",
27017));

DB db = client.getDB("students");
DBCollection collection = db.getCollection("grades");

BasicDBObject bdb = new BasicDBObject("type", "homework");

BasicDBObject sort = new BasicDBObject("student_id", 1).append("score",
1);

// DBCursor cur = collection.find(bdb).sort(sort);

List<DBObject> pipeline = new ArrayList<DBObject>();

pipeline.add(new BasicDBObject("$group", new BasicDBObject("_id",
"$student_id")));

AggregationOutput aoup = collection.aggregate(pipeline);

Iterable<DBObject> results = aoup.results();

Iterator<DBObject> itr = results.iterator();
DBCursor cur = null;
try {

while (itr.hasNext()) {
DBObject agdb = itr.next();

bdb.append("student_id", agdb.get("_id"));

cur = collection.find(bdb).sort(sort);

cur.hasNext();

DBObject docToRemove = cur.next();

Double minScore = (Double) docToRemove.get("score");

while (cur.hasNext()) {

DBObject doc1 = cur.next();

if (minScore > (Double) doc1.get("score")) {
minScore = (Double) doc1.get("score");
docToRemove = doc1;
}

}

System.out.println("*********************");
System.out.println(docToRemove);
System.out.println("*********************");

collection.remove(docToRemove);

}

}

finally {
if (cur != null)
cur.close();
}

}

}

What is the student_id of the lowest exam score above 65?

For "What is the student_id of the lowest exam score above 65?"

run the below query you will get the answer

db.grades.aggregate({$group:{_id:{studentId:"$student_id",'typ1':"$type"},'average':{$avg:'$score'}}},{"$sort":{"average":1}},{$match:{"_id.typ1":"exam","average":{$gte:65}}},{"$limit":1});

Thursday, May 29, 2014

Kill a Process running on a specific port in windows OS (i tested in windows 8.1)

1 . Find which process is running on the specific port with the below command

netstat -ano

2 . Then take the PID (which in my case is in the last column) , then run the following command

taskkill  /F /PID <<pid>>

once you run the above command and if the process killed successfully then you will see the below message

SUCCESS: The process with PID <<pid>> has been terminated.


* <<>pid>>  represents the pid which is using the specific port number.

Sunday, May 18, 2014

JAVA NIO , BUFFER , LIMIT and POSITION

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class FileNIO {

public static void main(String[] args) throws IOException {
try {
FileOutputStream fout = new FileOutputStream("nio.txt");

FileChannel fc = fout.getChannel();

ByteBuffer bb = ByteBuffer.allocate(10);

bb.clear();

for (int i = 0; i < 10; i++) {

System.out.println("space left in the buffer "+bb.limit());
System.out.println("next element where the data need to be put :"+bb.position());

bb.put((byte) 'A');

}

System.out.println("space left in the buffer "+bb.limit());
System.out.println("next element where the data need to be put :"+bb.position());


bb.flip();

fc.write(bb);

} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

}




will print the following output



space left in the buffer 10
next element where the data need to be put :0
space left in the buffer 10
next element where the data need to be put :1
space left in the buffer 10
next element where the data need to be put :2
space left in the buffer 10
next element where the data need to be put :3
space left in the buffer 10
next element where the data need to be put :4
space left in the buffer 10
next element where the data need to be put :5
space left in the buffer 10
next element where the data need to be put :6
space left in the buffer 10
next element where the data need to be put :7
space left in the buffer 10
next element where the data need to be put :8
space left in the buffer 10
next element where the data need to be put :9
space left in the buffer 10
next element where the data need to be put :10

Thursday, May 15, 2014

JSTL Cookie

To access the cookie using the jstl in jsp

{cookie.cookieName.value}  will return the value