3 // Copyright (c) 2014 - Michele De Stefano (micdestefano@users.sourceforge.net)
5 // Distributed under the MIT License (See accompanying file LICENSE)
9 * \example dict_usage.pycmd
11 * Shows the usage of the dict_usage extension from the Python prompt.
14 mds_utils::python::Dictionary usage example from the Python prompt:
16 >>> import dict_usage as du
17 >>> d = {1:2.2,'r':[1,2,3.3,'aaa'],'4':(1,2,3)}
19 {1: 2.2, 'r': [1, 2, 3.3, 'aaa'], '4': (1, 2, 3)}
20 >>> d2 = du.copy_dict(d)
22 {1: 2.2, 'r': [1, 2, 3.3, 'aaa'], '4': (1, 2, 3)}
23 >>> d2['r'] = ('aa','bb','cc')
25 {1: 2.2, 'r': [1, 2, 3.3, 'aaa'], '4': (1, 2, 3)}
27 {1: 2.2, 'r': ('aa', 'bb', 'cc'), '4': (1, 2, 3)}
31 >>> du.dict_contains(d,1)
33 >>> du.dict_contains(d,'cc')
35 >>> du.dict_contains(d,'r')
37 >>> du.dict_contains_int_key(d,1)
39 >>> du.dict_contains_int_key(d,2)
41 >>> du.dict_contains_str_key(d,'r')
43 >>> du.dict_contains_str_key(d,'cc')
45 >>> du.dict_set_item(d,'cc','dd')
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
50 >>> du.dict_set_item(d,1,3.3)
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)
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)
58 {'cc': 'dd', 1: 3.3, 'r': 5, '4': (1, 2, 3), 't': 4}
59 >>> du.dict_del_item(d,'4')
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')
69 [('cc', 'dd'), (1, 3.3), ('r', 5), ('t', 4)]
76 >>> d2 = {'s':5,'r':8}
77 >>> du.dict_update(d,d2)
79 {1: 3.3, 's': 5, 'r': 8, 't': 4, 'cc': 'dd'}