blob: b69adbbe6570ba07dc365b84ed1c972ec69b51e3 (
plain)
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
|
# h:\wrk\scipy\weave\examples>python object.py
# initial val: 1
# inc result: 2
# after set attr: 5
import weave
#----------------------------------------------------------------------------
# get/set attribute and call methods example
#----------------------------------------------------------------------------
class foo:
def __init__(self):
self.val = 1
def inc(self,amount):
self.val += 1
return self.val
obj = foo()
code = """
int i = obj.attr("val");
std::cout << "initial val: " << i << std::endl;
py::tuple args(1);
args[0] = 2;
i = obj.mcall("inc",args);
std::cout << "inc result: " << i << std::endl;
obj.set_attr("val",5);
i = obj.attr("val");
std::cout << "after set attr: " << i << std::endl;
"""
weave.inline(code,['obj'])
#----------------------------------------------------------------------------
# indexing of values.
#----------------------------------------------------------------------------
from UserList import UserList
obj = UserList([1,[1,2],"hello"])
code = """
int i;
// find obj length and accesss each of its items
std::cout << "UserList items: ";
for(i = 0; i < obj.length(); i++)
std::cout << obj[i] << " ";
std::cout << std::endl;
// assign new values to each of its items
for(i = 0; i < obj.length(); i++)
obj[i] = "goodbye";
"""
weave.inline(code,['obj'])
print "obj with new values:", obj
|