469,649 Members | 1,574 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,649 developers. It's quick & easy.

Class constant for extension

Hi,

I have written a small prototype Python extension for a C-library.

I have the methods all sorted out and it is working fine.

In the C-library, they are various constants of types like string,
integer, float and matrix. I'd like to expose them as READONLY values.

Is the use of PyMemberDefs a suitable way to provide such access?

I'd like the user of the extension to be able to do something like
the follow

import MyExtension

MyExtension.begin()
mv = MyExtension.minValue
MyExtension.process(MyExtension.versionStr)
MyExtension.end()

Is someone able to point me to an example code of how I can write my
extension so that the references to those constants can be used in the
above way.

Regards

Dec 19 '06 #1
1 1568
Yu**********@gmail.com wrote:
Hi,

I have written a small prototype Python extension for a C-library.

I have the methods all sorted out and it is working fine.

In the C-library, they are various constants of types like string,
integer, float and matrix. I'd like to expose them as READONLY values.

Is the use of PyMemberDefs a suitable way to provide such access?

I'd like the user of the extension to be able to do something like
the follow

import MyExtension

MyExtension.begin()
mv = MyExtension.minValue
MyExtension.process(MyExtension.versionStr)
MyExtension.end()

Is someone able to point me to an example code of how I can write my
extension so that the references to those constants can be used in the
above way.

Regards
A couple of years ago I wanted to do something similar for a project I
was working on.
Below is a snippet of some sample C code from the module's
initialization function which should give you an idea of the approach
used. It's a limited example and only creates module identifiers for
simple integer values, not the more complex types you are interested
in, nor does it do anything to make them read only, but it might help
get you started.

If you find something that addresses these deficiencies, please post
your findings.

Best,
-Martin

// snippet
#include "Python.h"

enum METAtags
{
kMETA_MojikumiX4051 = 0,
kMETA_UNIUnifiedBaseChars = 1,
kMETA_BaseFontName = 2
}

void initPyExtension()
{
PyObject *m, *d, *o;
m = Py_InitModule4("PyExtension", pyis_methods,
PySING_module_documentation,
(PyObject*)NULL, PYTHON_API_VERSION);

// Add some symbolic constants to the module
d = PyModule_GetDict(m); // get modules dictionary

PyObject* o = PyInt_FromLong(kMETA_MojikumiX4051);
PyDict_SetItemString(d, "idMojikumiX4051", o);
Py_INCREF(o); // for dictionary ref

PyObject* o = PyInt_FromLong(kMETA_UNIUnifiedBaseChars);
PyDict_SetItemString(d, "idUNIUnifiedBaseChars", obj);
Py_INCREF(o);

PyObject* o = PyInt_FromLong(kMETA_BaseFontName);
PyDict_SetItemString(d, "idBaseFontName", obj);
Py_INCREF(o);
Dec 24 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.

By using this site, you agree to our Privacy Policy and Terms of Use.