[Cython] preferable way to replace a class
Robert Bradshaw
robertwb at math.washington.edu
Tue Apr 7 21:33:57 CEST 2009
On Apr 7, 2009, at 12:29 PM, Brian Blais wrote:
> On Apr 7, 2009, at 15:05 , Lev Givon wrote:
>
>> Received from Brian Blais on Tue, Apr 07, 2009 at 02:48:51PM EDT:
>>> I am writing a tool to do some neural simulation, where I have
>>> several
>>> different neuron groups, which are defined as python classes. Is
>>> there a
>>> preferred way to write a cython-only replacement such that the
>>> end-user
>>> sees no difference? What I mean is that they should be able to do:
>>>
>>> import sim
>>>
>>> n1=sim.NeuronGroup1(5)
>>>
>>> sim.run_sim([n1])
>>>
>>> and if the cython version exists, use that one (for both the neuron
>>> group and the function to run the simulation), otherwise use the
>>> python
>>> one.
>>>
>>>
>> You could give the cython and python modules slightly different names
>> (e.g., sim_cython and sim_python) and create a wrapper module (called
>> sim) that contains the following code:
>>
>> try:
>> from sim_cython import *
>> except ImportError:
>> from sim_python import *
>>
>
> Thanks! Now, if not every class is implemented in sim_cython,
> should I do:
>
> from sim_python import *
> try:
> from sim_cython import *
> except ImportError:
> pass
Yes, that would work well.
You might also be interested in http://wiki.cython.org/pure which may
allow you to keep a single codebase for the compiled and interpreted
implementations.
- Robert
More information about the Cython-dev
mailing list