File: increment_example.py

package info (click to toggle)
python-scipy 0.18.1-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 75,464 kB
  • ctags: 79,406
  • sloc: python: 143,495; cpp: 89,357; fortran: 81,650; ansic: 79,778; makefile: 364; sh: 265
file content (40 lines) | stat: -rw-r--r-- 1,104 bytes parent folder | download
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
# examples/increment_example.py

# from weave import ext_tools

# use the following so that development version is used.
from __future__ import absolute_import, print_function

import sys
sys.path.insert(0,'..')
import ext_tools


def build_increment_ext():
    """ Build a simple extension with functions that increment numbers.
        The extension will be built in the local directory.
    """
    mod = ext_tools.ext_module('increment_ext')

    # Effectively a type declaration for 'a' in the following functions.
    a = 1

    ext_code = "return_val = PyInt_FromLong(a+1);"
    func = ext_tools.ext_function('increment',ext_code,['a'])
    mod.add_function(func)

    ext_code = "return_val = PyInt_FromLong(a+2);"
    func = ext_tools.ext_function('increment_by_2',ext_code,['a'])
    mod.add_function(func)

    mod.compile()

if __name__ == "__main__":
    try:
        import increment_ext
    except ImportError:
        build_increment_ext()
        import increment_ext
    a = 1
    print('a, a+1:', a, increment_ext.increment(a))
    print('a, a+2:', a, increment_ext.increment_by_2(a))