473,769 Members | 4,173 Online
Bytes | Software Development & Data Engineering Community
+ 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_tim e() '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 2098

"It's me" <it***@yahoo.co m> wrote in message
news:oD******** ***********@new ssvr13.news.pro digy.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.co m> wrote in message
news:oD******** ***********@new ssvr13.news.pro digy.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.co m>
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.co m> wrote in message
news:41******** ******@kdart.co m...
It's me wrote:
"It's me" <it***@yahoo.co m> wrote in message
news:oD******** ***********@new ssvr13.news.pro digy.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.co m>
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
2015
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 Python windows FAQ I shouldn't have to make a seperate dll, see link below). I create a MyModule.i file for swig which contains a simple test c++ class. I run swig on it with the following options: swig -python -c++ -includeall -shadow MyModule.i,...
0
1638
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 good ideas. i have also sent i to the swig users list a couple of times but have not got any response so i am casting my net a little wider... i have been struggling with the following problem for a couple of days now. +specifically, i have a...
1
5343
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 tried several of the *.i libraries at http://www.swig.org/Doc1.3/Library.html. Most notably the "std_string.i" And get the following: >>> w.SetDestFilename("test.bmp") Traceback (most recent call last):
2
4453
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 */ #include <time.h>
0
1603
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 reference compiler errors: ...\build\temp.win32-2.5\Release\example_wrap.o:example_wrap.c:(.text+0x670f): undefined reference to `_imp__PyExc_MemoryError' there are many other similar errors all prefaced with _imp__Py, so I am assuming there is a...
1
5343
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 abstract class are abstract too and won't create the correct constructor. Abstract class: class CORE_API Shape
3
2121
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 as function parameters to return values. I have read SWIG manual to find best way to overcome that, but there are many warnings about memory leaks and stuff, so I feel confused. Ok to put it more simply: how to safely define a variable in...
1
1707
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 class (we'll call it "Peter") with SWIG so some of its base functionality is available in Python. For the sake of this example, Peter has a method called get_type(), that takes no arguments and returns an integer. This all works, and within my...
0
1341
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 swig.org: http://www.swig.org/tutorial.html I have written example.c and example.i as described in the above tutorial. My first attempt at compiling the libraries was this:
0
10045
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9994
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7409
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6673
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5299
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3959
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 we have to send another system
2
3562
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.