Saturday, November 14, 2015

Converting java code to executable jar file through command prompt

Assume we have set of java files written using an IDE. To conver the file as executable jar file through command prompt and run it across machines. Here are the steps to follow:

§  Create a new folder . Eg: java2jar

§  Create another folder in the newly created folder and copy all the .class files that are required for the program to execute.
Eg: java2jar> src> copy all the files here.

§  Create a folder in the main folder and name it as lib or anything of your choice and copy all the dependable jar files. This is optional and if you have dependable jars.
Eg: java2jar>>lib>>copy all jar files here.

§  Open command prompt in Windows click on windows icon , type ctrl + R and then enter cmd .

§  Navigate to the path where you have created the new folder that has all .class files. 
>>cd C:\Users\lenovo\Desktop\java2jar

§  Type path and enter the path for jre available in the machine.In my machine it is available in the following location.
>>cd C:\Users\lenovo\Desktop\java2jar>path  C:\Program Files\Java\jre1.8.0_45\bin

§  Create new text document in java2jar(my folder name) and rename it as manifest.txt file.

§  Open manifest.txt and type as follows:

Class-Path: lib\jar1.jar lib\jar2.jar lib\jar3.jar #continue  entering all jar files available 
Main-Class:src\main.class                              #Enter the main class file that has main() method.

If you don't have dependable jars then skip Class-Path and enter only Main-Class.

§  Save and close the manifest.txt file.

§  Navigate to command prompt and type the command
>>cd C:\Users\lenovo\Desktop\java2jar>jar cvfm myjar.jar manifest.txt src\*.class

Explaning the above command:
myjar.jar is name of the jar file that you want to create
manifest.txt is the manifest file that has headers
src\*.class adds all the .class files available in the src folder.


§  Navigate to command prompt and type the command
>>cd C:\Users\lenovo\Desktop\java2jar>jar xvf  myjar.jar 

§  It creates META-INF folder in java2jar and it has manifest.MF.

§  Now from the command prompt type 
>>cd C:\Users\lenovo\Desktop\java2jar>java -jar myjar.jar

It runs the program from the command prompt.



Creating a batch file:

Open a new text document in java2jar folder and name it as run.bat

Enter java -jar myjar.jar in the file and save it.

Now double click on run.bat .

The complete folder java2jar can be shared across any machine.
















String contains in python

s = "A nice string search 234"
res = ""
items = ['a', '4', '6']
for x in items:
#print x
if s.find(x) == -1:
res = x + " is not in given string"
print  res
else:
res = x + " is in given string"
print res

Thursday, June 18, 2015

Wednesday, June 17, 2015

Remove punctation from the text - Python


import re
import string
def removePunctuation(text):
   
    #converts to lowercase
    text1 = text.lower()
   
    #removes punctuation
    #punctation = '''!()-[]{};:'"\,<>./?@#$%^&*_~'''
    text2 =  re.sub('[%s]' % string.punctuation, ' ' ,text1)

    #removes leading and trailing spaces
    text3 = text2.strip()
    return text3


   

print removePunctuation("'The best investments today,'")
print removePunctuation("Misery in Paradise;")

Output:

the best investments today
misery in paradise

Monday, June 15, 2015

Program to find two largest numbers using Python

nums = [10, 90, 80, 60, 50]
maxone = 0
maxtwo = 0
for x in nums:
if(maxone < x):
maxtwo = maxone
maxone = x

elif (maxtwo < x):
maxtwo = x

print "First two largest numbers"
print "First largest number:", maxone
print "First largest number:", maxtwo


output:

First two largest numbers
First largest number: 90
First largest number: 80

Friday, June 12, 2015

Conversion of decimal to hexadecimal, binary and octal in Python

#num= int(input("Enter a decimal number:))
num = 15
print "Decimal value of given number:", num

print "Octal value of", num, ":",oct(num)
print "Binary value of", num, ":",bin(num)
print "Hexadecimal value of", num, ":",hex(num)


Output:

Decimal value of given number: 15
Octal value of 15 : 017
Binary value of 15 : 0b1111
Hexadecimal value of 15 : 0xf

To interchange key value pairs of a dictionary object in Python

dt = {'a': '1', 'b' : '2', 'c': '3', 'd': '4'}
print "Original dictionary:", dt
dt1 = {}
for (x, y) in dt.iteritems():
(x,y) = (y,x)
dt1.update(dict([(x,y)]))
print "After interchanging key value pairs:", dt1

Output:

Original dictionary: {'a': '1', 'c': '3', 'b': '2', 'd': '4'}
After interchanging key value pairs: {'1': 'a', '3': 'c', '2': 'b', '4': 'd'}

Wednesday, June 10, 2015

Sorting a list with out sort/sorted functions - Python

lst = [1,0,4,5,6,7,8,0,0,0]

print(lst)
cou = len(lst)
for i in range(cou):
    for j in range(1, cou-i):
       #descending
        """if lst[j] > lst[j-1]:
            (lst[j-1], lst[j]) = (lst[j], lst[j-1])"""
        #ascending  
        if lst[j] < lst[j-1]:
            (lst[j-1], lst[j]) = (lst[j], lst[j-1])
print(lst)

Reverse each word in a sentence - Python

# your code goes here

st = "This is awesome!"

st1 = st.split(" ")
st2 = ''
st3 = []

print st1

for y in st1:
#print y
ls = len(y)
for x in range(len(y)):
st2 = st2 + y[ls - 1 -x]
st2 = st2 + ' '
st3.append(st2)
print ' '.join(st3)

Reverse a sentence in python

st = "This is awesome!"
st1 = ''
ls = len(st)
for x in range(len(st)):
st1 = st1 + st[ls - 1- x]
print st1

Reverse a string - Python

st = "awesome!"
st1 = ''
ls = len(st)
for x in range(len(st)):
st1 = st1 + st[ls - 1- x]
print st1

Length of a string with out len() function - Python

st = " This is awesome!"
count = 0

for x in st:
count = count + 1
print count
if(len(st) == count):
print "it works!"

Java program to print two largest numbers

Java program to print maximum of two numbers:

package caveof;

public class Maxtwo {

public void twoMaxNumbers(int[] nums){
int maxone = 0;
int maxtwo = 0;
for(int n:nums){
if(maxone < n){
maxtwo = maxone;
maxone = n;
}
else if(maxtwo < n){
maxtwo = n;
}
}
System.out.println("First Max Number: "+maxone);
        System.out.println("Second Max Number: "+maxtwo);

}

public static void main(String[] args) {
// TODO Auto-generated method stub
int num[] = {5,34,78,2,45,1,99,23};
Maxtwo twomn = new Maxtwo();
twomn.twoMaxNumbers(num);

}
}

Sunday, May 3, 2015

Sharing work flows of Rapid miner

Sharing Rapid miner process :

To share process in rapid miner with fellow data scientists around the world, rapid miner has an extension that supports sharing processes.
Before sharing we have to install the necessary packages.

Open Rapid miner ,
Helpà Update and Extensions









A new window gets opened.Enter comminity in the search text box and select”community extensions”
Install the packages.
Restart the rapid miner if asked.






Click on View menuà Show view àMy Expirement Browser



A modal appears with the name “Myexpirement browser”

Click on Login  and enter the details.
Now open any process from the repositories tab .
Click on upload button  in the MyExpirement Browser.


Enter the title and other details.
In Sharing Permissions, is for who can view and download our work flows.
Note:  To set work flow permissions we can do it anytime in myexpirement.org site.
Click ok.
Now open MyExpirement.org in a browser.
And check for the work flows uploaded by clicking on “MyWorkflows”


The below figure lists the work flows that has been uploaded in the user account.


Select any work flow and click on “Manage work flow” for managing work flow permissions, description etc.


Thursday, March 12, 2015

Deleting an uninstalled app from listing in my apps for android

Deleting an uninstalled app from appearing in My apps in the web page for an android device.

In android even you have not installed an app in your device it still displays as installed and lists in my apps section in play store account.
To delete the display of listing uninstalled apps under My apps follow the steps below:
1.Go to Playstore in your device.
2. Select My apps àAll
3.Select the app that you don’t want to be listed and which is not installed in your device.
4.Choose the close button available
5.It throws a popup to confirm your deletion
6.Select confirm

Refresh the page app is not displayed in My apps in your device  and also in the webpage.


Friday, February 13, 2015

Automation of naukri profile update - Selenium

This script uploads ur profile at naukri.com with out using AutoIt

package Selenium_script;

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.awt.Toolkit;
import java.awt.datatransfer.StringSelection;
import java.io.File;
import java.net.URISyntaxException;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class Upload {

public static String naukari = "https://login.naukri.com/nLogin/Login.php";
public static String nauk = "http://www.naukri.com/";
public static String upload = ".//*[@id='colL']/div[2]/div[1]/a[2]";
public static String view = ".//*[@id='colL']/div[2]/div[1]/span";
public static String view1 = ".//*[@id='colL']/div[2]/div[1]/a[1]";
public static String up_prof = ".//*[@id='uploadLink']";
public static String save = ".//*[@id='editForm']/div[8]/button";
public static String mynauk = ".//*[@id='mainHeader']/div/div/ul[2]/li[2]/a/div[2]";

public static void setClipboardData(String st) {

  StringSelection stringSelection = new StringSelection(st);
  Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stringSelection, null);
}

public static void main(String[] args) throws AWTException {




WebDriver dri = new FirefoxDriver();
dri.get(nauk);


dri.findElement(By.xpath(".//*[@id='login_Layer']/div")).click();
WebElement fra = dri.findElement(By.xpath(".//*[@id='loginLB']/div[2]"));

fra.click();

dri.findElement(By.id("eLogin")).sendKeys("yourusername"); //Enter your username
dri.findElement(By.id("pLogin")).sendKeys("yourpassword"); // Enter your password
dri.findElement(By.xpath(".//*[@id='lgnFrm']/div[7]/button")).click();
dri.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);


dri.manage().window().maximize();

//Logged in
//Clicking on view profile
WebElement view_prof = dri.findElement(By.xpath(view1));
Actions builder = new Actions(dri);
builder.click(view_prof).build().perform();



//Clicking on upload link
WebElement prof_upload = dri.findElement(By.xpath(up_prof));
prof_upload.click();

WebElement attachcv = dri.findElement(By.xpath(".//*[@id='attachCV']"));
Actions builder1 = new Actions(dri);
builder1.click(attachcv).build().perform();



//upload the file
//The below method calls the setclipboard method and put the file path in clipboard

setClipboardData("Path to your resume file");

//The below lines of code will paste the clipboard content and click open button in the modal window

Robot robot = new Robot();
robot.delay(500);

robot.keyPress(KeyEvent.VK_CONTROL); //Press control key
robot.keyPress(KeyEvent.VK_V); //Press key v
robot.keyRelease(KeyEvent.VK_V); // Release "v"
robot.keyRelease(KeyEvent.VK_CONTROL); //Release ctrl key
robot.keyPress(KeyEvent.VK_ENTER); //Press enter key that clicks the open button in the modal
robot.keyRelease(KeyEvent.VK_ENTER); //Release the enter key
robot.delay(1000); //Delay in milli seconds

//Closing the modal window of upload file
WebElement sav_btn = dri.findElement(By.xpath(save));
sav_btn.click();

//Log out button



WebElement my_naukri = dri.findElement(By.xpath(mynauk));
WebElement log_out = dri.findElement(By.xpath(".//*[@id='mainHeader']/div/div/ul[2]/li[2]/div/ul/li[5]/a"));
Actions builder3 = new Actions(dri);
builder3.moveToElement(my_naukri).click(log_out).build().perform();







}

}

Wednesday, January 14, 2015

Selenium script to get table values

package Ecomm_selenium;

import java.util.List;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Tablevalues {

       /**
       * @param args
       */
       public static void main(String[] args) {
              // TODO Auto-generated method stub
           //String paths = ".//*[@id='fk-mainbody-id']/div/div[2]";       
           //String
             
              WebDriver d = new FirefoxDriver();
              d.get(site);
              //WebElement webtable = d.findElement(By.xpath(".//*[@id='grey-box']/div"));
              int RowIndex = 3;
              List<WebElement> Rowcount = d.findElements(By.xpath(".//*[@id='grey-box']/div/div["+RowIndex+"]"));
             
              System.out.println("No: of Rows in table:" + Rowcount.size());
                    
              //for(WebElement rowelem : Rowcount)
              for(int i=0; i <= RowIndex; i++)
              {
                     int ColIndex = 3;
                     for(int j = 0; j <= ColIndex; j++){
                           WebElement value = d.findElement(By.xpath(".//*[@id='grey-box']/div/div["+i+"]/div["+j+"]/p"));
                           System.out.println("Row" + i + "Column" + j + "Data" + value.getText());
                     }
                    
                     
              }
              d.quit();
       }
      
}

Selenium script to get list of values

//code to get list of values

package Ecomm_selenium;
import java.util.List;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

//Getting list of webElements
public class Login {

                /**
                * @param args
                */
                public static void main(String[] args) {
                                // TODO Auto-generated method stub
                               
                                String site = "http://www.flipkart.com/";
                    String paths = ".//*[@id='fk-mainbody-id']/div/div[2]";                            
                    //String
                               
                                WebDriver d = new FirefoxDriver();
                                d.get(site);
                                List<WebElement> colu_vals = d.findElements(By.className("goquickly-list"));
                                java.util.Iterator<WebElement> i = colu_vals.iterator();
                                while(i.hasNext())
                                {
                                                WebElement value = i.next();
                                                System.out.println(value.getText());
                                               
                                }
                               
                               
                }

}