Skip to main content

Easy embedded Inline C for Python

Project description

ezpyinline

The ezpyinline is a pure python module which requires almost no setup to
allows you put C source code directly "inline" in a Python script or module,
then the C code is automatically compiled and then loaded for immediate access from Python.

ezpyinline is forked from PyInline (http://pyinline.sourceforge.net/)
but aim to be as easy as possible and do all the magics for you.

It's great for test usage, since it's requires almost no setup for ezpyinline.
Just grab ezpyinline.py into your program directory and start to use it.

The compiled python extension will placed at ~/.ezpyinline/ ,
however, you could easily change this via EZPYINLINE_ROOTDIR to any where you want.
Also you don't need to care recompile or older version function/files,
ezpyinline will auto remove older version functions which you no longer needed.

ezpyinline tested in python2.4+ on linux,
however it should works in python2.3+.
You'll need python-dev and working C compilers/toolchains to compile C.
However, if you just wanna deploy on non-develop environment,
just copy the ~/.ezpyinline directory and it should also works.

Tutorial:

Using ezpyinline is very simple: (also see example 1: helloworld.py )

1. write a C function and put this function in a raw string literal.
2. create a name binding use ezpyinline.C()
3. call your C function in python from where you need it.
4. run your python program as there's no C code inside it.

So grab ezpyinline.py and copy it to your current directory,
(or just use easy_install -Z ezpyinline if you have setuptools installed),
and try the following example.

Note: use raw string parameter in ezpyinline.C() will be a good practice.
This prevents problems caused from the escape characters in C string literal.

* Example 1: helloworld.py

--- cut-here ---

#!/usr/bin/python
import ezpyinline

#step 1
code = r"""
int helloworld() {
printf("hello ezpyinline!
");
}
"""
#step 2
ezc = ezpyinline.C(code)

#step 3
ezc.helloworld()

----------------
and step 4: type python helloworld.py in shell:

#python helloworld.py
hello ezpyinline!

program should works now, if you have any problem,
check if your C compiler and python-dev package installed.

You could also write like this:

* Example 1.b: helloworld2.py

--- cut-here ---

#!/usr/bin/python
import ezpyinline

ezc = ezpyinline.C(
r"""
int helloworld() {
printf("hello ezpyinline
");
}
""")

ezc.helloworld()

----------------

* Example 2: prime.py

--- cut-here ---

#!/usr/bin/python
import ezpyinline

ezc = ezpyinline.C(
r"""
int prime(int num) {
int x;
for (x = 2; x < (num - 1) ; x++) {
if (num == 2) {
return 1;
}
if (num % x == 0) {
return x;
}
}
return 1;
}
""")

for num in xrange(10000):
is_prime = ezc.prime(num)
if is_prime == 1:
print "%d is a prime number" % num
else:
print "%d equals %d * %d" %(num,is_prime,num/is_prime)

----------------
run time in my p4-3.0G box:

real 0m0.590s
user 0m0.430s
sys 0m0.020s

----------------
#!/usr/bin/python
for num in xrange(10000):
is_prime = 1
for x in xrange(2,num-1):
if (num % x == 0):
is_prime = x
break

if is_prime == 1:
print "%d is a prime number" % num
else:
print "%d equals %d * %d" %(num,is_prime,num/is_prime)

----------------
run time in my p4-3.0G box:

real 0m3.300s
user 0m3.190s
sys 0m0.090s

* Example 3: oldstyle_pyinline.py

PyInline compatibility, you old PyInline code still works!

As for now, most of code in ezpyinline was from PyInline,
everything you can do in PyInline should still works.

--- cut-here ---
#import PyInline
from ezpyinline import PyInline # only change PyInline import to this line

m = PyInline.build(code="""
double my_add(double a, double b) {
return a + b;
}
""", language="C")

print m.my_add(4.5, 5.5) # Should print out "10.0"
----------------

If you need more complex usage please also see PyInline's README
(http://pyinline.cvs.sourceforge.net/pyinline/PyInline/README.txt?revision=1.2&view=markup)
,distutils package (http://www.python.org/sigs/distutils-sig/)
,Python/C API Reference Manual (http://docs.python.org/api/api.html)
and Extending and Embedding the Python Interpreter (http://docs.python.org/ext/ext.html)

For more more complex usage, you should check:
Pyrex: http://www.cosc.canterbury.ac.nz/greg.ewing/python/Pyrex/
weave: http://www.scipy.org/Weave
and ShedSkin: http://sourceforge.net/projects/shedskin

Feedbacks are very welcome, please send them to my email.

Website: http://ezpyinline.sf.net

Author: __tim__ <timchen119.at.gmail.com>
License: The Artistic License (http://www.opensource.org/licenses/artistic-license.php)

Changelog:
2007-02-08 Version 0.1 public released.

Project details


Release history Release notifications | RSS feed

This version

0.1

Download files

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

Source Distribution

ezpyinline-0.1.tar.gz (7.6 kB view hashes)

Uploaded Source

Built Distribution

ezpyinline-0.1-py2.4.egg (17.3 kB view hashes)

Uploaded Source

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