473,842 Members | 1,603 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Python Binding

Hello everybody.

There's a C library which i'd like to have python bindings for. I havn't
known anything before about how to write python bindings for a C library.

I succeeded now by using distutils to write the first bindings for functions
and similar.

Now, it seems as something is blocking my brain. For the library, i
need "custom" types, so types defined in this library (structures),
including pointers and similar.

I've been thinking about what i will need to represent this lists in python.
I thought about creating an external python object, providing "informatio n"
i get from the list in C structures which can be converted.

Basically, it are list of packages, which have several attributes (next,
prev, etc). But i don't know how to supply a proper list from the binding /
object written in C.

Any suggestions or hints about this?

Thank you,
Georg
May 5 '07 #1
7 1869
Georg Grabler wrote:
There's a C library which i'd like to have python bindings for. I havn't
known anything before about how to write python bindings for a C library.

I succeeded now by using distutils to write the first bindings for functions
and similar.

Now, it seems as something is blocking my brain. For the library, i
need "custom" types, so types defined in this library (structures),
including pointers and similar.

I've been thinking about what i will need to represent this lists in python.
I thought about creating an external python object, providing "informatio n"
i get from the list in C structures which can be converted.

Basically, it are list of packages, which have several attributes (next,
prev, etc). But i don't know how to supply a proper list from the binding /
object written in C.

Any suggestions or hints about this?
Use Pyrex. It's a Python-like language that was designed to write extension
modules in C and it's great for writing wrappers.

http://www.cosc.canterbury.ac.nz/gre.../python/Pyrex/

An example wrapper is described here:

http://ldots.org/pyrex-guide/

Hope it helps,
Stefan
May 6 '07 #2
Stefan Behnel schrieb:
Georg Grabler wrote:
>There's a C library which i'd like to have python bindings for. I havn't
known anything before about how to write python bindings for a C library.

I succeeded now by using distutils to write the first bindings for functions
and similar.

Now, it seems as something is blocking my brain. For the library, i
need "custom" types, so types defined in this library (structures),
including pointers and similar.

I've been thinking about what i will need to represent this lists in python.
I thought about creating an external python object, providing "informatio n"
i get from the list in C structures which can be converted.

Basically, it are list of packages, which have several attributes (next,
prev, etc). But i don't know how to supply a proper list from the binding /
object written in C.

Any suggestions or hints about this?

Use Pyrex. It's a Python-like language that was designed to write extension
modules in C and it's great for writing wrappers.

http://www.cosc.canterbury.ac.nz/gre.../python/Pyrex/

An example wrapper is described here:

http://ldots.org/pyrex-guide/

Hope it helps,
Stefan
Hi,
use SWIG.
It is a no-brainer for simple libs. wxPython relies heavily on it.
I worked with some times and it is really straightforward .
See this: http://www.swig.org/Doc1.1/HTML/Python.html
Cheers,
Stefan
May 6 '07 #3
Stefan Behnel schrieb:
Georg Grabler wrote:
>There's a C library which i'd like to have python bindings for. I havn't
known anything before about how to write python bindings for a C library.

I succeeded now by using distutils to write the first bindings for functions
and similar.

Now, it seems as something is blocking my brain. For the library, i
need "custom" types, so types defined in this library (structures),
including pointers and similar.

I've been thinking about what i will need to represent this lists in python.
I thought about creating an external python object, providing "informatio n"
i get from the list in C structures which can be converted.

Basically, it are list of packages, which have several attributes (next,
prev, etc). But i don't know how to supply a proper list from the binding /
object written in C.

Any suggestions or hints about this?

Use Pyrex. It's a Python-like language that was designed to write extension
modules in C and it's great for writing wrappers.

http://www.cosc.canterbury.ac.nz/gre.../python/Pyrex/

An example wrapper is described here:

http://ldots.org/pyrex-guide/

Hope it helps,
Stefan
Hi,
use SWIG.
It is a no-brainer for simple libs. wxPython relies heavily on it.
I worked with some times and it is really straightforward .
See this: http://www.swig.org/Doc1.1/HTML/Python.html
Cheers,
Stefan
May 6 '07 #4
Georg Grabler <gg******@gmail .comwrote:
There's a C library which i'd like to have python bindings for. I havn't
known anything before about how to write python bindings for a C library.

I succeeded now by using distutils to write the first bindings for functions
and similar.

Now, it seems as something is blocking my brain. For the library, i
need "custom" types, so types defined in this library (structures),
including pointers and similar.

I've been thinking about what i will need to represent this lists in python.
I thought about creating an external python object, providing "informatio n"
i get from the list in C structures which can be converted.

Basically, it are list of packages, which have several attributes (next,
prev, etc). But i don't know how to supply a proper list from the binding /
object written in C.

Any suggestions or hints about this?
Sounds like a job for ctypes which is bundled with py 2.5.

http://docs.python.org/lib/module-ctypes.html

It is great for access C libraries (assuming you have a shared library
(.so or .dll).

You'll end up writing python code rather than C code which you'll
enjoy!

--
Nick Craig-Wood <ni**@craig-wood.com-- http://www.craig-wood.com/nick
May 6 '07 #5
Hello,

I've basically had plans on just extending python for the wrapper.
That basically works fine, i just don't get along by the types
provided (http://docs.python.org/ext/ext.html).

Anyway, i want to extend python for types. The problem i currently
experience is the following.

Guess what - i have a base class, doing some wrapping. Currently,
called pybtest (just testing around though).
Now, i'd need a configuration object, accessable in python. By using
the library, it should be possible to access it using
pybtest.config. insertValue('xy z') as an example, but i havn't found
any way to archive this yet.

If i write config as an own (external) type, i can't get it into the
pybtest object for some reason. It's compiled as an extra "config.so" ,
and with the test script i need to do import config. Anyway, it's not
in the pybtest object., i've to build up a config and pass it by to
the pybtest binding, so it can set it's internal configuration
structures.

If i try to compile it within the pybtest, i can't use ext_modules,
and as expected, i can't do another pybtestinit function, so it's
quite useless, at least with the knowledge i could get out of the
documentation.

By just making a include file of the type, including the type
definition adding up some functions to pybtest, i actually can access
it, without the wanted functionality of pybtest.config.

Why do you all suggest other things than the way suggested by python?
I havn't got a real problem writing the code in C, actually, it looked
as if it would give me several possibilities i wouldn't have with
pyrex (like binding more library functions to one provided python
function and so on). I havn't had a closer look to SWIG yet.

It looks as if i could just provide single objects or modules, and no
additional object into a module.

Does anyone have closer knowledge on this?
Btw: Sorry for the long delay time, i had to describe the things
closer, and had to take a deeper look into the bindings than i had
before.

Kind regards,
Georg

May 8 '07 #6
STiAT wrote:
Why do you all suggest other things than the way suggested by python?
Because going to Paris is not the only way to get french bread?

Why would you want to write all that ugly glue code by hand that Pyrex
generates for free? Module descriptors? Class descriptors? Method descriptors?
Reference counting? That's what Pyrex saves you from. Honestly.

From what I read in your mail, that's exactly the kind of thing you're having
trouble with. Wouldn't you prefer concentrating on your real code instead?

I havn't got a real problem writing the code in C, actually, it looked
as if it would give me several possibilities i wouldn't have with
pyrex (like binding more library functions to one provided python
function and so on).
No idea what you mean in your parentheses, but I don't think there are many
"possibilit ies" you "wouldn't have with Pyrex".

We used Pyrex to write lxml, a wrapper around the huge API of libxml2 and
libxslt. It's some 11000 lines of Pyrex code by now, but the generated C code
is some 67000 lines in total. Even if it's somewhat verbose and generic in
places, I wouldn't have wanted to write that by hand.

Stefan
May 8 '07 #7
You are completely right wihtin this. It's some time i didn't reply, but
i've taken a look on pyrex and swig now which did cost me some time, and
they really make it easier. SWIG provides more possibilities and bindings
for other languages too, though - i must say i've been faster with pyrex,
for some reason it fits me better.

I'd like to use swig, but for some reason i've troubles defining a
completely new type, so a type which is not a wrapper type, but a type
provided to python.

Kind regards,
Georg

Stefan Behnel wrote:
STiAT wrote:
>Why do you all suggest other things than the way suggested by python?

Because going to Paris is not the only way to get french bread?

Why would you want to write all that ugly glue code by hand that Pyrex
generates for free? Module descriptors? Class descriptors? Method
descriptors? Reference counting? That's what Pyrex saves you from.
Honestly.

From what I read in your mail, that's exactly the kind of thing you're
having trouble with. Wouldn't you prefer concentrating on your real code
instead?

>I havn't got a real problem writing the code in C, actually, it looked
as if it would give me several possibilities i wouldn't have with
pyrex (like binding more library functions to one provided python
function and so on).

No idea what you mean in your parentheses, but I don't think there are
many "possibilit ies" you "wouldn't have with Pyrex".

We used Pyrex to write lxml, a wrapper around the huge API of libxml2 and
libxslt. It's some 11000 lines of Pyrex code by now, but the generated C
code is some 67000 lines in total. Even if it's somewhat verbose and
generic in places, I wouldn't have wanted to write that by hand.

Stefan
May 12 '07 #8

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

Similar topics

15
19353
by: Prabu | last post by:
Hi, I'm new to python, so excuse me if i'm asking something dumb. Does python provide a mechanism to implement virtual functions? Can you please give a code snippet also...:) Thanx in advance -Prabu.
30
3532
by: Christian Seberino | last post by:
How does Ruby compare to Python?? How good is DESIGN of Ruby compared to Python? Python's design is godly. I'm wondering if Ruby's is godly too. I've heard it has solid OOP design but then I've also heard there are lots of weird ways to do some things kinda like Perl which is bad for me. Any other ideas?
5
1726
by: paolo veronelli | last post by:
I've a vague idea of the differences,I don't know scheme anyway. I'd like to see an example to show what is missing in python about closures and possibly understand if ruby is better in this sense. Iuse ruby and python in parallel for my job just to learn them and their differences and python is shorter and cleaner ,but i feel it's missing something, in closures.Any hints?
22
5070
by: Glurt Wuntal | last post by:
I am a newbie with Python. It's a great language, but I would like to be able to present a simple gui menu for some of my scripts; something better than using 'raw_input' prompts. Any recommendations for a program that will allow me to create the gui screens? Something useable in Linux. thanks.
0
2548
by: Rafal Zawadzki | last post by:
Hi. I tried earlier to write python zsi mail list, but nobody answered. I am using ZSI 1.7/2.0rc1 with TTPro Soap SDK. The wsdl file can be found here: http://demo.seapine.com/ttsoapcgi.wsdl My wsdl is the same, only different is the address of the cgi file. for example i cannot use defectRequest method:
267
10912
by: Xah Lee | last post by:
Python, Lambda, and Guido van Rossum Xah Lee, 2006-05-05 In this post, i'd like to deconstruct one of Guido's recent blog about lambda in Python. In Guido's blog written in 2006-02-10 at http://www.artima.com/weblogs/viewpost.jsp?thread=147358
21
1952
by: Roy Smith | last post by:
I'm working on a product which for a long time has had a Perl binding for our remote access API. A while ago, I wrote a Python binding on my own, chatted it up a bit internally, and recently had a (large) customer enquire about getting access to it. I asked for permission to distribute the Python binding, and after a few weeks of winding its way through the corporate bureaucracy I got an email from a product manager who wants to meet...
270
8235
by: Jordan | last post by:
Hi everyone, I'm a big Python fan who used to be involved semi regularly in comp.lang.python (lots of lurking, occasional posting) but kind of trailed off a bit. I just wrote a frustration inspired rant on my blog, and I thought it was relevant enough as a wider issue to the Python community to post here for your discussion and consideration. This is not flamebait. I love Python, and I'm not out to antagonise the community. I also...
0
9872
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9715
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10612
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...
0
10314
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9453
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7858
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
5696
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
5885
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3145
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.