File: object.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 (60 lines) | stat: -rw-r--r-- 1,679 bytes parent folder | download | duplicates (2)
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
""" Attribute and method access on Python objects from C++.

    Note: std::cout type operations currently crash python...
          Not sure what is up with this...
"""
from __future__ import absolute_import, print_function

import scipy.weave as weave

#----------------------------------------------------------------------------
# get/set attribute and call methods example
#----------------------------------------------------------------------------


class Foo(object):
    def __init__(self):
        self.val = 1

    def inc(self,amount):
        self.val += amount
        return self.val
obj = Foo()
code = """
       py::tuple result(3);

       int i = obj.attr("val");
       result[0] = i;

       py::tuple args(1);
       args[0] = 2;
       i = obj.mcall("inc",args);
       result[1] = i;

       obj.set_attr("val",5);
       i = obj.attr("val");
       result[2] = i;

       return_val = result;
       """

print('initial, inc(2), set(5)/get:', weave.inline(code,['obj']))

#----------------------------------------------------------------------------
# indexing of values.
#----------------------------------------------------------------------------
from UserList import UserList
obj = UserList([1,[1,2],"hello"])
code = """
       int i;
       // find obj length and access each of its items
       //std::cout << "UserList items: ";
       //for(i = 0; i < obj.length(); i++)
       //    std::cout << obj[i].str() << " ";
       //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)