nested_key_moved#

gufe.tokenization.nested_key_moved(dct, old_name, new_name)#

Serialization migration: Move nested key to a new location.

This can be used in when writing a serialization migration (see GufeTokenizable.serialization_migration()) where a key that is nested in a structure of dicts/lists has been moved elsewhere. It uses labels that match Python namespace/list notations. That is, if dct is the following dict:

{'first': {'inner': ['list', 'of', 'words']}}

then the label 'first.inner[1]' would refer to the word 'of'.

In that case, the following call:

nested_key_moved(dct, 'first.inner[1]', 'second')

would result in the dictionary:

{'first': {'inner': ['list', 'words']}, 'second': 'of'}

This is particular useful for things like protocol settings, which present as nested objects like this.

Parameters:
  • dct (dict) – dictionary based on the old serialization version

  • old_name (str) – label for the old location (see above for description of label format)

  • new_name (str) – label for the new location (see above for description of label format)

Returns:

input dictionary modified to move the value at the old location to the new location

Return type:

dict