Skip to content

Instantly share code, notes, and snippets.

@bfroehle
bfroehle / file_wrapper.cpp
Created Oct 5, 2011
Boost.Python from Python to FILE* wrapper
View file_wrapper.cpp
#include <boost/python.hpp>
#include <iostream>
namespace {
void *convert_to_FILEptr(PyObject* obj) {
return PyFile_Check(obj) ? PyFile_AsFile(obj) : 0;
}
}
void test_argument(FILE* f) {
@bfroehle
bfroehle / printf.c
Created Oct 13, 2011
How does Cython compile print?
View printf.c
/* Generated by Cython 0.13 on Thu Oct 13 10:39:36 2011 */
#define PY_SSIZE_T_CLEAN
#include "Python.h"
#ifndef Py_PYTHON_H
#error Python headers needed to compile C extensions, please install development version of Python.
#else
#include <stddef.h> /* For offsetof */
#ifndef offsetof
@bfroehle
bfroehle / reporting.cpp
Created Oct 13, 2011
printf alternative in Python
View reporting.cpp
#include <boost/python.hpp>
void report(std::string msg) {
PyObject *f = PySys_GetObject((char *)"stdout");
PyFile_WriteString(msg.c_str(), f);
PyFile_WriteString("\n", f);
}
BOOST_PYTHON_MODULE(reporting) {
boost::python::def("report", &report);
@bfroehle
bfroehle / bad.py
Created Jan 27, 2012
Using MethodType to convert builtin function to instancemethod.
View bad.py
from demo import incx
class Point(object):
"""A 1-diml Point"""
def __init__(self, x):
self.x = x
incx = incx
p = Point(0)
p.incx()
@bfroehle
bfroehle / setup.py
Created Feb 7, 2012
A simple example to trigger cython/cython PR-85
View setup.py
from distutils.core import setup
from Cython.Build import cythonize
setup(name = 'tst',
ext_modules = cythonize(['tst.pyx'])
)
@bfroehle
bfroehle / knee.py
Created Feb 29, 2012
knee.py for Python 2.6, 2.7, and 3.2
View knee.py
"""
An Python re-implementation of hierarchical module import.
Function names and arguments have been chosen to mimic the C code in
`Python/import.c` whenever possible.
This code is intended to be read, not executed. However, it does work
-- all you need to do to enable it is "import knee".
(The name is a pun on the klunkier predecessor of this module, "ni".)
View test_oinspect.py
import sys
import nose.tools as nt
from IPython.core import oinspect
def test_oinspect_f2py():
from numpy import f2py
# This is FORTRAN, so the indentation is important:
source = \
@bfroehle
bfroehle / 00README.rst
Created Jun 13, 2012 — forked from GaelVaroquaux/00README.rst
Copy-less bindings of C-generated arrays with Cython
View 00README.rst

Cython example of exposing C-computed arrays in Python without data copies

The goal of this example is to show how an existing C codebase for numerical computing (here c_code.c) can be wrapped in Cython to be exposed in Python.

The meat of the example is that the data is allocated in C, but exposed in Python without a copy using the PyArray_SimpleNewFromData numpy

@bfroehle
bfroehle / gist:3153959
Created Jul 21, 2012
IPython #2179 Failure
View gist:3153959
..............................................
----------------------------------------------------------------------
Ran 46 tests in 0.066s
OK
..................................................................................................................................................................................................................S....................................................................................................................................K.........
----------------------------------------------------------------------
Ran 353 tests in 23.375s
OK (KNOWNFAIL=1, SKIP=1)
@bfroehle
bfroehle / results.log
Created Jul 31, 2012
IPython test log
View results.log
..............................................
----------------------------------------------------------------------
Ran 46 tests in 0.060s
OK
....................................................E.....................................................................................................................................................................................................................................................................................................................................................................................................................................K.........
======================================================================
ERROR: test format constraint, and validity of jpeg and png
----------------------------------------------------------------------
Traceback (most recent call last):