Michele De Stefano's C++ Utilities
obj_usage.pycmd
1 // obj_usage.pycmd
2 //
3 // Copyright (c) 2014 - Michele De Stefano (micdestefano@users.sourceforge.net)
4 //
5 // Distributed under the MIT License (See accompanying file LICENSE)
6 
7 
8 /**
9  * \example obj_usage.pycmd
10  *
11  * Shows the usage of the obj_usage extension from the Python prompt.
12  */
13 
14 >>> import obj_usage as ou
15 >>> class A:
16 ... pass
17 ...
18 >>> a = A()
19 >>> ou.set_attr(a,'x',1.1)
20 >>> a.x
21 1.1
22 >>> a.y = 'aaa'
23 >>> ou.print_attr(a,'x')
24 Attribute value: 1.1
25 >>> ou.print_attr(a,'y')
26 Traceback (most recent call last):
27  File "<stdin>", line 1, in <module>
28 RuntimeError: Cannot get a double value
29 >>> # Obviously the implemented function is able to retrieve only double values
30 ...
31 >>> a.z = 5.6
32 >>> ou.print_attr(a,'z')
33 Attribute value: 5.6
34 >>> b = ou.dup_obj(a)
35 >>> b.x
36 1.1
37 >>> b.y
38 'aaa'
39 >>> b.z
40 5.6
41 >>> b.y = 'r'
42 >>> a.y
43 'r'
44 >>> class B:
45 ... def __call__(self):
46 ... print 'Output from __call__'
47 ... def test_call_attr(self):
48 ... print 'Output from test_call_attr'
49 ...
50 >>> b = B()
51 >>> ou.test_call(b)
52 Output from __call__
53 >>> ou.test_call_attr(b,'test_call_attr')
54 Output from test_call_attr
55 >>> class A:
56 ... def __call__(self,a=1,b=2,c=3):
57 ... print "a = ",a,"\nb = ",b,"\nc = ",c,"\n"
58 ...
59 >>> a = A()
60 >>> ou.test_call_args(a,())
61 a = 1
62 b = 2
63 c = 3
64 
65 >>> ou.test_call_args(a,('aa','bb',[1,2,3]))
66 a = aa
67 b = bb
68 c = [1, 2, 3]
69 
70 >>> ou.test_call_args_kw(a,(7,),{'b':5,'c':6})
71 a = 7
72 b = 5
73 c = 6
74 
75 >>> ou.test_call_args_kw(a,(),{'b':5,'c':6})
76 a = 1
77 b = 5
78 c = 6
79 
80 >>> ou.test_call_args_kw(a,(),{'b':5,'c':6,'a':8})
81 a = 8
82 b = 5
83 c = 6