Author Topic: pyxll and classes  (Read 116 times)

apertosa

  • Newbie
  • *
  • Posts: 7
  • Karma: +0/-0
    • View Profile
pyxll and classes
« on: September 22, 2016, 04:44:32 PM »
Hello!
I just started using pyxll and so far this is really impressive. I work for an engineering software firm and being able to leverage the huge customer base that has Excel installed is something that we are keenly interested in.

The Python code that I want to be able to integrate in Excel is (generally speaking) heavily OO. There are no functions, but a whole bunch of classes and their methods.

This means that our Python stuff doesn't necessarily expose functions to create various objects. But it does instead make the classes available. Example, suppose I have a Point class with a bunch of overloaded operators, derived off the list object.

Code: [Select]
class Point (list):
  """x, y, z location with overloaded math operations"""
  #-----------------------------------------------------------------------------
  def __init__ (self, x=None, y=None, z=None):
    list.__init__ (self)
    if x == None: x = y = z = 0
    if y == None: x, y, z = x
    self[:] = (x, y, z)

etc....

So when I create a Point instance all I have to do is this:
p = Point (100, 10, 100)

Since there are no functions to 'create' an instance, there is nothing for me to expose to the Pyxll layer with the @ decorator. So I'm trying to figure out what to do. It seems that I have to supply functions to create each and every object, decorate them and have them return the object itself. So for Point:

Code: [Select]
@xl_func
def createPoint(**kwds):
  point = Point(**kwds)
  return point

Is this a viable way to proceed?

Tony

  • Administrator
  • Hero Member
  • *****
  • Posts: 541
  • Karma: +12/-0
    • View Profile
Re: pyxll and classes
« Reply #1 on: September 23, 2016, 01:37:55 PM »
Hi,

yes, that approach seems perfectly reasonable to me :)

If you have to pass objects around a lot you might find the object cache example (included in the download) interesting.

Best regards,
Tony