How to use Dictionaries


#1

Can anyone give me a concrete example on how to use dictionaries? I do not understand how to use them and I can’t figure out what they can be use for. TIA


#2

A forum search on tag, or just “dictionaries”, will yield several topics.
Topics tagged with “dictionaries”

Here’s the best:


#3

Of course I searched and found all these topics :wink: But despite Peter’s explanations, I still do not understand how to use them concretely. Sorry.


#4

OK, well, how about we reverse this - do you have a specific task you want to accomplish for which Dictionaries (Hash tables) might be useful?


#5

Peter, this is exactly my problem, I don’t see how Dictionaries might be useful :persevere:


#6

Well, that’s ok then. Don’t worry about it. If you don’t have a problem to which dictionaries are the solution, then there is no point trying to use them.

There have been occasional forum questions for which dictionaries might be a helpful solution. Such as if you have a set of projects, and each one has a name and folder and client and email address, you can just switch one variable and use dictionary lookups to find the appropriate info.

But it’s not a tech that is going to be useful in too many cases.


#7

Many of my macros rely on a csv, which is read-in at the start of a macro. The macros then do a look-up of this csv and populate a number of variables.

My feeling is that dictionaries might handle this kind of thing, but given that:

  1. I have already written the macros; and
  2. the CSV is very easy to amend and update (which happens every two-three weeks when I have a new assignment)

I haven’t looked in them just yet :slight_smile:


#8

Thanks Peter, it’s a little bit clearer now :+1:


#9

I wonder whether the phrasing of the Wiki entry might become clearer with a slight adjustment of number ?

At the moment, the opening overview in

https://wiki.keyboardmaestro.com/manual/Dictionaries

explains that:

A dictionary is a mapping from a key name to a value

but might that not prove a bit confusing ? It sounds rather like one name and one value – which would be puzzling – not very obviously different, at first reading, from a Keyboard Maestro variable …

In fact, of course, a dictionary is not a single key name -> Value mapping, it is a named collection of different key -> value mappings.

Classic examples of dictionaries might be a named menu, with a set of menu item label -> menu item value mappings or a named group of addresses, with a set of names mapped to address details.

Perhaps the opening phrase could be edited to something like:

A dictionary is a named set of mappings from key names to corresponding values

(AppleScript records and JavaScript dictionaries might also be recognisable analogues to some readers, of course)

{ "alpha": 1, "beta": 2, "gamma": 3 } etc

(otherwise known as a name-space, or a set of names for values)


#10

I’ve adjusted the wiki page.


#11

I use dictionaries frequently in Python. Given that perspective, I’m trying to understand the syntax for their use in KM.
Here’s an example:
I have created a dictionary in KM, by using a stock ticker as the key and the value as the URL to stock research for that ticker. <key=abbv> – <value=https://research.investors.com/stock-checkup/nyse-abbvie-inc-abbv.aspx>
I want to roll thru this dictionary, opening each URL, processing the HTML page text, and associating that data with the ticker.
I’ve got a lot of this working (in Python), but I need to work with the dictionary in KM to wrap the whole process.

in python, it’s
for thekey in thedict.keys():
do_stuff with thedict[thekey]

how does this work in KM?

Thanks!


#12

In KM, this is handled with a For Each action and a dictionary keys collection:


#13

I don’t know about Python, but in KM Dictionaries do not support arrays, and KM does not have traditional arrays. So it becomes complicated in KM. You would have to create a separate KM Dictionary for each stock (named Stock1, Stock2, …), and all of them would have two keys: Symbol and URL.

So, here is an example macro that will generate a list of all stocks, the Symbol and URL for each:

@peternlewis, if there is a better, simpler, way of doing this, please educate all of us.

###MACRO:   Dictionary Test @TEST

~~~ VER: 1.0    2017-12-31 ~~~

####DOWNLOAD:
Dictionary Test @TEST.kmmacros (7.4 KB)
Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.


Example Results


Frankly, I would never use KM for this use case. I’d use JavaScript (in a Execute a JavaScript For Automation action (KM Wiki), where it is very easy and compact to define an array of stock objects, and to search, select, sort them. But then I’m still struggling with how to use Dictionaries manual (KM Wiki).


#14

I solved it, once I understood the syntax.
First I created the dictionary with the tickers and URLs.


Then I process.

I got a little stalled until I noticed it’s %Variable, not %variable.

Thanks for the help!!


#15

Could you please post your macro just so we all will have a good example?

It seems very limited in that you are having to use the stock symbol as the key name, and then storing the URL as the value for that key. So the key name does not reveal any info about the nature of its value. This means you no way to provide additional keys about a specific stock, unless you use a kluge like naming the key as stockSymbol-keyword, as in “vz-URL”.


#16

Hi JMichaelTX,

Creating the dictionary was a mostly manual process. I chose not to automate the creation of the dictionary, due to the IBD website’s wonky research page. Even so, it was a cinch to create the dictionary. I just use the Set Dictionary Action, fill in the ticker, go to IBD and fill in the ticker there, and hit the Get Stock Checkup button. After I hit that button, the page redirects to the URL for that ticker. Granted, scripting this in KM is relatively easy, but IBD will misbehave after some number of tickers, and you have to kill the macro.

So,

  1. Create a macro (pictured in a previous post)
  2. insert a Set Dictionary action
  3. fill in the dictionary field with “stock_tickers”
  4. Fill in the key with the stock ticker
  5. Fill in the URL with the URL you get from IBD
  6. Create a new Set Dictionary action, using the same name in the dictionary field, but a different ticker and URL. Repeat this step with all your tickers.
  7. When done adding your ticker actions, save and run the macro

You end up with one dictionary, holding some number of tickers with their associated URL.

set stock dictionary values.kmmacros (6.9 KB)


#17

You could either have multiple dictionaries, eg a Symbol dictionary and a URL dictionary, or you can have compound keys “key1.symbol” “key1.URL”.


#18

Thanks, Peter. I like that approach the best.


#19

Hi all,

The solution I came up with solved my issue perfectly, in the simplest manner. Given the additional discourse here, I may not have stated my issue clearly enough. My solution was simply a means to an end. KM allowed me to elegantly solve a piece of a larger project, which is Python based.

But I’m still thinking about the conversation here, trying to figure out what you’re trying to solve.

I believe you’re trying to add more data to the value part of the dictionary. As mentioned, the key.URL, key.symbol, key.other_stuff could work. Seems somewhat unwieldy to me. If you need additional data in the value, why not make the value another dictionary? That value could look like this:
{“stock_name”: “Toronto Dominion”, “market_cap”: “$68,000,000”, “dividend_yield”: “3.9%”} I’m doing something like this in my Python project. I use the URLs to nab the HTML source for each ticker, and parse that HTML for the data I need. The data elements go into a Python dictionary which has about 40 elements.

I find working with dictionaries easier in Python than KM, but I’m more familiar with Python. I’m excited to see dictionaries added to KM. KM is a really useful tool.


#20

Generally speaking, one dictionary object contains all properties for the given target object. So if my target is stocks, then one dictionary object would be for one stock, and it would include all desired properties of that stock: symbol, name, company, rating, URL, etc.

The complication is that you want to associate the dictionaries for all stocks together.
In JavaScript, you can create one variable which is an array of objects, and then search, sort that array on any object property. Since KM does NOT support arrays, we have use other schemes, such as the ones that @peternlewis described.

IMO, KM dictionaries work well for simple use cases. If you have a complex use case, and know how to use dictionaries (objects) in other tools, then the other tool (Python, JavaScript, etc) may be a better tool to use. It’s up to you.

The tool features missing from KM Dictionaries that could be important are search and sort.
Yes, you can do a search by using a For Each Action to loop through all dictionaries and then a If Then to find the ones that match. But then what? You can’t get an array of found dictionaryies, so you have to extract the properties of the object you want, and then store them somewhere. But where? . . . so it gets complicated and verbose quickly.

There is no sort in KM. So you’d have to extract the properties to sort on into a text list, and then use another tool, like Bash sort, to sort the data.

As always, I try to use the best tool I know how to use for the job. So, it might be KM in some cases, and JavaScript in other cases; or even ASObjC, which has very powerful dictionary tools.

But this is just version 1 of KM Dictionaries. Who knows what additional features @peternlewis has planned for us. :smile: