473,395 Members | 1,516 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,395 software developers and data experts.

Mutual module imports

How does one normally make a Python extension module that has some parts
in Python and some functions in C share globals between the Python and C
functions? Will that approach work with Pyrex?

I have written a Python module that uses some C functions. I wrote the
module in two parts, one Python, one Pyrex (C). They need to share some
globals. (I use pyrex to handle ref counting. I think I'm glad I did.)

At first they just sort of mutually imported each other, and it worked
until I put tests in the Python one and set it up to run them when it is
named "__main__". What happened reminded me that there are also other
ways modules can be imported under different names, so I tried a
different approach.

Now the Python module imports the Pyrex module and just shoves
references to its globals into the Pyrex module (the Pyrex module
defines them as None). The Pyrex module doesn't import the Python
module anymore. This also works, even when the Python module has a
different name (e.g. "__main__"). I just feel dirty about it.
__________________________________________________ ______________________
TonyN.:' *firstname*nlsnews@georgea*lastname*.com
' <http://www.georgeanelson.com/>
Oct 18 '05 #1
2 1773
The trick is via something called Extending. Pyrex just makes extending
a bit easier, but, depending on the complexity of the function, it
might be easier just to extend it yourself. Basically you make a
function that translates python into C. There are some instructions on
the Python website, http://www.python.org . Look around, and you'll
find lots of cool stuff on it.

Oct 18 '05 #2
"Tony Nelson" <*firstname*nlsnews@georgea*lastname*.com> wrote:
[snipped]

I have written a Python module that uses some C functions. I wrote the
module in two parts, one Python, one Pyrex (C). They need to share some
globals.
[snipped]

Now the Python module imports the Pyrex module and just shoves
references to its globals into the Pyrex module (the Pyrex module
defines them as None). The Pyrex module doesn't import the Python
module anymore. This also works, even when the Python module has a
different name (e.g. "__main__"). I just feel dirty about it.


Well, I feel dirty every time I have to share globals <wink>.

George
Oct 18 '05 #3

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

Similar topics

9
by: Paul Rubin | last post by:
That's what the Python style guides advise. They don't seem to like def frob(x): import re if re.search('sdfxyz', x): ... instead preferring that you pollute your module's global namespace...
8
by: Grant D. Watson | last post by:
If this has been answered before, or if my terminology is off, please bear with me; my Python experience is limited to use in one class and to personal projects. I'd like to do something rather...
10
by: Ron | last post by:
Hello, The following code works in a Form class module but not a standard module: Dim d1 As DateTime Format(d1, "MMMM") In a standard module I get a squigly line saying that an argument has...
2
by: Matthias Kramm | last post by:
Hi All, I'm having a little bit of trouble using the "imp" module to dynamically import modules. It seems that somehow cyclic references of modules don't work. I'm unable to get the following...
2
by: jimmyfishbean | last post by:
Hi, I have created a VB.Net windows service. Everything was working fine until I attempted to add a setup project. Now, for some strange, when I try and run the application (using INSTALLUTIL...
6
by: Ritesh Raj Sarraf | last post by:
Hi, I've been very confused about why this doesn't work. I mean I don't see any reason why this has been made not to work. class Log: def __init__(self, verbose, lock = None): if verbose...
1
by: Thomas Wittek | last post by:
Hi! Is there any possibility/tool to automatically organize the imports at the beginning of a module? I don't mean automatic imports like autoimp does as I like seeing where my...
3
by: Jugdish | last post by:
Why doesn't the following work? $HOME/pkg/__init__.py $HOME/pkg/subpkg/__init__.py $HOME/pkg/subpkg/a.py $HOME/pkg/subpkg/b.py # empty import a
13
by: Rafe | last post by:
Hi, I am in a situation where I feel I am being forced to abandon a clean module structure in favor of a large single module. If anyone can save my sanity here I would be forever grateful. My...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
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,...
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
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...

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.