Reputation: 151
I would like to include code from a file for use in another file, much like importing a module, but I don't want to compile the first file in order to do so.
My project pipeline looks something like this:
file A defines an extension type object as so:
cdef class Thing(object):
...
There will be many different versions of this kind of file, each one defining a class called "Thing".
file B does all types of neat stuff using the class "Thing" but does not define it in file B, as file B is able to perform general actions on many different kinds of objects called "thing" defined in file A.
e.g.
cdef int do_cool_stuff(Thing a, Thing b):
...
Basically. I can copy-paste the code from file B into each file A and it will all work properly. But I would like to organize my project better, and these two types of files do very different tasks, and I will want to be mixing and matching these tasks.
Any advice?
In addition, it would be nice to be able to do something like this:
in file A:
ctypedef ... newtype
and in file B:
cdef newtype...
If it helps, the newtype will be very similar to a double, but may carry more information. I will be overloading comparison operators <, >, ==, etc, but I want the newtype to carry more information than a double.
Edit: I will add some more information, as it seems that most readers feel this problem is more trivial than it is.
I have several programs that are very long, and the files can all be partitioned into two distinct parts, the second half of each of these parts is identical codewise, but they all use a type that is defined in the first part. The second part cannot stand on its own because it depends on the ctypedef statement in part one.
So what I would like is this: file1:
cdef class Thing:...
file2:
stuff(Thing one, Thing two)
file3:
import file1
import file2
and compile file3, with files 1 and 2 uncompiled.
If I make file1 and file2 .pxd files and use cimport I get complaints about def
statements not allowed and that all cdef
statements must be inlined. While if I use import with .py files the compiler complains that file1
is not a cimported module.
Why does cython insist that my header files be formatted in a much more restrictive manner than my main file? I essentially want to avoid copy-pasting file2 into my main files, it would be nice to write it once and import it, but I don't seem to be able to find any documentation that can help me through this.
Upvotes: 1
Views: 1819
Reputation: 151
As DavidW mentioned in comments, what I am likely looking for is the
include "filename.pxi"
command. Which is indicative of sloppy planning. The best coding practices would be to create .pxd and .pyx files to augment the main .pyx file.
Upvotes: 2