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

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

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

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

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: https://www.djangoproject.com/download/

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

$ cd Django-1.5.2

# Setup django
$ python setup.py install
- this will install django web framework

# Create the new site using following command
$ django-admin.py startproject mysite
- after you run this command, it will create the following folder structure
- mysite
--- manage.py
--- mysite
------ urls.py
------ __init__.py
------ wsgi.py
------ settings.py
------ views.py (add new file as shown below)

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

# Update the urls.py file in mysite/ directory as
#!/usr/bin/python
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 manage.py 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:

    <context-param>
      <param-name>javax.faces.FACELETS_REFRESH_PERIOD</param-name>
      <param-value>-1</param-value>
    </context-param>

I changed it to '1' and also changed
    <context-param>
      <param-name>javax.faces.PROJECT_STAGE</param-name>
      <param-value>Production</param-value>
    </context-param>



to

    <context-param>
      <param-name>javax.faces.PROJECT_STAGE</param-name>
      <param-value>Development</param-value>
    </context-param>

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)

Or

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

eg:
Host MyHost
HostName 192.168.1.101
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.

XX:MaxPermSize=256m

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

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

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) {
                System.out.println(reader.readLine());
            }
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}