From: Darius K. <d.k...@gm...> - 2009-08-12 10:04:55
|
Hi, I am writing jython application with eclipse SWT/JFace. I have to pass float array to java object to get back some values from it. I am using jarray package for it. Is there more pythonic way to do it? --->8 code 8<--- bounds = zeros(4, 'f') # from java org.eclipse.swt.graphics.Path.getBounds(float[] bounds) path.getBounds(bounds) # from java org.eclipse.swt.graphics.Rectangle(int x, int y, int width,int height) rect = Rectangle(int(round(bounds[0])), int(round(bounds[1])), int(round(bounds[2])), int(round(bounds[3]))) --->8 code 8<--- Thanks in advance! -- Best Regards Darius Kučinskas [http://blog-of-darius.blogspot.com/] |
From: Frank W. <fwi...@gm...> - 2009-08-12 13:22:03
|
You can at least reduce the 4 calls to int(round(...)) using a list comprehension: rounded = [int(round(x)) for x in bounds] -Frank |
From: Charlie G. <cha...@gm...> - 2009-08-13 21:26:34
|
2009/8/12 Darius Kučinskas <d.k...@gm...>: > Hi, > > I am writing jython application with eclipse SWT/JFace. I have to pass float > array to java object to get back some values from it. I am using jarray > package for it. Is there more pythonic way to do it? > > --->8 code 8<--- > bounds = zeros(4, 'f') > > > # from java org.eclipse.swt.graphics.Path.getBounds(float[] bounds) > path.getBounds(bounds) Jython should convert python lists to Java arrays, so I think you can do path.getBounds([0.0, 0.0, 0.0, 0.0]) > # from java org.eclipse.swt.graphics.Rectangle(int x, int y, int width,int > height) > rect = Rectangle(int(round(bounds[0])), > > > int(round(bounds[1])), > > > int(round(bounds[2])), > > > int(round(bounds[3]))) You can combine Frank's list comprehension with an * to explode the arguments to make this rect = Rectangle(*[int(round(x)) for x in bounds]) Charlie |
From: Frank W. <fwi...@gm...> - 2009-08-13 23:39:27
|
2009/8/13 Charlie Groves <cha...@gm...>: > You can combine Frank's list comprehension with an * to explode the > arguments to make this rect = Rectangle(*[int(round(x)) for x in > bounds]) Ah excellent suggestion! There OP put this question to stackoverflow as well, I'll add that part in. -Frank |