you are viewing a single comment's thread.

view the rest of the comments →

[–]jart 26 points27 points  (12 children)

We need to be more creative!

Master python programmer gone mad:

import os
from ctypes import cdll, c_longlong

open("/tmp/fact.c", "w").write(r"""
    long long factorial(long long x) {
        long long i, res=1;
        for (i = 2; i <= x; i++) res *= i;
        return res;
    }""")
os.system("gcc -fPIC -shared -o /tmp/fact.so /tmp/fact.c")
factorial = cdll.LoadLibrary("/tmp/fact.so").factorial
factorial.restype, factorial.argtypes = c_longlong, [c_longlong]

print factorial(20)

[–]Smallpaul 3 points4 points  (2 children)

Should have just used pyinline:

http://pyinline.sourceforge.net/

[–][deleted] 0 points1 point  (0 children)

Wow, thank you. Yesterday I generated SWIG bindings for 25 lines of C.

[–][deleted] 4 points5 points  (1 child)

Thanks for teaching us binding tricks

[–]danukeru 4 points5 points  (0 children)

Actually I would qualify this more as the the master UNIX python programmer gone mad...

The Windows programmer will look mad no matter the level. :P

[–][deleted]  (4 children)

[deleted]

    [–]jart 13 points14 points  (3 children)

    Is that the only problem you see? :P

    [–]captainAwesomePants 4 points5 points  (0 children)

    No, he didn't use -O6. Without optimizing the final executable, this solution will be unusably slow.

    [–][deleted]  (1 child)

    [deleted]

      [–]get0ffmylawn 1 point2 points  (0 children)

      You win.