accessing python objects from C++

Robin Munn rmunn at pobox.com
Wed Dec 18 16:16:51 EST 2002


I see you're using Outlook Express but have managed to turn off HTML
posting -- thank you for that. But do you have any idea why OE is
doubling up your carriage returns? Every time you press Return, it seems
to be putting two carriage return characters in. It makes your code a
bit more difficult to read than necessary -- look what it did to your
ValDate class, for instance:

Stevek <skille at ameritech.net> wrote:
> 
> class ValDate:
> 
>     julDate = 0
> 
> 
> 
>     year    = 2002
> 
>     month   = 9
> 
>     day     = 30
> 
> 
> 
>     def __init__(self, valDate=None):

And so on. You get the idea.

Anyway, on to answer your question:

> What I can't seem to figure out however is how I can populate the global
> dictionary with a custom date object from within the C++ application.  The
> C++ application is cycling through all the historical data day by day and
> populating the dictionary with stock prices.
> 
> The stock prices can be placed in the dictionary with code similar to the
> following:
> 
> PyDict_SetItemString( m_pLocalDict, "LastPrice",
> PyFloat_FromDouble(value) );
> 
> Since Python provides the PyFloat_FromDouble() conversion routine, this is
> pretty easy.  However, populating the dictionary with a date object doesn't
> seem to be as straight foward.  I have created my own Python date class, and
> would like to update it's value with a date that corrosponds to the price
> data that was also placed in the dictionary.
> 
> I want to use my own date class because I'm performing some special
> calculations.  How do I access and alter Python class variables from within
> the C++ application?  I have a custom date class such as:

Have you looked at the PyInstance_New functions? I think that's what
you'll want to use. See:

    http://www.python.org/doc/current/api/instanceObjects.html

for more details. You'll need a PyObject * pointer to the class, of
course. Once you've created an instance of your class, you can interact
with its attributes with the functions described in:

    http://www.python.org/doc/current/api/object.html

I hope this gets you pointed in the right direction.

-- 
Robin Munn <rmunn at pobox.com>
http://www.rmunn.com/
PGP key ID: 0x6AFB6838    50FF 2478 CFFB 081A 8338  54F7 845D ACFD 6AFB 6838



More information about the Python-list mailing list