OpenOffice.org Forum at OOoForum.orgThe OpenOffice.org Forum
 
 [Home]   [FAQ]   [Search]   [Memberlist]   [Usergroups]   [Register
 [Profile]   [Log in to check your private messages]   [Log in

calc addin with python returning array problem

 
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API
View previous topic :: View next topic  
Author Message
kurt.forrester
General User
General User


Joined: 21 Jun 2011
Posts: 5

PostPosted: Tue Jun 21, 2011 7:02 am    Post subject: calc addin with python returning array problem Reply with quote

Hi All,

I am trying to write an addin for calc using python. I have found an example on the internet describing the basics (which I can reproduce for a simple case) that returns a single value. However when I try to return an array I get a #VALUE! error. I have included the information from the idl and python files to show the changes. I hope somebody can shed some light on the issue I am having as I find there is very little on python as the addin language and nothing on returning arrays with python.
##idl (working)

#include <com/sun/star/uno/XInterface.idl>

module com { module python { module numpy { module random {

interface XNPrandom
{
double triangular( [in] double left, [in] double mode , [in] double right, [in] any size );
};

}; }; }; };

##python (working)
import uno
import unohelper
from com.python.numpy.random import XNPrandom
import numpy as np

class randomImpl( unohelper.Base, XNPrandom ):
def __init__( self, ctx ):
self.ctx = ctx

def triangular( self, left, mode, right, size=None ):
return np.random.triangular(left,mode,right,size)

def createInstance( ctx ):
return randomImpl( ctx )

g_ImplementationHelper = unohelper.ImplementationHelper()
g_ImplementationHelper.addImplementation( \
createInstance,"com.python.numpy.random.python.randomImpl",

("com.sun.star.sheet.AddIn",),)

##idl (not working)
#include <com/sun/star/uno/XInterface.idl>

module com { module python { module numpy { module random {

interface XNPrandom
{
sequence< sequence< double > > triangular( [in] double left, [in] double mode , [in] double right, [in] any size );
};

}; }; }; };

##python (not working)

import uno
import unohelper
from com.python.numpy.random import XNPrandom
import numpy as np

class randomImpl( unohelper.Base, XNPrandom ):
def __init__( self, ctx ):
self.ctx = ctx

def triangular( self, left, mode, right, size=None ):
return tuple(np.random.triangular(left,mode,right,size))

def createInstance( ctx ):
return randomImpl( ctx )

g_ImplementationHelper = unohelper.ImplementationHelper()
g_ImplementationHelper.addImplementation( \
createInstance,"com.python.numpy.random.python.randomImpl",
("com.sun.star.sheet.AddIn",),)
Back to top
View user's profile Send private message
Villeroy
Super User
Super User


Joined: 04 Oct 2004
Posts: 10106
Location: Germany

PostPosted: Tue Jun 21, 2011 8:40 am    Post subject: Reply with quote

=OFFSET(Sheet1.A1;0;0;2;3) returns #VALUE as well since this function call is supposed to return 2x3 values (A1:C2) but a cell can take only one value. You've got to enter that formula as an array formula which can return more than one value at once.
_________________
Rest in peace, oooforum.org
Get help on https://forum.openoffice.org
Back to top
View user's profile Send private message
kurt.forrester
General User
General User


Joined: 21 Jun 2011
Posts: 5

PostPosted: Tue Jun 21, 2011 8:46 am    Post subject: Reply with quote

Villeroy thank you for your response. I am familiar with array functions and have been entering the formula as an array ({}) and it is still returning #VALUE!.

Have you any other suggestions?

Kind Regards,

Kurt
Back to top
View user's profile Send private message
Villeroy
Super User
Super User


Joined: 04 Oct 2004
Posts: 10106
Location: Germany

PostPosted: Tue Jun 21, 2011 8:53 am    Post subject: Reply with quote

http://www.oooforum.org/forum/viewtopic.phtml?t=59534 (convert the tuple of tuples into a 2-dimensional structure)
_________________
Rest in peace, oooforum.org
Get help on https://forum.openoffice.org
Back to top
View user's profile Send private message
kurt.forrester
General User
General User


Joined: 21 Jun 2011
Posts: 5

PostPosted: Wed Jun 22, 2011 12:07 am    Post subject: Reply with quote

I admit that I am following the instructions on the post blindly as I do not quite understand the overall architecture but I still cannot get a satisfactory result. I do feel I am closer to the answer but still not quite there.

Here is what I have done... I have removed the real workings of my python function and replaced it with something that used native python types:
Code:

import uno
import unohelper
from com.python.numpy.random import XNPrandom
import numpy as np

class randomImpl( unohelper.Base, XNPrandom ):
    def __init__( self, ctx ):
        self.ctx = ctx

    def triangular( self, left, mode, right, size=None ):
        wrapper = uno.createUnoStruct('com.sun.star.script.ArrayWrapper')
        row = []
        for i in range(3):
            column = []
            for j in range(3):
                column.append(float(j))
            row.append(tuple(column))
           
        wrapper.IsZeroIndex = False
        wrapper.Array = tuple(row)
        return wrapper

def createInstance( ctx ):
    return randomImpl( ctx )

g_ImplementationHelper = unohelper.ImplementationHelper()
g_ImplementationHelper.addImplementation( \
    createInstance,"com.python.numpy.random.python.randomImpl",
        ("com.sun.star.sheet.AddIn",),)


This should return a 3x3 array independent of the values fed to the function in Calc. However, I still get a value error when I enter the following in-cell formula:
'{=TRIANGULAR(1,2,3,4)}' -> '#VALUE!'

If I take the python function and use it directly in a python session I get the following output (which appears correct to me):
Code:

>>> import uno
>>> def triangular( self, left, mode, right, size=None ):
...     wrapper = uno.createUnoStruct('com.sun.star.script.ArrayWrapper')
...     row = []
...     for i in range(3):
...         column = []
...         for j in range(3):
...             column.append(float(j))
...         row.append(tuple(column))
...         
...     wrapper.IsZeroIndex = False
...     wrapper.Array = tuple(row)
...     return wrapper
...
>>> a = triangular(1,2,3,4)
>>> a.Array
((0.0, 1.0, 2.0), (0.0, 1.0, 2.0), (0.0, 1.0, 2.0))
>>> a
(com.sun.star.script.ArrayWrapper){ IsZeroIndex = (boolean)false, Array = (any){ ([]any){ (any){ ([]any){ (any){ (double)0 }, (any){ (double)1 }, (any){ (double)2 } } }, (any){ ([]any){ (any){ (double)0 }, (any){ (double)1 }, (any){ (double)2 } } }, (any){ ([]any){ (any){ (double)0 }, (any){ (double)1 }, (any){ (double)2 } } } } } }


So I assume this is the structure that is being returned by my python function to Calc, when it is being called as an array function, from within a cell. But as I mentioned I am still getting a #VALUE! error. Can you see an error in what I have done? Or do you have any further insight into my problem?

Kind Regards,

Kurt
Back to top
View user's profile Send private message
Villeroy
Super User
Super User


Joined: 04 Oct 2004
Posts: 10106
Location: Germany

PostPosted: Wed Jun 22, 2011 6:30 am    Post subject: Reply with quote

No, sorry. I gave up OOo coding. It is a pointless waste of time. Today I noticed that my Basic wrappers for Python functions stopped working so I can not even try your function without building a complete add-in.
_________________
Rest in peace, oooforum.org
Get help on https://forum.openoffice.org
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API All times are GMT - 8 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group