473,320 Members | 2,088 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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 1669
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: fctk | last post by:
hello, i'm trying to compile this small program: int main(void) { unsigned long int max; max = 4000000000;
3
by: johnmmcparland | last post by:
Hi all, I would like to have a static constant array inside a class definition which would contain the number of days in each month (I am writing a Date class as an exercise). However my...
9
by: Justin Voelker | last post by:
I have a configuration file that contains the host, username, password, and database name required for any database connections. The config file runs through a few if/then/else statements to...
9
by: Jess | last post by:
Hello, I was told that if I declare a static class constant like this: class A{ static const int x = 10; }; then the above statement is a declaration rather than a definition. As I've...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.