473,403 Members | 2,270 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,403 software developers and data experts.

Python code and C code in the same module?

I'd like to merge a module written in C with a module written in Python
so that the user can access objects from either module by importing the
single, merged module.

Specifically, I'm looking at the collections module, which is written in
C, and my Bunch object, which is written in Python. I'd like the Bunch
object to appear in the collections module so that you can do:

from collections import Bunch

without, of course, removing the ability to do:

from collections import deque

There's no need for the code to reside in the same file or anything of
course -- I just want the different objects to appear in the same module.

Thanks,

Steve
Jul 18 '05 #1
2 1213
Steven Bethard wrote:
I'd like to merge a module written in C with a module written in Python so that the user can
access objects from either module by importing the single, merged module.

Specifically, I'm looking at the collections module, which is written in C, and my Bunch object,
which is written in Python. I'd like the Bunch object to appear in the collections module so that
you can do:

from collections import Bunch

without, of course, removing the ability to do:

from collections import deque

There's no need for the code to reside in the same file or anything of course -- I just want the
different objects to appear in the same module.


the usual way to do this is to expose the C objects via the Python
module:

# foo.py
from _foo import flubb, glubb, dubb

# main.py
import foo

foo.flubb(flabb)

if you insist on doing it the other way around, you can emulate from-import
on the C level. see e.g. the "call" function in Modules/_sre.c for an example
(but instead of calling the "func", add it to the local module; see the "init_sre"
function in the same module for code that adds stuff to the module dict).

</F>

Jul 18 '05 #2
Fredrik Lundh wrote:
if you insist on doing it the other way around,


This paragraph should actually continue ". . .you clearly have too much free
time" };>

Cheers,
Nick.
Jul 18 '05 #3

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

Similar topics

8
by: Bo Peng | last post by:
Dear list, I am writing a Python extension module that needs a way to expose pieces of a big C array to python. Currently, I am using NumPy like the following: PyObject* res =...
0
by: Kurt B. Kaiser | last post by:
Patch / Bug Summary ___________________ Patches : 378 open ( +3) / 3298 closed (+34) / 3676 total (+37) Bugs : 886 open (-24) / 5926 closed (+75) / 6812 total (+51) RFE : 224 open...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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,...
0
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...
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,...

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.