Book - 3.10) Blockly - Dictionaries
Dictionaries
A dictionary can be created in Blockly as shown in the following example. The dictionary menu (labelled Dicts) contains the create dict with block. By default this block creates a dictionary with a single key/value pair. To add more key/value pairs, click the edit icon (the white star in the blue square) on the create dict with block. This exposes an additional window (see figure below) in which the key/value block can be dragged into the interior space in the Create Dictionary block to create an additional key/value pair in the dictionary. This step can be repeated to create as many key/value pairs as desired. When done, click on the edit icon again.
Creating a Dictionary in Blockly
The key/value pairs in the dictionary can be defined as shown in the next figure. Each key field can be edited to be the name of the desired key. Icons from the Values menu or the Properties menu can be inserted into the slot adjacent to each key position to define the vlue associated with that key. In the figure below, a dictionary has been created where the key author has the value ‘J.K. Rowlings’, the price key has the value 7.48, and the page count key has the value 320.
Defining the key/value Pairs in a Blockly Dictionary
A textual notation is used in Blockly to show the value of a dictionary in the Property Explorer and when the dictionary is printed. The figure below continues the example of the book dictionary used above. In this continuation, the dictionary has been set as the value of the property aBook and the value of this property is printed. When the Blockly program is run, the output field shows that dictionary printed in the text format and the Property Explorer shows that the property aBook is of type Dictionary an has the value represented in the text notation. Notice that the value of the property aBook is the entire dictionary. That is, the dictionary as a whole is treated as a single value. This is important because it means that no matter how many key/value pairs a dictionary might have the dictionary as a whole is a single value.
Use of Dictionary Text Notation in Blockly
In Blockly, the value associated with a key can be obtained as shown in the next figure. This figure continues the book example begun above. In this figure the get key from dict block has been selected from the Dicts menu. Inserted into the get key slot is the key page count as a string. The from dict slot contains the block for the property aBook. The get key from dict block represents the value associated with the specified key in the identified dictionary. In this example the value is set as the value of the property length. The value of length is printed and its value can also be seen in the Property Explorer. In each case the value of length is 320.
Accessing the Value Associated with a Key in a Blockly Dictionary
Combining Lists and Dictionaries
A simple example of a list of dictionaries is shown in the following figure. This table visually illustrates the abstraction of the weather forecast for a given city. The weather abstraction has three properties: temperature, humidity and wind. Each row of the table represents a separate instance of the abstraction (i.e. the next day in the forecast).
Graphical Representation of the Weather Forecast Abstraction
Because the properties of city, temperature, humidity, and wind are conceptually different and involve different types we would use a dictionary to represent each instance. This involves breaking the abstraction table apart and representing each instance (each row in the abstraction table) by a dictionary.
Because the forecasted weather data is in a list iteration can be used to access and manipulate each element of the list. The only difference is that the item selected from the list on each iteration is a complete dictionary. The following Blockly example shows how to print the wind speed values in each of the weather dictionaries.
Iterating Through List of Dictionaries
In this example the forecast property used in the iteration has as its value the list item currently selected in the Blacksburg_Forecast list. The forecast is a dictionary. The get key from dict block selects from the forecast dictionary the value associated with the key wind. This value is then printed.
If we examine the Property Visualizer (see next figure) we can see that the value of the property Blacksburg_Forcast is of type List and has a value consisting of a sequence of weather dictionaries. It can also be seen that the forecast property is of type Dictionary and its value is the value that it had on the last step of the iteration (i.e., the last dictionary in the list).
Iterating Through List of Dictionaries
Dictionaries with List Values¶
So far our examples dictionaries have key-value pairs where the value is a single basic type. For example, the temperature property is a single whole number, the crime rate is a single number with a decimal point, the name of a city is a single string. However, more intricate arrangements are possible - and needed.
We will now see that a dictionary’s key can have a list as its value. A simple example illustrating a property with a list value is the abstraction of a temperature forecast that separates the high temperatures that are forecast from the low temperatures that are forecast. This abstraction has two properties: highs and lows as shown in the following figure. The value associated with the highs property is a list of forecasted high temperatures. The lows property is a list of forecasted low temperatures. Each temperature is represented by a single whole number (i.e., an int type).
Dictionary Keys with List Values
In Blockly, the block in the Weather named “get highs and lows in <city>” returns the high and low temperatures for the selected city. The following figure show a Blockly algorithm to create a line drawing of the high and low temperatures for a given city on the same plot.
A Blockly Example of a Dictionary with List Values
The textual representation of a dictionary with list values can be seen by examining the output from the above program. The output is shown in the figure below. Notice that the value associated with the key ‘highs’ is a list of numbers. The same is true of the value associated with the key ‘lows’.
The Output from the Blockly Example of a Dictionary with List Values
More Complex Combinations¶
Having seen that lists and dictionaries can be combined individually we now want to see that these two data structures can be combined in multiple ways to represent an abstraction. This idea will be explored in more depth in the next section (see Layers of Abstraction).
Consider the example shown in the following figure that is that abstraction of a national weather forecast. In this abstraction, each instance is an abstraction of the forecast for a particular city. The city forecast abstraction has two properties: city and forecast. The forecast property gives the forecasted temperatures for that corresponding city.
Multiple Combinations of Lists and Dictionaries
To represent this abstraction as a data structure we will need:
- a list each element of which is an instance of the city forecast abstraction
- a dictionary to represent each city forecast abstraction; the dicionary has the two keys city and forecast*
- a list to represent the forecasted temperatures for a given city; this list is the value of the key forecast in the dictionary representing the city forecast abstraction.
Notice that with this organization we can talk meaningfully say:
- The national weather forecast abstraction is a list of city forecasts abstractions
- Each city forecast abstraction contains a list of temperatures
The following Blockly example shows an algorithm that will will create a line drawing of the forecast for each city in the national weather report. Notice that on each iteration, the value of the property City Report is a dictionary with the keys city and forecasts. In this algorithm the list of temperatures is accessed using the forecasts key. This list of temperatures is plotted. The name of the city is accessed using the city key. The name of the city is printed.
Multiple Combinations of Lists and Dictionaries in Blockly
The output from this Blockly program can be seen in the figure below. On each iteration, the name of the city is printed and the list of temperatures is plotted.
The Outputs from the Blockly Program