473,473 Members | 1,906 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

swig & Python question

I am playing around with SWING building a Python module using the no brainer
example in http://www.swig.org/tutorial.html. With that first example,
/* File : example.c */

#include <time.h>
double My_variable = 3.0;

int fact(int n) {
if (n <= 1) return 1;
else return n*fact(n-1);
}

int my_mod(int x, int y) {
return (x%y);
}

char *get_time()
{
time_t ltime;
time(&ltime);
return ctime(&ltime);
}
and using the swig file of:
/* example.i */
%module example
%{
/* Put header files here (optional) */
%}

extern double My_variable;
extern int fact(int n);
extern int my_mod(int x, int y);
extern char *get_time();
I was able to execute this Python script:

import example
example.fact(5) 120 example.my_mod(7,3) 1 example.get_time() 'Sun Feb 11 23:01:07 1996'

However, when I try to access the gloabl variable My_variable by doing:

print example.cvar

I get a blank (rather then a value of 3.0).

Why?

--
It's me
Jul 18 '05 #1
3 2076

"It's me" <it***@yahoo.com> wrote in message
news:oD*******************@newssvr13.news.prodigy. com...
I am playing around with SWING building a Python module using the no brainer example in http://www.swig.org/tutorial.html. With that first example,


Oops! Soapy fingers. "SWIG" - not "SWING".

--
It's me.
Jul 18 '05 #2
It's me wrote:
"It's me" <it***@yahoo.com> wrote in message
news:oD*******************@newssvr13.news.prodigy. com...
I am playing around with SWING building a Python module using the no


brainer
example in http://www.swig.org/tutorial.html. With that first example,

Oops! Soapy fingers. "SWIG" - not "SWING".

--
It's me.


I have used SWIG before, and it's not always such a "no-brainer". In
fact, it rarely is except for trivial examples. But it can work. I think
it is best suited for wrapping large libraries. For small stuff, it
would be better to just do it "manually" using the Python C API.
Good luck.

--
It's not me.


--
\/ \/
(O O)
-- --------------------oOOo~(_)~oOOo----------------------------------------
Keith Dart <kd***@kdart.com>
public key: ID: F3D288E4
================================================== ==========================
Jul 18 '05 #3
Whether SWIG will work in a "no brainer" way or not depends on the original
code, I think. If the original code uses a very convoluted design, of
course things will get hairy. If the package uses a very clean structure,
I think you will find SWIG works out very nicely.

The intriguing things is, however, once you structure the package to a form
SWIG would work, it opens up the door to support multiple script languages
(and they have a long list of supported script languages).

If you hand crafted it to run the Python-C API, then you can only use Python
as script.

--
It's me
"Keith Dart" <kd***@kdart.com> wrote in message
news:41**************@kdart.com...
It's me wrote:
"It's me" <it***@yahoo.com> wrote in message
news:oD*******************@newssvr13.news.prodigy. com...
I am playing around with SWING building a Python module using the no
brainer
example in http://www.swig.org/tutorial.html. With that first example,

Oops! Soapy fingers. "SWIG" - not "SWING".

--
It's me.


I have used SWIG before, and it's not always such a "no-brainer". In
fact, it rarely is except for trivial examples. But it can work. I think
it is best suited for wrapping large libraries. For small stuff, it
would be better to just do it "manually" using the Python C API.
Good luck.

--
It's not me.


--
\/ \/
(O O)
-- --------------------oOOo~(_)~oOOo--------------------------------------

-- Keith Dart <kd***@kdart.com>
public key: ID: F3D288E4

================================================== ==========================
Jul 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Jon Moldover | last post by:
Hi, I'm using Python in my win32 app by linking to the python23.dll. I'm trying to expose some c++ code in my app to Python so I can make application calls from Python scripts (according to the...
0
by: Andrew Collier | last post by:
hello, i am not sure whether this problem relates to swig, python or my c++ compiler. +please forgive me if this is inappropriately posted but i am sure that someone +on this list must have some...
1
by: Leonard J. Reder | last post by:
Hello, I have been trying to get this simple call to work with a SWIG interface. The problem is I do not know how to pass a const char * or a std::string & in python to SWIG wrapped code. I...
2
by: ajikoe | last post by:
Hi, I tried to follow the example in swig homepage. I found error which I don't understand. I use bcc32, I already include directory where my python.h exist in bcc32.cfg. /* File : example.c...
0
by: newbie73 | last post by:
Going through the tutorial on http://swig.org, I created the example files (pasted below). After generating the _wrap file, I tried compiling (using mingw32) and received a lot of undefined...
1
by: Stodge | last post by:
Yet another SWIG question (YASQ!). I'm having a problem with using an abstract base class. When generating the Python bindings, SWIG thinks that all the concrete classes that derive from this...
3
by: code_berzerker | last post by:
Hi i'm relatively new to Python and my C/C++ knowledge is near to None. Having said that I feel justified to ask stupid questions :) Ok now more seriously. I have question refering to char* used...
1
by: Uberman | last post by:
I have a bit of a odd arrangement here with SWIG, Python, Embedded Python and C++ classes exported into Python. Here's the plot: I have a class defined in a C++ DLL library. I am wrapping this...
0
by: Basha J P M | last post by:
I am beginner in python. I am working through the tutorial examples from http://www.swig.org/ and have run into some problems. I took the following command instructions from the tutorial on...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.