Welcome, guest | Sign In | My Account | Store | Cart

Use other languages like lua, C++ or simple bash from python. Instead of writing code as a string and then send it to some eval or execute function, we introduce here decorators that allow to write regular python functions containing the external code as docstring.

That way the external code needs minimal adoptions only and becomes better read- and maintainable. Write your function in your favourite language and just place the right decorator in front of it - it simple like that.

At the current state this recipe is a DRAFT and needs to be further extended and tested.

Python, 97 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import sys

import lua as _lua  # lua (lunatic-pathon)
import os           # bash
import scipy.weave  # Cpp

# more languages can and should be added


### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ###
# Definition of Decorators
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ###

def lua(function):
    def wrapper(*args):
        # 1. Do some preprocessing.
        #print function.__doc__

        # 2. Call 'function' with given arguments.
        _lua.execute(function.__doc__)
        function(*args)

        # 3. Do some postprocessing.
        # ...
    return wrapper

def bash(function):
    def wrapper(*args):
        # 1. Do some preprocessing.
        #print function.__doc__

        # 2. Call 'function' with given arguments.
        os.system(function.__doc__)
        function(*args)

        # 3. Do some postprocessing.
        # ...
    return wrapper

def Cpp(function):
    def wrapper(*args):
        # 1. Do some preprocessing.
        #print function.__doc__
 
        # 2. Call 'function' with given arguments.
        #scipy.weave.inline(function.__doc__,
        #                   ['u', 'dx2', 'dy2', 'dnr_inv', 'nx', 'ny'],
        #                   type_converters=converters.blitz,
        #                   compiler = 'gcc')
        scipy.weave.inline(function.__doc__)
        function(*args)

        # 3. Do some postprocessing.
        # ...
    return wrapper


### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ###
# Example
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ###

@lua
def lua_test():
    """print('lua run')
       print('Hello lua World')
       print('lua ok')
    """
    pass

@bash
def bash_test():
    """echo bash run
       echo Hello bash world!
       ls -la
       echo bash ok
    """
    pass

@Cpp
def Cpp_test():
    """printf("C++ run\\n");
       printf("Hello C++ world!\\n");
       printf("C++ ok\\n");
    """
    pass


print "run\n"

lua_test()
print "\n"
bash_test()
print "\n"
Cpp_test()
print "\n"

print "ok"

The result of the code above looks like:

run

lua run
Hello lua World
lua ok

bash run
Hello bash world!
[directory listing (omitted)]
bash ok

C++ run
Hello C++ world!
C++ ok

ok

Please contact me if there are any open questions.