Return class instance from COM method in Python

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Paul

    Return class instance from COM method in Python

    Hi

    I'm implementing a few COM classes using Python. I've come across a
    problem when I'm trying the return an instance of a class from a
    method on one of my classes. The code is as follows, with two classes
    - clsDividend and clsDividendEsim ate. The
    clsDividend.Get DividendEstimat es method tries to return a new instance
    of the clsDividendEsti mate class, but throws an error.

    Any ideas what I'm doing wrong - is it possible to do what I'm trying
    to achieve?

    The client will ultimately be Excel, but I can't even get it to work
    within a Python client yet. Ultimately I'd like to do this in my
    client(pseudo):

    oDiv = CreateObject("S BLFront.clsDivi dend")
    oDivEst = oDiv.GetDividen dEstimates("tes tval")
    msgbox oDivEst.ccy

    Many thanks
    Paul

    # Expose the Python SBLFront.
    class clsDividend(cls SBLFront):
    """The SBLFront object exposed via COM
    """
    #_reg_clsctx_ = pythoncom.CLSCT X_LOCAL_SERVER
    _public_methods _ = ['GetDividendEst imates']
    _public_methods _.extend(clsSBL Front._public_m ethods_)

    # All registration stuff to support fully automatic
    register/unregister
    _reg_verprogid_ = "SBLFront.clsDi vidend.1"
    _reg_progid_ = "SBLFront.clsDi vidend"
    _reg_desc_ = "Python SBLFront"
    _reg_clsid_ = "{2E851A1B-A8D1-4AD5-BA16-EB6B9BCF2F21}"
    _reg_class_spec _ = "win32com.serve rs.sblfront.cls Dividend"

    # ------------------------------------------------------------------------

    def __init__(self):
    clsSBLFront.__i nit__(self)
    self.dict = {}

    def GetDividendEsti mates(self, InsId):
    oDivEst = clsDividendEsti mate('USD', 1.0, 1.0,
    '2003-01-01','2003-01-01','2003-01-01','Test description')
    return oDivEst

    # ----------------------------------------------------------------------------

    class clsDividendEsti mate:
    """The SBLFront object exposed via COM
    """
    _public_methods _ = []
    _public_attrs_ = ['ccy', 'dividend', 'tax_factor', 'pay_day',
    'ex_div_day', 'day', 'description']
    _readonly_attrs _ = ['ccy', 'dividend', 'tax_factor', 'pay_day',
    'ex_div_day', 'day', 'description']

    # All registration stuff to support fully automatic
    register/unregister
    _reg_verprogid_ = "SBLFront.clsDi videndEstimate. 1"
    _reg_progid_ = "SBLFront.clsDi videndEstimate"
    _reg_desc_ = "Python SBLFront"
    _reg_clsid_ = "{055DEFBC-B095-4C5B-A9CE-BFBB965BBDC1}"
    _reg_class_spec _ = "win32com.serve rs.sblfront.cls DividendEstimat e"

    # ------------------------------------------------------------------------

    def __init__(self):
    self.dict = {}
    self.ccy = ''
    self.dividend = 0.0
    self.tax_factor = 0.0
    self.pay_day = ''
    self.ex_div_day = ''
    self.day = ''
    self.descriptio n = ''

    def __init__(self, sCCY, dDiv, dTax, dtDay, dtPayDay, dtExDivDay,
    sDesc):
    self.dict = {}
    self.ccy = sCCY
    self.dividend = dDiv
    self.tax_factor = dTax
    self.pay_day = dtPayDay
    self.ex_div_day = dtExDivDay
    self.day = dtDay
    self.descriptio n = sDesc
  • Paul

    #2
    Re: Return class instance from COM method in Python

    I managed to solve my own problem - it didn't like having two __init__
    methods (different prototypes).

    However, I have another problem. I've worked out how to return a
    list, and also how to return an instance of a class. I know need to
    be able to return a list of objects. Is this possible (no luck so
    far)?

    Paul

    paul.kemp@stand ardbank.com (Paul) wrote in message news:<bb8e48c9. 0310200804.7835 0aef@posting.go ogle.com>...[color=blue]
    > Hi
    >
    > I'm implementing a few COM classes using Python. I've come across a
    > problem when I'm trying the return an instance of a class from a
    > method on one of my classes. The code is as follows, with two classes
    > - clsDividend and clsDividendEsim ate. The
    > clsDividend.Get DividendEstimat es method tries to return a new instance
    > of the clsDividendEsti mate class, but throws an error.
    >
    > Any ideas what I'm doing wrong - is it possible to do what I'm trying
    > to achieve?
    >
    > The client will ultimately be Excel, but I can't even get it to work
    > within a Python client yet. Ultimately I'd like to do this in my
    > client(pseudo):
    >
    > oDiv = CreateObject("S BLFront.clsDivi dend")
    > oDivEst = oDiv.GetDividen dEstimates("tes tval")
    > msgbox oDivEst.ccy
    >
    > Many thanks
    > Paul
    >
    > # Expose the Python SBLFront.
    > class clsDividend(cls SBLFront):
    > """The SBLFront object exposed via COM
    > """
    > #_reg_clsctx_ = pythoncom.CLSCT X_LOCAL_SERVER
    > _public_methods _ = ['GetDividendEst imates']
    > _public_methods _.extend(clsSBL Front._public_m ethods_)
    >
    > # All registration stuff to support fully automatic
    > register/unregister
    > _reg_verprogid_ = "SBLFront.clsDi vidend.1"
    > _reg_progid_ = "SBLFront.clsDi vidend"
    > _reg_desc_ = "Python SBLFront"
    > _reg_clsid_ = "{2E851A1B-A8D1-4AD5-BA16-EB6B9BCF2F21}"
    > _reg_class_spec _ = "win32com.serve rs.sblfront.cls Dividend"
    >
    > # ------------------------------------------------------------------------
    >
    > def __init__(self):
    > clsSBLFront.__i nit__(self)
    > self.dict = {}
    >
    > def GetDividendEsti mates(self, InsId):
    > oDivEst = clsDividendEsti mate('USD', 1.0, 1.0,
    > '2003-01-01','2003-01-01','2003-01-01','Test description')
    > return oDivEst
    >
    > # ----------------------------------------------------------------------------
    >
    > class clsDividendEsti mate:
    > """The SBLFront object exposed via COM
    > """
    > _public_methods _ = []
    > _public_attrs_ = ['ccy', 'dividend', 'tax_factor', 'pay_day',
    > 'ex_div_day', 'day', 'description']
    > _readonly_attrs _ = ['ccy', 'dividend', 'tax_factor', 'pay_day',
    > 'ex_div_day', 'day', 'description']
    >
    > # All registration stuff to support fully automatic
    > register/unregister
    > _reg_verprogid_ = "SBLFront.clsDi videndEstimate. 1"
    > _reg_progid_ = "SBLFront.clsDi videndEstimate"
    > _reg_desc_ = "Python SBLFront"
    > _reg_clsid_ = "{055DEFBC-B095-4C5B-A9CE-BFBB965BBDC1}"
    > _reg_class_spec _ = "win32com.serve rs.sblfront.cls DividendEstimat e"
    >
    > # ------------------------------------------------------------------------
    >
    > def __init__(self):
    > self.dict = {}
    > self.ccy = ''
    > self.dividend = 0.0
    > self.tax_factor = 0.0
    > self.pay_day = ''
    > self.ex_div_day = ''
    > self.day = ''
    > self.descriptio n = ''
    >
    > def __init__(self, sCCY, dDiv, dTax, dtDay, dtPayDay, dtExDivDay,
    > sDesc):
    > self.dict = {}
    > self.ccy = sCCY
    > self.dividend = dDiv
    > self.tax_factor = dTax
    > self.pay_day = dtPayDay
    > self.ex_div_day = dtExDivDay
    > self.day = dtDay
    > self.descriptio n = sDesc[/color]

    Comment

    Working...