python-extensions

Passing numpy integer array to c code

纵饮孤独 提交于 2019-12-22 13:57:33
问题 I'm trying to write Cython code to dump a dense feature matrix, target vector pair to libsvm format faster than sklearn's built in code. I get a compilation error complaining about a type issue with passing the target vector (a numpy array of ints) to the relevant c function. Here's the code: import numpy as np cimport numpy as np cimport cython cdef extern from "cdump.h": int filedump( double features[], int numexemplars, int numfeats, int target[], char* outfname) @cython.boundscheck(False)

Building 64-bit Python extensions with f2py on Windows

萝らか妹 提交于 2019-12-20 10:37:18
问题 I'm attempting to build a Python extension from Fortran source using Numpy's f2py.py script. I'm following the steps from http://www.scipy.org/F2PY_Windows (web archive). My system is Windows 7 64-bit, and I primarily use Python 2.7.3 [MSC v.1500 64 bit (AMD64)]. I have Numpy-MKL 1.7.1, from http://www.lfd.uci.edu/~gohlke/pythonlibs/#numpy Here is what I've tried: Choose a Fortran compiler. Since I'm using 64-bit Python, a 64-bit Fortran compiler is required. From MinGW-w64, I've tried a few

Aspect oriented programming (AOP) in Python [duplicate]

百般思念 提交于 2019-12-18 10:28:12
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Any AOP support library for Python? I am familiar with the AspectJ extension for the Java language. I want to know if there is such a thing for Python. Don't get me wrong, I do not mean a library but a language extension like AspectJ is to Java. 回答1: Python does not need something like a "language extension" for being able to work in an Aspect Oriented way. That is simply due to the dynamic mechanisms in Python

How to build a Python C Extension so I can import it from a module

北战南征 提交于 2019-12-17 10:41:32
问题 I have a Python project with many sub-modules that I package up with distutils. I would like to build some Python extensions in C to live in some of these sub-modules but I don't understand how to get the Python extension to live in a submodule. What follows is the simplest example of what I'm looking for: Here is my Python extension c_extension.c : #include <Python.h> static PyObject * get_answer(PyObject *self, PyObject *args) { return Py_BuildValue("i", 42); } static PyMethodDef Methods[]

Can I use MinGW compiled Python extensions together with Visual C++ compiled ones?

强颜欢笑 提交于 2019-12-14 03:09:20
问题 Having trouble compiling a Python extension under Windows, I've asked a question.One of the answers does not answer my question but is worth asking as a question on its own. Given a Visual C++ compiled Python distribution under Windows, would I have any problems if I use Visual C++ compiled extensions along with MinGW compiled ones? This would allow me resort to MinGW when it is easier than configuring MS compiler. 回答1: It's not officially supported, but I think it should work. Python exposes

How to write python extensions in pure asm and would it be efficient?

◇◆丶佛笑我妖孽 提交于 2019-12-12 03:43:45
问题 I have medium amateur skills in Python and I'm beginner in asm and haven't any knowledge of C -language. I know that python C -extensions must follow specific interface to work fine. Is this possible to write python extension in pure Assembly with the right interface and full functionality? The second question is would it be efficient enough if case of doing it right? While googling I haven't found any examples of code or some articles or solutions about this question. And this ISN'T the

Python extensions with C: staticforward

人盡茶涼 提交于 2019-12-11 01:34:27
问题 So I needed to use the code of the subprocess module to add some functionality I needed. When I was trying to compile the _subprocess.c file, it gives this error message: Error 1 error C2086: 'PyTypeObject sp_handle_type' : redefinition This is the code part which is relevant from _subprocess.c file: typedef struct { PyObject_HEAD HANDLE handle; } sp_handle_object; staticforward PyTypeObject sp_handle_type; static PyObject* sp_handle_new(HANDLE handle) { sp_handle_object* self; self =

Data corruption: Where's the bug‽

浪子不回头ぞ 提交于 2019-12-10 03:49:09
问题 Last edit: I've figured out what the problem was (see my own answer below) but I cannot mark the question as answered, it would seem. If someone can answer the questions I have in my answer below, namely, is this a bug in Cython or is this Cython's intended behavior, I will mark that answer as accepted, because that would be the most useful lesson to gain from this, IMHO. Firstly, I have to start by saying that I have been trying to figure this out for three days, and I am just banging my

Pointers and “Storing unsafe C derivative of temporary Python reference”

南楼画角 提交于 2019-12-10 03:21:15
问题 I was writing code to store a (potentially) very large integer value into an array of chars referenced by a pointer. My code looks like this: cdef class Variable: cdef unsigned int Length cdef char * Array def __cinit__(self, var, length): self.Length = length self.Array = <char *>malloc(self.Length * sizeof(char)) # Error for i in range(self.Length): self.Array[i] = <char>(var >> (8 * i)) def __dealloc__(self): self.Array = NULL When I tried compiling the code, I got the error, "Storing

Integrating C and Python: ValueError: module functions cannot set METH_CLASS or METH_STATIC

浪尽此生 提交于 2019-12-08 05:13:02
问题 I am making my first venture into integrating C and Python 2.7.3. For starters, I'm just trying to write a C module for Python that can do basic addition. (It is called npfind because once I figure this out, I want to write a find method for numpy) npfind.h: #include <math.h> extern int add(int a, int b); npfind.c: #include "npfind.h" int add(int a, int b) { return a + b; } pynpfind.c: #include "Python.h" #include "npfind.h" static char* py_add_doc = "Adds two numbers."; static PyObject* py