Sublime Forum

Load dictionary from *.sublime-settings ? [Solved]

#1

I’m working on a plugin & currently have a dictionary of strings within the plugin.py file.

I’m looking for a way to externalize the dictionary to the plugin.sublime-settings file, in order to give the user the ability to define characters & strings that are used in the code.

I know that something like settings.get(“char_Example1”) would work for individual variables, but I have a LOT of dictionary entries & the code uses an integer loop to use them accordingly.

@ (my) plugin.py (Python)

dict_char_Example = {} dict_char_Example[1] = "☻" dict_char_Example[2] = "•"

@ (a typical) plugin.sublime-settings (JSON)

{ "char_Example1": "☻", "char_Example2": "•", }

Any ideas on how to transfer a dictionary between the two files?

0 Likes

#2
{"key":value}
0 Likes

#3

To me, this looks like a use case for a list and not a dict (because you are using an incrementing integer).

The settings file would then just look like this:

{
  "dict_char_example": "☻", "•", ...]
}
0 Likes

#4

That’s a great solution for a list, thanks! I actually just managed to figure out how to load a dictionary. The actual code I’ll be using has several groups of variables that are best defined in isolated groups (for overview / organizational purposes), so a dictionary is the best way to achieve that. Details @ next post.

0 Likes

#5

I figured it out after checking out a few plugins, here is the code in case anyone is interested:

@ MyPlugin.sublime-settings

[code]{

"dict_my_settings":
	
		{
			"str_a": "",
			"str_b": "",
		},
		{
			"str_a": "Variable 1-A",
			"str_b": "Variable 1-B",
		},
		{
			"str_a": "Variable 2-A",
			"str_b": "Variable 2-B",
		},
	],

}[/code]

@ MyPlugin.py

[code]def Test_Load_Dictionary_From_Settings( self, edit ):

settings_MyPlugin = sublime.load_settings("MyPlugin.sublime-settings")
dict_MySettings = settings_MyPlugin.get("dict_my_settings", ])

int_Current_MySettings = 2
dict_Current_MySettings = dict_MySettings[int_Current_MySettings]

print(dict_Current_MySettings"str_b"])

# result:    Variable 2-B[/code]

:mrgreen:

0 Likes