babellog

Friday, July 30, 2004

Python + Oracle (2)

Wow!! I didn't think getting connected to Oracle from Python would be this easy. Came across a few hickups early on, but I'm impressed!!

One was that I had Oracle 8 client. Therefore it kept on giving me the following error

ImportError: DLL load failed: The specified procedure could not be found.

Since I had downloaded the latest cx_Oracle I needed an Oracle 9i client. The resident BOFH was kind enough to point me here here, you'll need a OTN account to down load it. Just unzip the stuff there. It comes with OCI.dll. and a number of .jar which are not needed I think.

Next problem was where to setup the tnsnames.ora file. Since it didn't pick up the one in the old net/admin dir (dumb idea) didn't work, I tried putting it in the current directory, and it worked!!!

the code snippet

import cx_Oracle

connection = cx_Oracle.connect("username", "password", "tns")

cursor = connection.cursor()
cursor.arraysize = 50
cursor.execute("""select instance_name from v$instance""")
for x in cursor.fetchall():
print x[0]

Loop Spike



I was once scanning an InputStream where the end of stream is not known. Only way out was kind of knowing what is the Character or string pattern that will be at the end of the stream and stop reading from the stream as soon as you encounter that pattern (You will encounter situation when you use TelnetClient from Jakarta Commons Net). Otherwise since InputStream in Java was blocking (prior to non-blocking IO was introduced) your code will get stuck at read() method and will not get out. I was not always sure what the string pattern the stream would end with. Anyway cut the story short, I ended up scanning the stream in a loop and introduced timeout to get out of the loop if I don't find what I am looking for.

This introduced a new problem, the processor utilization spiked to 100% (Obviously :-)) But introducing a Thread.sleep(1) brought back the processor back to normal. Since I can't replicate the whole code in this blog, here is a loop that checks for an existence of a file and reports you (if you add a System.out.println around f.exists()) as soon as it is created. Run the code with the comments on and comments off and check the processor utilization.

Who knows, it might become handy when you are trying to scan a stream from a telecom switch far away in a pacific island :-)

import java.io.File;


 
public class LoopSpike {
public LoopSpike() {
}

public static void main(String[] args) {
File f = new File("test.txt");
while (true){

f.exists();
// try {
// Thread.sleep(1);
// }
// catch (InterruptedException ex) {
// }
}
}

}

Thursday, July 29, 2004

Paul Graham says ...

Paul Graham says...

"The programmers you'll be able to hire to work on a Java project won't be as smart as the ones you could get to work on a project written in Python. And the quality of your hackers probably matters more than the language you choose. Though, frankly, the fact that good hackers prefer Python to Java should tell you something about the relative merits of those languages"

from the rest of the "essay", he seems to spend a lot of time slamming Java for no apparent reason. Of course, he also thinks that

"... you find that open source operating systems already have a dominant market share, and the number one language is probably Perl."

But why does Java suck ? I've gotten used to it over the years and while I would rarely program in it voluntarily, I'm sure it doesn't deserve the abuse.

Python + Oracle

Going to get my fingers dirty with python after a long time. I think I have forgotten the little I learnt due to my time being taken up by other mundane activities. Computronix Python/Oracle Interface: cx_Oracle, provide Oracle conectivity from Python. I still haven't used it, I wonder if there are any other such libraries.


And Python 2.3.4 is out.

Initialised

Ummmmm......