< pre class="lang-py Prettyprint-override">
Код: Выделить всё
from distutils.core import setup, Extension
libDIV = Extension(
'DIV',
sources = ['example.c']
)
setup (
name = 'divv',
version = '0.1',
description = 'DIVV, you want it',
ext_modules = [libDIV],
packages = [''],
package_data={'': ['libDIV.so']}
)
Код: Выделить всё
/usr/local/lib/python3.4/dist-packages/DIV.cpython-34m.so
Код: Выделить всё
import ctypes
lib = ctypes.cdll.LoadLibrary('DIV.so') # not sure what to do here
def divide_modulo(a, b):
div = ctypes.c_int(0)
rest = ctypes.c_int(0)
lib.divide_modulo(a, b, ctypes.byref(div), ctypes.byref(rest))
return (div.value, rest.value)
print(divide_modulo(11, 4))
class DynamicArray(ctypes.Structure):
_fields_ = [("n", ctypes.c_int), ("r", ctypes.POINTER(ctypes.c_int))]
Как мне распространить/настроить setup.py для библиотеки c-файлов, которые встраиваются в общую библиотеку, и некоторого кода Python для обертывания c-функциональности?
Для полноты это example.c:
Код: Выделить всё
#include
void divide_modulo(int a, int b, int *div, int *rest)
{
*div = a / b;
*rest = a % b;
}
typedef struct dynamic_array {
int n;
int *r;
} dynamic_array;
dynamic_array return_dynamic_array(int n)
{
dynamic_array r;
r.n = n;
r.r = (int *)malloc(sizeof(int) * n);
unsigned int i = 0;
for (; i < n; ++i)
r.r[i] = i;
return r;
}
В настоящее время я делаю это, чтобы найти имя файла:< /p>
Код: Выделить всё
import ctypes, numpy, site, os.path
from scipy.sparse import csr_matrix
so_file = next(
os.path.join(directory, filename)
for directory in site.getsitepackages()
for filename in os.listdir(directory)
if filename.startswith("DIV.") and filename.endswith(".so")
)
lib = ctypes.cdll.LoadLibrary(so_file)
Подробнее здесь: https://stackoverflow.com/questions/343 ... ges-folder