Friday, October 4, 2013

Lambda in Python (Map, Reduce and Filter)

Map, Reduce and Filter using Lambda in Python

1. Map
>>>map(lambda x:x*x, range(1,10))
>>>[1, 4, 9, 16, 25, 36, 49, 64, 81]

2. Reduce
>>>reduce(lambda x,y: x+y, range(1,10))

3. Filter
>>>filter(lambda x: x % 2 != 0, range(1,20))
>>>[1, 3, 5, 7, 9, 11, 13, 15, 17, 19]

ex1: Add sum of all even number from 1 to 20
>>>reduce(lambda x,y: x+y, filter(lambda x: x % 2 == 0, range(1,20)))

ex2: lambda as return function
>>>g = lambda x: x**2
>>>print g(2)

ex3: like partial function
>>>def increament(n):
>>>    return lambda x: x + n
>>>f = increment(10)
>>>print f(6)
>>>print increament(6)(10)

ex4: prime numbers between 1-100
>>>nums = range(2,100)
>>>for i in range(2,8):
>>>    nums = filter(lambda x: x == i or x % i, nums)
>>>print nums
>>>[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]

ex5: Letter count
>>>print map(lambda w: len(w), 'It has been raining since this morning'.split())
>>>[2, 3, 4, 7, 5, 4, 7]

ex6: Functional Style of adding
>>> from operator import add
>>> expr = "28+32+++32++39"
>>> print reduce(add, map(int, filter(bool, expr.split("+"))))
>>> 131

Wednesday, September 4, 2013

Creating Simple 'Hello World' Django web application

Creating Simple Hello World Django/python web application

# Download the Django from:

# untar the application tar
$ tar xvf Django-1.5.2.tar

$ cd Django-1.5.2

# Setup django
$ python install
- this will install django web framework

# Create the new site using following command
$ startproject mysite
- after you run this command, it will create the following folder structure
- mysite
--- mysite
------ (add new file as shown below)

# Create new python file in mysite/ directory as and add following line
from django.http import HttpResponse
def hello(request):
    html = "<html><body><b>Hello World!</b></body></html>"
    return HttpResponse(html)

# Update the file in mysite/ directory as
from django.conf.urls import patterns, include, url
from mysite.views import hello
urlpatterns = patterns('',
    url(r'^$', hello),

# Finally from inside mysite/ directory run this command
$ python runserver

# Open the browser and type this url http://localhost:8000

Friday, August 23, 2013

Hot Deployment/Republishing the JSF Facelet in Tomcat/Eclipse Environment

Today I was trying to update/republish the JSF pages without redeploying in the tomcat server again and again but couldn't get success.

After few minutes of google, I figured what was the issue. In my web application web.xml file, the FACELETS_REFRES_PERIOD is set as -1 as:


I changed it to '1' and also changed



and VOILA! it worked and I am happy. Now I can change the static resources without module redeploy.

Tuesday, August 20, 2013

Configure ssh login information in config file

How to configure the ssh login information in Config file

Usually we connect the remote host using ssh by giving following command in terminal

$ssh -i filename.pem user@ipaddress 

(if there is the pem file for the security key)


$ssh user@ipaddress

Instead of doing this we can add these information in Config file and just type in

$ssh <host>

The config file reside in .ssh/ folder as .ssh/config and add the following:
Host <HostName/AnyName>
HostName <ipaddress>
User <username>
IdentityFile <if there are any pem file> (Optional)

Host MyHost
User user

Thursday, March 14, 2013

Setting tomcat server in debug mode

To set up tomcat server in debug mode add the following line in the VM argument

Xdebug -Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=n

In eclipse configuration you can add the Edit configuration window as shown below:

Setting Maximum Perm Size for JVM

Maximun Perm Memory is the memory where most of the classes and class loader reside. To adjust or increment the maximum perm gen size, just add the following line as VM arguments when starting server.


Where 256 is the memory MB, you can adjust this value as per your application requirement.

Sunday, July 15, 2012

Reading file in Java

Following is an example how to read the file and display using System.out.println()


public class FileReader {
    public static void main(String[] args) {
        String file = "C:\\Users\\test.csv";
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new InputStreamReader(
                    new FileInputStream(file)));
            while (reader.readLine() != null) {
        } catch (IOException e) {
        } finally {
            try {
                if (reader != null) {
            } catch (IOException e) {