Sunday, May 22, 2011

Linux Basics Walkthrough Part 1

Nowadays, Linux comes with pretty simple to use GUI. Professionals still prefers CLI to perform there tasks as they know the mantra GUI is simple but CLI is powerful. Let's dig into the basis.

1. Navigation

cd - Change Directory. For Example: cd /home/karan

2. Where I'm
 
pwd - prints the current working directory.


3. Who I'm

whoami - Shows user who is currently issuing command.

4. Feel Secure

passwd - Allows you to change the password for the user who is issuing the command.

Note: passwd <username> Allows Super Users/Admins to change the password of other users.

5. Reading files

cat - Allows user to view content of a file.

P.S. I'll be updating this post soon with lot more commands.

Hope this Helps! Please write your comments it will help me improve.

Tuesday, March 01, 2011

Python Basics: Python Complex Numbers

Python supports Complex Numbers and are built-in to the language. You can create complex numbers either by specifying the number in (real + imagJ) form or using a built-in method complex(a, b).

Complex Numbers:
  1. For Electronic Engineers python uses j or J instead of an i for imaginary numbers.
  2. j or J means square root of -1.
  3. You can perform arithmetic operations on Complex Numbers.
  4. Both real and imagJ part are floating-point numbers.
  5. Complex Number has a nonzero real part.
  6. cmath library. (for your reference.)
(Post in your comments if i left out something important so i can add in here.)

How Complex Numbers Works.

  1 #!/usr/local/bin/python3.2
  2 # Filename: ComplexNum.py
  3
  4 # How Imaginary Number Arithmetic Works.
  5 print('1j + 1j :', 1j + 1j)
  6 print('3j - 1j :', 3j - 1j)
  7 print('2j * 1j :', 2j * 1j)
  8 print('2j / 1j :', 2j / 1j)
  9
 10 # complex(real, imagJ)
 11 print('complex(1, 1) :', complex(1, 1))
 12
 13 # Make Complex Numbers bit More Complex. Arithmetic on real and imagJ parts. :)
 14 print('3 * complex(2, 3) :', 3 * complex(2, 3))
 15 print('2j * complex(4, 6) :', 2j * complex(4, 6))
 16 print('(2 + 4j) * complex(4, 2) :', (2 + 4j) * complex(4, 2))
 17 print('(2 + 4j) * (4 + 2j) :', (2 + 4j) * (4 + 2j))
 18 print('complex(2, 4) * complex(4, 2) :', complex(2, 4) * complex(4, 2))

You can download the file here: ComplexNum.py

Output:

1j + 1j : 2j
3j - 1j : 2j
2j * 1j : (-2+0j)
2j / 1j : (2+0j)
complex(1, 1) : (1+1j)
3 * complex(2, 3) : (6+9j)
2j * complex(4, 6) : (-12+8j)
(2 + 4j) * complex(4, 2) : 20j
(2 + 4j) * (4 + 2j) : 20j
complex(2, 4) * complex(4, 2) : 20j

Explanation:
  1. Line #5 & #6 are pretty much self-explained.
  2. Line #7 2j * 1j = 2j^2. As we know that j here means square root of -1. Now j^2 equals -1. leads to the output of (-2 + 0j). As we know that a complex number has a nonzero real part.
  3. Line #8 2j / 1j = (2+0j). Complex Part division is a bit harder maybe or not. Because we need to multiply but top and bottom by what we call complex conjugate. This is what you get when you change the sign of the imaginary part but not the real part. 2j/1j * -1j/-1j, i.e, -2j^2/-1j^2. Minus signs cancels out we are left with (2 + 0j).
  4. Line #11 uses python interpreter built-in function complex(a, b). But the output has nothing much that i can explain.
I believe rest of it is more or less self -explainable. But if you think they need explanation post in your comments. :)

Hope this Helps! Please write your comments it will help me improve.

Monday, February 14, 2011

Java in Practice: Coding and UML Aggregation v/s Composition



Aggregation [ 'HAS-A' Relationship ]

class StereoSystem {
    private boolean state ;
    StereoSystem() {}
    StereoSystem(boolean state) {
        this.state = state ;
        System.out.println("Stereo System State: " + (state == true ? "On!" : "Off!")) ;
    }
}

class Car {
    private StereoSystem s ;
    Car() {}
    Car(String name, StereoSystem s) {
        this.s = s ;
    }
    public static void main(String[] args) {
        StereoSystem ss = new StereoSystem(true) ;  // true(System is ON.) or false (System is OFF)
        Car c = new Car("BMW", ss) ;
    }
}


In UML Aggregation is Expressed as


Composition [ 'Contains' Relationship ]

import java.util.Date ;

class Piston {
    private Date pistonDate ;
    Piston() {
        pistonDate = new Date() ;
        System.out.println("Manufactured Date :: " + pistonDate) ;
    }
}

class Engine {
    private Piston piston ;
    Engine() {
        piston = new Piston() ;
    }
    public static void main(String[] args) {
        Engine engine = new Engine() ;
    }
}

In UML Composition is Expressed as 






I originally posted this here: http://www.coderanch.com/t/443002/java/java/Java-Coding-UML-Aggregation-Composition


Hope this Helps! Please write your comments it will help me improve.

Python Basics: Built-in function abs usage

First of all Built-in functions are those functions that are available to us all the time without importing any python modules. Today we'll see how to use abs(x) function.
So, Let's get Started:
abs(x) function takes an argument that can be a integer or a floating point number. It returns you the absolute value of a given number. A argument can be complex number, in that case the magnitude is returned.
Simple Case:

Let, say if x is less than zero, i.e, x < 0, than -x is returned. Otherwise, if x is greater than or equal to zero, i.e, x >= 0, than x is returned.

Complex Case:

Let, say if x is complex, than in that case abs returns a square root of  x.imag**2 + x.real**2 (a.k.a magnitude) of a number.

We'll be discussing complex numbers in another post. But in any case the formula to compute them remains same.

Hope this Helps! Please write your comments it will help me improve.

Saturday, February 12, 2011

Fix: Weblogic ServletContext.getRealPath() returns null

Weblogic Server[WLS] returns null if your code is using Servlet.getRealPath() many of you expect that it's another weblogic server bug but it's not. An application can be deployed in two ways either exploded or archive. If you are deploying an application as exploded which is a folder containing your application you won't see such an error. But, if you deploy it as an archive which is like zipping the folder having extentions like war, ear etc.  you will see this error, and mostly we prefer to deploy our applications as archive rather than exploded. So, how to fix such an error.
So, Let's get started
Note:  The advantage of deploying an application as exploded over archive is the access to the application content is much faster.
It's a simple fix. You can fix this problem by approaching it in two ways one is application server level. The other is application level.

Application Server Level

Goto Server Admin Console > Domain > Web Application > Archived Real Path Enabled. Enable this option restart the server and Bingo.!!!

It makes an entry into domain config.xml file something like this,

<web-app-container>
    <show-archived-real-path-enabled>true</show-archived-real-path-enabled>
</web-app-container>

Application Level

Create a xml file in WEB-INF folder name it weblogic.xml if it's not there by default. Add the following entry.

<container-descriptor>
    <show-archived-real-path-enabled>true</show-archived-real-path-enabled>
</container-descriptor>

Redeploy your application done.

Hope this Helps! Please write your comments it will help me improve.

Wednesday, February 09, 2011

JSF2.0: Unable to load JSF2.0/Primefaces jars on Weblogic Server 10.3.2 start up

I think except for the option to upgrade to a new version or applying a patchset there is a workaround that i tried and it worked for me.

Problem
It occurs because weblogic 10 is shipped with Mojarra JSF 1.2 and the version that we want to make it work is Mojarra JSF 2.0.

Solution
It's a simple fix. In weblogic there is a file called commEnv.bat/sh. If you are running windows edit the bat file, else if you are running Linux/Mac edit the sh file.

Search for PRE_CLASSPATH or WEBLOGIC_CLASSPATH and add a complete path to the jsf-api.jar, jsf-impl.jar before weblogic.jar and if you are using Primefaces, just append it afterwords. Restart the server, deploy your application make sure that jsf-api.jar and jsf-impl.jar are there in the lib folder, but you are now free to remove the primefaces jar as it is already loaded by weblogic at server start-up otherwise you will see the same error still your application won't break.


Topic Discussed
http://forums.oracle.com/forums/thread.jspa?threadID=1043858&start=30&tstart=0
http://www.coderanch.com/t/486434/JSF/java/Unable-load-primefaces-jar-Weblogic

Hope this Helps! Please write your comments it will help me improve.

Tuesday, February 08, 2011

Python Basics: Built-in function Print usage

Before embarking on a journey to learn any programming language most people create a sample Hello World application to see how the language works. Today we are going to learn this only with a variation of how you use it's in-built print function.

So, Let's get started.
I'll be using vim to write programs and python 3.2 to run it. If you are using windows prefer python IDLE which comes bundled with python.

1. Source Code: Vim 












Explanation:
1. The first line of the code is called the shebang line
2. Line 2 is a comment.
3. Line 4 uses the built-in print function to print the string on the standard output on screen.
4. Line 5 uses a variation of the function.
5. Line 7 is a comment
6. Line 9 has nothing special
7. from Line 10 onwards it shows you the variation of how to use the print function.
Before i explain you line no 10, 11 and 12 i want you to look at the definition of the function itself.
print([object...][, sep=' '][, end='\n'][, file=sys.stdout])
sep and end keyword arguments take a string and if nothing is given the defaults are taken and for last file keyword takes a file object and how it works is explained in Line 12. You might have noticed the output. It doesn't print out line 12 on the screen instead it writes that line to a file specified pybuf.txt. Here, open function returns you a file object and it is defined as follows.
open(filename, mode)
Both of them are strings. I'll explain open function later in detail except the filename is name of the file to be created if not existed and mode is set to 'a' which means append text to the end of a file.

2. Output: Python 3.2












3. pybuf.txt file





Monday, February 07, 2011

Install Python 3.2 on Linux [Ubuntu]

First of all, I don't see a point to show you how to install Python on Windows. It's dead easy download the installer from python.org and in few clicks you will have python up and running. Installing Python on Linux is bit different and requires some pre-work to be done.

So Let's get started.
Note: Every Modern Linux had a python Installed. You can verify this by opening your Terminal and typing python. It will open the python shell for you. Do not remove the installed version.
Step 1: Pre-requisites for Ubuntu

To be able to compile Python Source, you will need few packages. Fire up the terminal and execute this command
sudo apt-get install build-essential libncursesw5-dev libreadline5-dev libssl-dev libgdbm-dev libc6-dev libsqlite3-dev tk-dev
It will take some time to finish depending upon your bandwidth. :)

Step 2: Downloading Python Sources and extracting them on your disk

Download a tarball from the python site here is a direct link. Once, the download is completed you can extract the files by doing a right-click on the file and then clicking the extract option
OR
Fire up the terminal and execute this command which will download the file first and extract it on the disk.
wget http://www.python.org/ftp/python/3.2/Python-3.2rc2.tgz && tar -xvf Python-3.2rc2.tgz
Step 3: Installing Python 3.2

Fire up a new Terminal and execute the following commands individually.

1.   ./configure
2.   make
3.  sudo make altinstall

Hope this Helps! Please write your comments it will help me improve.