Skip to main content

wrapper for C functions

Project description

SClib
=====

A simple hack that allows easy and straightforward evaluation of C functions
within python code, boosting flexibility for better trade-off between
computation power and feature availability, such as visualization and existing
computation routines in SciPy.


At the core of SClib [#]_ is ctypes [Hell]_, which actually does the whole
work: it maps Python data to C compatible data and provides a way to call
functions in DLLs or shared libraries. SClib acts as glue: it puts things
together for the user, to provide him with an easy to use interface.

.. [#] The code for SClib and example use are availible at <https://github.com/drestebon/SClib>

The requirements for SClib are very simple: call a function on an array of
numbers of arbitrary type and size and return the output of the function, again
of arbitrary type and size.

The resulting interface is also very simple: A library is initialized in the
python side with the path to the DLL (or shared library) and a list with the
names of the functions to be called:

.. code-block:: python

In [1]: import SClib as sc
In [2]: lib = sc.Clib('test.so', ['fun'])

The functions are then available as a members of the library and can be called
with the appropriate number of arguments, which are one dimensional arrays of
numbers. The function returns a list containing the output arrays of the
function:

.. code-block:: python

In [3]: out, = lib.fun([0])

In the C counterpart, the function declaration must be accompanied with
specifications of the inputs and outputs lengths and types. This is
accomplished with the helper macros defined in sclib.h:

.. code-block:: c

#include <sclib.h>
PYO(fun, 1, 1);
PYO_TYPES(fun, 1, INT);
PYI(fun, 1, 1);
PYI_TYPES(fun, 1, INT);
void fun(int * out, int * in) {
*out = 42;
}

An arbitrary number of inputs or outputs can be specified, for example:

.. code-block:: c

#include <math.h>
#include <sclib.h>
PYO(fun, 2, 1, 2);
PYO_TYPES(fun, 2, INT, FLOAT);
PYI(fun, 2, 1, 2);
PYI_TYPES(fun, 2, INT, FLOAT);
void fun(int * out0, float * out1,
int * in0, float * in1) {
*out0 = 42*in0[0];
out1[0] = in1[0]*in1[1];
out1[1] = powf(in1[0], in1[1]);
}

In the function declaration, all the outputs must precede the inputs and must
be placed in the same order as in the PY macros.

These specifications are processed during compilation time, but only the number
of inputs and outputs is static, the lengths of each component can be
overridden at run time:

.. code-block:: python

In [4]: lib.INPUT_LEN['fun'] = [10, 1]
In [5]: lib.retype()

In these use cases the length of the arguments should be given to the function
through an extra integer argument.

In the function body, both inputs and outputs should be treated as one
dimensional arrays.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

SClib-1.0.0.tar.gz (5.7 kB view details)

Uploaded Source

Built Distributions

SClib-1.0.0.linux-x86_64.tar.gz (6.7 kB view details)

Uploaded Source

SClib-1.0.0-py2.py3-none-any.whl (6.8 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file SClib-1.0.0.tar.gz.

File metadata

  • Download URL: SClib-1.0.0.tar.gz
  • Upload date:
  • Size: 5.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for SClib-1.0.0.tar.gz
Algorithm Hash digest
SHA256 9f9e8d15652bd02b638c3e5bbaf55fc2e23d6e2d6e990669fb134777a32fbf66
MD5 91ecbf892389d69e9503ab393e2ae8f3
BLAKE2b-256 90de343fd8a2b84e7c355f6b1db8cbc828542d9e651637f14dd2ccda6b1139f0

See more details on using hashes here.

File details

Details for the file SClib-1.0.0.linux-x86_64.tar.gz.

File metadata

File hashes

Hashes for SClib-1.0.0.linux-x86_64.tar.gz
Algorithm Hash digest
SHA256 9f9d6776055d5f188cd8ce3dfa184c6a796665086b069b239edc77f100b2c6ab
MD5 03125e906e420eee46873ada92750298
BLAKE2b-256 3efdd27880221579b65d231b0150c0d7e1b10a850cc7d103c87323188c1000e0

See more details on using hashes here.

File details

Details for the file SClib-1.0.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for SClib-1.0.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 0057bbfc3bee874a5c0100d31d80d80afb702a609e1d568e8a02323fd3e2ad8f
MD5 e590858f42050e24ec0021b12cab6cb2
BLAKE2b-256 8c4ad5e3576dd665368494b3e6e61dba0499cd434de10ab9496ecaf1fa0c5e56

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page