Note that there are some explanatory texts on larger screens.

plurals
  1. POImport constants from .h file into python
    primarykey
    data
    text
    <p>I've been looking for a simple answer to this question, but it seems that I can't find one. I would prefer to stay away from any external libraries that aren't already included in Python 2.6/2.7.</p> <p>I have 2 c header files that resemble the following:</p> <pre class="lang-c prettyprint-override"><code>//constants_a.h const double constant1 = 2.25; const double constant2 = -0.173; const int constant3 = 13; </code></pre> <p>...</p> <pre class="lang-c prettyprint-override"><code>//constants_b.h const double constant1 = 123.25; const double constant2 = -0.12373; const int constant3 = 14; </code></pre> <p>...</p> <p>And I have a python class that I want to import these constants into:</p> <pre class="lang-py prettyprint-override"><code>#pythonclass.py class MyObject(object): def __init(self, mode): if mode is "a": # import from constants_a.h, like: # self.constant1 = constant1 # self.constant2 = constant2 elif mode is "b": # import from constants_b.h, like: # self.constant1 = constant1 # self.constant2 = constant2 </code></pre> <p>...</p> <p>I have c code which uses the constants as well, and resembles this: </p> <pre><code>//computations.c #include &lt;stdio.h&gt; #include &lt;math.h&gt; #include "constants_a.h" // do some calculations, blah blah blah </code></pre> <p>How can I import the constants from the header file into the Python class?</p> <p>The reason for the header files constants_a.h and constants_b.h is that I am using python to do most of the calculations using the constants, but at one point I need to use C to do more optimized calculations. At this point I am using ctypes to wrap the c code into Python. I want to keep the constants away from the code just in case I need to update or change them, and make my code much cleaner as well. I don't know if it helps to note I am also using NumPy, but other than that, no other non-standard Python extensions. I am also open to any suggestions regarding the design or architecture of this program.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload