From: Paul D. <pau...@ds...> - 2006-12-12 22:56:14
|
Hi Guys, I have been spending what little spare time I have looking at the csv module. I'm afraid I haven't got very far yet as I am currently smack bang in the middle of a very steep learning curve! I am making progress however and am still as keen as ever! Currently, I am trying to understand the correct way to parse the arguments for csv.reader(). The method's definition from the python library docs is: reader(csvfile[, dialect='excel'[, fmtparam]]) As csvfile is mandatory I thought it would be okay to specify it explicitly in my method signature, then use optional arguments for the rest. With a little help from the Wiki FAQ I ended up with this: public static PyObject reader(PyObject iterableObject, PyObject[] args, String[] keywords) { } But here is where I get confused because now Jython *EXPECTS* 3 arguments which is not how Python behaves: >>> import csv >>> csv.reader() Traceback (most recent call last): File "<stdin>", line 1, in ? TypeError: expected at least 1 arguments, got 0 jython: >>> import csv >>> csv.reader() Traceback (innermost last): File "<console>", line 1, in ? TypeError: reader(): expected 3 args; got 0 >>> So to make csv.reader() behave the same as python, I'd need to pass an array of PyObjects into reader() then use ArgParser and do my own type checking and error handling: public static PyObject reader(PyObject[] args) { //Use ArgParser and my own error handling to throw if there isn't //at least one argument + other checks. } I guess it's easier to explicitly specify arguments when the module hides behind a python wrapper then you already know the number of arguments the wrapper uses. I can't do this for csv as the reader() method is exposed directly through the csv.py wrapper so it must support dynamic arguments. Does all of this make sense or am I getting it completely wrong? Cheers, Paul. |