Tuesday, September 24, 2013

Java thread wait notify alternate text print


package com.cisco;

import java.util.concurrent.locks.Lock;

public class Threads {

    private Object lock= new Object();
    private boolean flag = false;
    private Runnable thread1 ;
    private Runnable thread2 ;
    
    public static void main(String[] args) throws InterruptedException {
        Threads threads  = new Threads();
        threads.thread1 =  threads.new MyRunnable();
        threads.thread2 = threads.new MyRunnableT();
        Thread thread1 = new Thread(threads.thread1);
        Thread thread2 = new Thread(threads.thread2);
        thread1.start();
        thread2.start();
        try {
            thread1.join();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println("thread 1 joined");
        thread2.join();
        System.out.println("end");
        
    }
    
    class MyRunnable implements Runnable {

        @Override
        public void run() {
            for (int i =0 ;i<10;i++){
            synchronized(lock){
                System.out.println("I have the lock" + Thread.currentThread().getName());
                try {
                    if(flag==false)
                    {// if flag == false you wait for it to be true
                        
                        lock.wait();
                        // do whatever you want
                        //s et flag to false
                        flag = false;
                        lock.notify();
                    }    
                    else {
                        // flag is true so do whatever you want and then set it to false
                        // do what ever you want 
                        flag= true;
                        lock.notify();
                        
                        }
                    } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                }
            }
        }
        
    }
    
    class MyRunnableT implements Runnable {

        @Override
        public void run() {
            for (int i =0 ;i<10;i++){
            synchronized(lock){
                System.out.println("I have the lock" + Thread.currentThread().getName());
                
                    if(flag==true)
                        {
                        // do what ever you want and then set the flag to true
                        // and then notify the other thread.
                        flag = false;
                        lock.notify();
                        
                        }
                    else {
                        // if flag is true you wait for it to be false
                        // then do whatever you want and then set it to true
                        try {
                            
                            // do whatever you want
                            flag = false;
                            lock.notify();
                            lock.wait();
                            
                        } catch (Exception e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                
                }
            }
        }
        
    }
}

Tuesday, September 17, 2013

'ascii' codec can't encode character u'\u2019' in position 16: ordinal not in range(128)


http://stackoverflow.com/questions/3224268/python-unicode-encode-error

Likely, the problem is that you parsed it okay, and now you're trying to print the contents of the XML and you can't because theres some foreign Unicode characters. Try to encode your unicode string as ascii first:
unicodeData.encode('ascii', 'ignore')

HTTP Request with headers in Python

#!/usr/bin/env python

import requests
import xml.etree.ElementTree as ET

headers = {'key':'value'}

r = requests.get('www.google.com',auth=('username', 'password'),headers=headers)

print r.text

root = ET.fromstring ( r.text.encode('ascii','ignore'))

for xyz in  root.findall('POPULARITY_RECORD'):
        neoUrl = xyz.find('FIELD_VALUE').text
        print neoUrl
        httpstatus_neo = requests.get(neoUrl)
        print httpstatus_neo.status_code
# print r.text

Reading XML in python

#!/usr/bin/env python

import xml.etree.ElementTree as ET
import httplib
import urllib2
tree = ET.parse('popularity_response.xml')
root = tree.getroot()
print root
for popularity_record in root.findall('POPULARITY_RECORD'):
 print popularity_record.find('FIELD_VALUE').text
 try : 
  response =     urllib2.urlopen(popularity_record.find('FIELD_VALUE').text)
  print response.code
 except urllib2.URLError as e :
  print e
  continue

Tuesday, September 3, 2013

Configuring Oracle for Python

Configuring Oracle for Python
Python code for testing ORacle DB COnnectivity

# assuming python and pip are already installed
# installing the instantclient is usually where problems happen
# download the following files from oracle
# oracle-instantclient11.2-basic-11.2.0.3.0-1.x86_64.rpm
# oracle-instantclient11.2-devel-11.2.0.3.0-1.x86_64.rpm
# oracle-instantclient11.2-sqlplus-11.2.0.3.0-1.x86_64.rpm
# install the rpms
rpm -ivh oracle-instantclient11.2-basic-11.2.0.3.0-1.x86_64.rpm
rpm -ivh oracle-instantclient11.2-sqlplus-11.2.0.3.0-1.x86_64.rpm
rpm -ivh oracle-instantclient11.2-devel-11.2.0.3.0-1.x86_64.rpm
# the sqlplus package isn't specifically needed, but is usually useful for testing and command line sql connections
# configure oracle env (modify exact path based on version of rpm you download)
vim /etc/profile.d/oracle
#!/bin/bash
LD_LIBRARY_PATH="/usr/lib/oracle/11.2/client64/lib:${LD_LIBRARY_PATH}"
export LD_LIBRARY_PATH
TNS_ADMIN="/etc/oracle"
export TNS_ADMIN
ORACLE_HOME="/usr/lib/oracle/11.2/client64/lib"
export ORACLE_HOME
# copy/create your tnsnames.ora file
touch /etc/oracle/tnsnames.ora
# symlink headers to ORACLE_HOME to avoid "cannot locate Oracle include files" error
mkdir /usr/lib/oracle/11.2/client64/lib/sdk
ln -s /usr/include/oracle/11.2/client64 /usr/lib/oracle/11.2/client64/lib/sdk/include
# done. Install cx_Oracle
pip install cx_Oracle
----------------------------------------------------------------------------------------
import os
import cx_Oracle
import csv
SQL="SELECT * FROM SOME_TABLE"
# Network drive somewhere
filename="S:\Output.csv"
FILE=open(filename,"w");
output=csv.writer(FILE, dialect='excel')
# You can set these in system variables but just in case you didnt
os.putenv('ORACLE_HOME', '/oracle/product/10.2.0/db_1')
os.putenv('LD_LIBRARY_PATH', '/oracle/product/10.2.0/db_1/lib')
connection = cx_Oracle.connect('userid/password@99.999.9.99:PORT/SID')
cursor = connection.cursor()
cursor.execute(SQL)
for row in cursor:
    output.writerow(row)
cursor.close()
connection.close()
FILE.close()