1
+ """
2
+ Copyright: MAXON Computer GmbH
3
+ Author: Maxime Adam, Ferdinand Hoppe
4
+
5
+ Highlights the key-functionalities of maxon.DataDictionary.
6
+
7
+ Topics:
8
+ - Constructing DataDictionary instances.
9
+ - Reading, writing, and deleting key-value pairs.
10
+ - Retrieving the number of contained pairs.
11
+ - Testing if a specific key is contained.
12
+ - Testing equality.
13
+ """
14
+ import typing
15
+ import maxon
16
+
17
+ def main ():
18
+ # A Python dictionary object as a data source.
19
+ pyDict : dict = {42 : "The answer." , "pi" : 3.1415 , True : "A string" }
20
+
21
+ # Create a reference to a `DataDictionaryInterface` object.
22
+ dataDict : maxon .DataDictionary = maxon .DataDictionary ()
23
+
24
+ # Items can be set with the `.Set` method or with the bracket syntax. Just as the standard
25
+ # Python dictionary, the type `DataDictionary` supports mixed key and value types.
26
+ dataDict .Set ("myKey" , "foo" )
27
+ for key , value in pyDict .items ():
28
+ dataDict [key ] = value
29
+
30
+ # However, only the most atomic Python types as string, int, float, and bool are being
31
+ # automatically converted to their maxon API equivalent. For storing a list of integer values
32
+ # under a key for example, one must convert that list explicitly to a BaseArray.
33
+ dataDict ["prime_numbers" ] = maxon .BaseArray (maxon .Int32 , [2 , 3 , 5 , 7 , 11 , 13 ])
34
+
35
+ # The number of key-value pairs in a `DataDictionary` instance can be evaluated with with
36
+ # `len()` and `.GetCount()`. Alternatively, `.IsPopulated()` or `IsEmpty() can be called to
37
+ # evaluate if an instance is empty or not.
38
+ length : int = len (dataDict )
39
+ alsoLength : int = dataDict .GetCount ()
40
+ isPopulated : bool = dataDict .IsPopulated ()
41
+ isEmpty : bool = dataDict .IsEmpty ()
42
+ print (f"\n dataDict: { length = } , { alsoLength = } , { isPopulated = } , { isEmpty = } " )
43
+
44
+ # Other than the standard Python dictionary, a `DataDictionary` instance will yield directly
45
+ # key-value pairs when iterated. Values can also be accessed with the bracket syntax and the
46
+ # method `.Get` which works similar to the method of the same name of a Python dict.
47
+ print ("\n State of dataDict:" )
48
+ for key , value in dataDict :
49
+ print (f"\t { key = } , { value = } " )
50
+
51
+ pi : float = dataDict ["pi" ]
52
+ # Will return the default value None, as the key "bar" does not exist.
53
+ bar : typing .Optional [str ] = dataDict .Get ("bar" , None )
54
+
55
+ # The occurrence of a key can be tested with the keyword `in` or the method `.Contains()`.
56
+ hasKeyPi : bool = "pi" in dataDict
57
+ hasKeyBar : bool = dataDict .Contains ("bar" )
58
+ print (f"\n dataDict: { hasKeyPi = } , { hasKeyBar = } " )
59
+
60
+ # Key-value pairs can be removed with the `del` keyword or the method `.Erase()`.
61
+ del dataDict ["pi" ]
62
+ dataDict .Erase (42 )
63
+
64
+ print ("\n New state of dataDict:" )
65
+ for key , value in dataDict :
66
+ print (f"\t { key = } , { value = } " )
67
+
68
+ # DataDictionary instances can also be compared for equaltiy but the comparison is carried out
69
+ # as an identity comparison.
70
+
71
+ # Two data dictionaries which are are being filled with the same content.
72
+ aData : maxon .DataDictionary = maxon .DataDictionary ()
73
+ bData : maxon .DataDictionary = maxon .DataDictionary ()
74
+
75
+ aData [1000 ] = "Hello World!"
76
+ aData ["value" ] = 42
77
+ bData [1000 ] = "Hello World!"
78
+ bData ["value" ] = 42
79
+
80
+ # Will evaluate as `True` because `xData` points to the same object as `aData`.
81
+ xData : maxon .DataDictionary = aData
82
+ print (f"\n { aData == xData = } " )
83
+
84
+ # Will evaluate as `False` because `aData` points to another object than `bData`. Not the
85
+ # content is being compared, but the identity of the two objects.
86
+ print (f"{ aData == bData = } " )
87
+
88
+ if __name__ == "__main__" :
89
+ main ()
0 commit comments