Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #11286 > unrolled thread
| Started by | kj <no.email@please.post> |
|---|---|
| First post | 2011-08-12 17:02 +0000 |
| Last post | 2011-08-15 16:16 +0000 |
| Articles | 9 — 9 participants |
Back to article view | Back to comp.lang.python
Java is killing me! (AKA: Java for Pythonheads?) kj <no.email@please.post> - 2011-08-12 17:02 +0000
Re: Java is killing me! (AKA: Java for Pythonheads?) Nathan Rice <nathan.alexander.rice@gmail.com> - 2011-08-12 13:15 -0400
Re: Java is killing me! (AKA: Java for Pythonheads?) MRAB <python@mrabarnett.plus.com> - 2011-08-12 18:35 +0100
Re: Java is killing me! (AKA: Java for Pythonheads?) rav <rafalgulinski@gmail.com> - 2011-08-13 06:17 -0700
Re: Java is killing me! (AKA: Java for Pythonheads?) Alain Ketterlin <alain@dpt-info.u-strasbg.fr> - 2011-08-12 19:45 +0200
Re: Java is killing me! (AKA: Java for Pythonheads?) Miki Tebeka <miki.tebeka@gmail.com> - 2011-08-12 11:18 -0700
Re: Java is killing me! (AKA: Java for Pythonheads?) Chris Angelico <rosuav@gmail.com> - 2011-08-12 21:39 +0100
Re: Java is killing me! (AKA: Java for Pythonheads?) Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2011-08-12 23:31 -0700
Re: Java is killing me! (AKA: Java for Pythonheads?) Dirk Olmes <dirk@xanthippe.ping.de> - 2011-08-15 16:16 +0000
| From | kj <no.email@please.post> |
|---|---|
| Date | 2011-08-12 17:02 +0000 |
| Subject | Java is killing me! (AKA: Java for Pythonheads?) |
| Message-ID | <j23mbe$nfe$1@reader1.panix.com> |
*Please* forgive me for asking a Java question in a Python forum.
My only excuse for this no-no is that a Python forum is more likely
than a Java one to have among its readers those who have had to
deal with the same problems I'm wrestling with.
Due to my job, I have to port some Python code to Java, and write
tests for the ported code. (Yes, I've considered finding myself
another job, but this is not an option in the immediate future.)
What's giving me the hardest time is that the original Python code
uses a lot of functions with optional arguments (as is natural to
do in Python).
As far as I can tell (admittedly I'm no Java expert, and have not
programmed in it since 2001), to implement a Java method with n
optional arguments, one needs at least 2**n method definitions.
Even if all but one of these definitions are simple wrappers that
call the one that does all the work, it's still a lot of code to
wade through, for nothing.
That's bad enough, but even worse is writing the unit tests for
the resulting mountain of fluffCode. I find myself writing test
classes whose constructors also require 2**n definitions, one for
each form of the function to be tested...
I ask myself, how does the journeyman Python programmer cope with
such nonsense?
For the sake of concreteness, consider the following run-of-the-mill
Python function of 3 arguments (the first argument, xs, is expected
to be either a float or a sequence of floats; the second and third
arguments, an int and a float, are optional):
def quant(xs, nlevels=MAXN, xlim=MAXX):
if not hasattr(xs, '__iter__'):
return spam((xs,), n, xlim)[0]
if _bad_quant_args(xs, nlevels, xlim):
raise TypeError("invalid arguments")
retval = []
for x in xs:
# ...
# elaborate acrobatics that set y
# ...
retval.append(y)
return retval
My Java implementation of it already requires at least 8 method
definitions, with signatures:
short[] quant (float[], int , float)
short[] quant (float[], int )
short[] quant (float[], float)
short[] quant (float[] )
short quant (float , int , float)
short quant (float , int )
short quant (float , float)
short quant (float )
Actually, for additional reasons, too arcane to go into, I also
need four more:
short quant (Float , Integer, Float)
short quant (Float , Integer )
short quant (Float , Float)
short quant (Float )
Writing JUnit tests for these methods is literally driving me
INSANE.
Some advice on implementing and testing functions with optional
arguments in Java would be appreciated.
TIA!
kj
[toc] | [next] | [standalone]
| From | Nathan Rice <nathan.alexander.rice@gmail.com> |
|---|---|
| Date | 2011-08-12 13:15 -0400 |
| Message-ID | <mailman.2226.1313169309.1164.python-list@python.org> |
| In reply to | #11286 |
<SNIP>
public FooClass(String requiredArgument1, Long requiredArgument2, map
yourOptionalArgumentMap) {
...
}
[toc] | [prev] | [next] | [standalone]
| From | MRAB <python@mrabarnett.plus.com> |
|---|---|
| Date | 2011-08-12 18:35 +0100 |
| Message-ID | <mailman.2227.1313170612.1164.python-list@python.org> |
| In reply to | #11286 |
On 12/08/2011 18:02, kj wrote:
>
>
>
> *Please* forgive me for asking a Java question in a Python forum.
> My only excuse for this no-no is that a Python forum is more likely
> than a Java one to have among its readers those who have had to
> deal with the same problems I'm wrestling with.
>
> Due to my job, I have to port some Python code to Java, and write
> tests for the ported code. (Yes, I've considered finding myself
> another job, but this is not an option in the immediate future.)
>
> What's giving me the hardest time is that the original Python code
> uses a lot of functions with optional arguments (as is natural to
> do in Python).
>
> As far as I can tell (admittedly I'm no Java expert, and have not
> programmed in it since 2001), to implement a Java method with n
> optional arguments, one needs at least 2**n method definitions.
> Even if all but one of these definitions are simple wrappers that
> call the one that does all the work, it's still a lot of code to
> wade through, for nothing.
>
> That's bad enough, but even worse is writing the unit tests for
> the resulting mountain of fluffCode. I find myself writing test
> classes whose constructors also require 2**n definitions, one for
> each form of the function to be tested...
>
> I ask myself, how does the journeyman Python programmer cope with
> such nonsense?
>
> For the sake of concreteness, consider the following run-of-the-mill
> Python function of 3 arguments (the first argument, xs, is expected
> to be either a float or a sequence of floats; the second and third
> arguments, an int and a float, are optional):
>
> def quant(xs, nlevels=MAXN, xlim=MAXX):
> if not hasattr(xs, '__iter__'):
> return spam((xs,), n, xlim)[0]
>
> if _bad_quant_args(xs, nlevels, xlim):
> raise TypeError("invalid arguments")
>
> retval = []
> for x in xs:
> # ...
> # elaborate acrobatics that set y
> # ...
> retval.append(y)
>
> return retval
>
> My Java implementation of it already requires at least 8 method
> definitions, with signatures:
>
[snip]
I would declare:
short[] quant (float[], int , float)
short quant (Float , Integer, Float)
and see how it goes.
"float" and "int" should be boxed to "Float" and "Integer"
automatically.
If the second and third arguments are frequently the default, then I
would also declare:
short[] quant (float[])
short quant (Float )
[toc] | [prev] | [next] | [standalone]
| From | rav <rafalgulinski@gmail.com> |
|---|---|
| Date | 2011-08-13 06:17 -0700 |
| Message-ID | <b5c772aa-39d7-4dd8-8749-ce106441c2d7@m18g2000vbl.googlegroups.com> |
| In reply to | #11293 |
On Aug 12, 1:35 pm, MRAB <pyt...@mrabarnett.plus.com> wrote:
> On 12/08/2011 18:02, kj wrote:
>
>
>
>
>
>
>
>
>
> > *Please* forgive me for asking a Java question in a Python forum.
> > My only excuse for this no-no is that a Python forum is more likely
> > than a Java one to have among its readers those who have had to
> > deal with the same problems I'm wrestling with.
>
> > Due to my job, I have to port some Python code to Java, and write
> > tests for the ported code. (Yes, I've considered finding myself
> > another job, but this is not an option in the immediate future.)
>
> > What's giving me the hardest time is that the original Python code
> > uses a lot of functions with optional arguments (as is natural to
> > do in Python).
>
> > As far as I can tell (admittedly I'm no Java expert, and have not
> > programmed in it since 2001), to implement a Java method with n
> > optional arguments, one needs at least 2**n method definitions.
> > Even if all but one of these definitions are simple wrappers that
> > call the one that does all the work, it's still a lot of code to
> > wade through, for nothing.
>
> > That's bad enough, but even worse is writing the unit tests for
> > the resulting mountain of fluffCode. I find myself writing test
> > classes whose constructors also require 2**n definitions, one for
> > each form of the function to be tested...
>
> > I ask myself, how does the journeyman Python programmer cope with
> > such nonsense?
>
> > For the sake of concreteness, consider the following run-of-the-mill
> > Python function of 3 arguments (the first argument, xs, is expected
> > to be either a float or a sequence of floats; the second and third
> > arguments, an int and a float, are optional):
>
> > def quant(xs, nlevels=MAXN, xlim=MAXX):
> > if not hasattr(xs, '__iter__'):
> > return spam((xs,), n, xlim)[0]
>
> > if _bad_quant_args(xs, nlevels, xlim):
> > raise TypeError("invalid arguments")
>
> > retval = []
> > for x in xs:
> > # ...
> > # elaborate acrobatics that set y
> > # ...
> > retval.append(y)
>
> > return retval
>
> > My Java implementation of it already requires at least 8 method
> > definitions, with signatures:
>
> [snip]
>
> I would declare:
>
> short[] quant (float[], int , float)
> short quant (Float , Integer, Float)
>
> and see how it goes.
>
> "float" and "int" should be boxed to "Float" and "Integer"
> automatically.
>
> If the second and third arguments are frequently the default, then I
> would also declare:
>
> short[] quant (float[])
> short quant (Float )
This seems to be a very good solution but I would replace basic types
with wrappers
short[] quant (float[], Integer, Float)
short quant (Float , Integer, Float)
When you implement those two methods (where you need to check if
Integer, Float are not null) then you can define two more methods:
short[] quant (float[]) {
return quant (float[], null, null);
}
short quant (float) {
return quant (float, null, null);
}
That gives you 2 methods for unit testing.
[toc] | [prev] | [next] | [standalone]
| From | Alain Ketterlin <alain@dpt-info.u-strasbg.fr> |
|---|---|
| Date | 2011-08-12 19:45 +0200 |
| Message-ID | <87k4ai8qwk.fsf@dpt-info.u-strasbg.fr> |
| In reply to | #11286 |
kj <no.email@please.post> writes: [...] > def quant(xs, nlevels=MAXN, xlim=MAXX): [...] > My Java implementation of it already requires at least 8 method > definitions, with signatures: (BTW, your approach won't work if several optionals have the same type.) [...] - use Integer, Float, etc. everywhere. The compiler will box primitive types as needed at call sites - re. default values (on positional params), use null to indicate a use of the default, or use ellipsis, or use class-provided static fields at call sites, or use a special class to represent all the optional parameters - re. named parameters with default, use a map, or change the call sites to remove them -- Alain.
[toc] | [prev] | [next] | [standalone]
| From | Miki Tebeka <miki.tebeka@gmail.com> |
|---|---|
| Date | 2011-08-12 11:18 -0700 |
| Message-ID | <04e60610-b887-49f9-940f-7b4b507ae466@glegroupsg2000goo.googlegroups.com> |
| In reply to | #11286 |
You can probably do that with varargs.
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2011-08-12 21:39 +0100 |
| Message-ID | <mailman.2237.1313181576.1164.python-list@python.org> |
| In reply to | #11286 |
On Fri, Aug 12, 2011 at 6:02 PM, kj <no.email@please.post> wrote: > I ask myself, how does the journeyman Python programmer cope with > such nonsense? > Firstly, figure out how many combinations of optional arguments actually make sense. Any that don't, don't support. That may well cut it down significantly. And then, if there are any that make sense but will be really rare (eg if you allow optional specification of a max and a min, and most times you'll use either both bounds or neither), you can save a few by having the "optional" argument specified with a sentinel meaning "default". It's ugly in a few places to justify simplicity in most. If you really need 100% flexibility, then the suggestions already posted will definitely be the best. ChrisA
[toc] | [prev] | [next] | [standalone]
| From | Dennis Lee Bieber <wlfraed@ix.netcom.com> |
|---|---|
| Date | 2011-08-12 23:31 -0700 |
| Message-ID | <mailman.2250.1313217094.1164.python-list@python.org> |
| In reply to | #11286 |
On Fri, 12 Aug 2011 17:02:38 +0000 (UTC), kj <no.email@please.post>
declaimed the following in gmane.comp.python.general:
>
> As far as I can tell (admittedly I'm no Java expert, and have not
> programmed in it since 2001), to implement a Java method with n
> optional arguments, one needs at least 2**n method definitions.
> Even if all but one of these definitions are simple wrappers that
> call the one that does all the work, it's still a lot of code to
> wade through, for nothing.
>
Does this apply?
http://download.oracle.com/javase/1,5.0/docs/guide/language/varargs.html
--
Wulfraed Dennis Lee Bieber AF6VN
wlfraed@ix.netcom.com HTTP://wlfraed.home.netcom.com/
[toc] | [prev] | [next] | [standalone]
| From | Dirk Olmes <dirk@xanthippe.ping.de> |
|---|---|
| Date | 2011-08-15 16:16 +0000 |
| Message-ID | <j2bgo9$but$1@news01.versatel.de> |
| In reply to | #11286 |
On Fri, 12 Aug 2011 17:02:38 +0000, kj wrote: > *Please* forgive me for asking a Java question in a Python forum. My > only excuse for this no-no is that a Python forum is more likely than a > Java one to have among its readers those who have had to deal with the > same problems I'm wrestling with. > > Due to my job, I have to port some Python code to Java, and write tests > for the ported code. (Yes, I've considered finding myself another job, > but this is not an option in the immediate future.) Can't you sidestep the porting effort and try to run everything in Jython on the JVM? -dirk (Python lurker with Java experience)
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web