Michele De Stefano's C++ Utilities
dict_usage.pycmd
1 // dict_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 dict_usage.pycmd
10  *
11  * Shows the usage of the dict_usage extension from the Python prompt.
12  */
13 
14 mds_utils::python::Dictionary usage example from the Python prompt:
15 
16 >>> import dict_usage as du
17 >>> d = {1:2.2,'r':[1,2,3.3,'aaa'],'4':(1,2,3)}
18 >>> d
19 {1: 2.2, 'r': [1, 2, 3.3, 'aaa'], '4': (1, 2, 3)}
20 >>> d2 = du.copy_dict(d)
21 >>> d2
22 {1: 2.2, 'r': [1, 2, 3.3, 'aaa'], '4': (1, 2, 3)}
23 >>> d2['r'] = ('aa','bb','cc')
24 >>> d
25 {1: 2.2, 'r': [1, 2, 3.3, 'aaa'], '4': (1, 2, 3)}
26 >>> d2
27 {1: 2.2, 'r': ('aa', 'bb', 'cc'), '4': (1, 2, 3)}
28 >>> du.clear_dict(d2)
29 >>> d2
30 {}
31 >>> du.dict_contains(d,1)
32 True
33 >>> du.dict_contains(d,'cc')
34 False
35 >>> du.dict_contains(d,'r')
36 True
37 >>> du.dict_contains_int_key(d,1)
38 True
39 >>> du.dict_contains_int_key(d,2)
40 False
41 >>> du.dict_contains_str_key(d,'r')
42 True
43 >>> du.dict_contains_str_key(d,'cc')
44 False
45 >>> du.dict_set_item(d,'cc','dd')
46 >>> d
47 {'cc': 'dd', 1: 2.2, 'r': [1, 2, 3.3, 'aaa'], '4': (1, 2, 3)}
48 >>> # You can notice that I've just created a new item
49 ...
50 >>> du.dict_set_item(d,1,3.3)
51 >>> d
52 {'cc': 'dd', 1: 3.3, 'r': [1, 2, 3.3, 'aaa'], '4': (1, 2, 3)}
53 >>> du.dict_set_item_str_int(d,'t',4)
54 >>> d
55 {'cc': 'dd', 1: 3.3, 'r': [1, 2, 3.3, 'aaa'], '4': (1, 2, 3), 't': 4}
56 >>> du.dict_set_item_str_int(d,'r',5)
57 >>> d
58 {'cc': 'dd', 1: 3.3, 'r': 5, '4': (1, 2, 3), 't': 4}
59 >>> du.dict_del_item(d,'4')
60 >>> d
61 {'cc': 'dd', 1: 3.3, 'r': 5, 't': 4}
62 >>> du.dict_del_item(d,'p')
63 Traceback (most recent call last):
64  File "<stdin>", line 1, in <module>
65 RuntimeError: Could not remove item from dictionary.
66 >>> du.dict_get_item(d,'r')
67 5
68 >>> du.dict_items(d)
69 [('cc', 'dd'), (1, 3.3), ('r', 5), ('t', 4)]
70 >>> du.dict_keys(d)
71 ['cc', 1, 'r', 't']
72 >>> du.dict_values(d)
73 ['dd', 3.3, 5, 4]
74 >>> du.dict_len(d)
75 4L
76 >>> d2 = {'s':5,'r':8}
77 >>> du.dict_update(d,d2)
78 >>> d
79 {1: 3.3, 's': 5, 'r': 8, 't': 4, 'cc': 'dd'}