0

I've got a dictionary of key, value pairs that are basically a shorthand name for a file and the location to which the file should be written (e.g. {"curv" : r"C:\\dir\moredir\curv.tif", "dem" : r"C:\\dir\moredir\dem.tif"}. I would like to store the key as a variable name and its associated value as the variable value (dem = r"C:\\dir\moredir\dem.tif"), but am not sure how (or if I should).

I'd like to do something along the lines of this but setting the local or global variables instead of attributes of a class.

The only way I know to assign the key, value pairs as variables is to manually write

key = dict["key"]

I could do this, or use dict["key"] in the place of a variable, but this gets cumbersome across modules, where the dictionary names change. I would like to be able to run something to quickly convert that dictionary to variables so that curv alone will suffice.

Backstory: The dictionary is the product of another module/function designed to create the file paths for output files. I do not have access to the path outside of the return from that function. I can always write a new function to return the values differently, however.

Community
  • 1
  • 1
troubbble
  • 1
  • 1
  • 2
    Why would you want to do that, when you have a perfectly good dictionary to store those values? Store the *key* in a variable instead and use that to look up the value each time you need it. – Martijn Pieters Jan 25 '16 at 21:27
  • @MartijnPieters I may not be understanding. It looks like storing the key in a variable would still require me to first set the variable and then access it using dict[variable], so is not an improvement over dict["key"]. I would still manually have to list out the keys/variables. I'm trying to avoid having to manually set each key's value as a variable value. I didn't see a way to do that in the answers provided on the linked post. If the answer is "you just don't do that" that's OK, I just want to be sure I'm stating the issue clearly. – troubbble Jan 25 '16 at 22:06
  • Again, why would you want to dynamically set variables based on the keys in your dictionary, when you could just use the dictionary itself? What is excatly wrong with `dict[keyvariable]` over having to determine what variables you generated from the keys? – Martijn Pieters Jan 25 '16 at 22:08
  • @MartijnPieters It's mostly a matter of readability and a curiosity about the limits and correct use of Python. I'm applying a logistic regression model to the rasters that are being named in this dictionary, and the formula is already long and complicated. Cutting down a variable from `in_wsd_dict["curv"]` to `curv` makes it that much easier to read (and check for errors). I could, of course, shorten the dictionary name, though that will still be slightly messier than simply using `curv`. It's not essential, I was just curious if it was possible. – troubbble Jan 25 '16 at 22:18
  • Generating variables doesn't make code more readable. Because everywhere you *use* that variable now also has to be using dynamic lookups. If they don't, why generate the variables in the first place? – Martijn Pieters Jan 26 '16 at 07:23
  • @MartijnPieters I do agree that generating variables from the dictionary in a hidden manner does not make the code more readable (it would make it less readable, in fact). I'm simply wanting to be able to write something like (ignoring camel case): `formula = 1 / (1 + Exp(-11.5093+(avgd8slpCON*0.7875)+(profCON*5.0051)+(tlenCON*0.000004221)+(Ln(d8areaCON*d8slpCON)*0.7140)+(Ln(d8areaCON/d8slpCON)*0.9735)+(profCON*planCON*32.6120)+(planCON*fel*-0.0893)+(Ln(d8areaCON/d8slpCON)*Ln(d8areaCON/d8slpCON)*-0.00910)))` rather than (continued in next comment) – troubbble Jan 26 '16 at 17:33
  • `formula = "1 / (1 + Exp(-11.5093+(ma_file_dict["avgd8slpCON"]*0.7875)+(ma_file_dict["profCON"]*5.0051)+(ma_file_dict["tlenCON"]*0.000004221)+(Ln(ma_file_dict["d8areaCON"]*ma_file_dict["d8slpCON"])*0.7140)+(Ln(ma_file_dict["d8areaCON"]/ma_file_dict["d8slpCON"])*0.9735)+(ma_file_dict["profCON"]*ma_file_dict["planCON"]*32.6120)+(ma_file_dict["planCON"]*ma_file_dict["fel"]*-0.0893)+(Ln(ma_file_dict["d8areaCON"]/ma_file_dict["d8slpCON"])*Ln(ma_file_dict["d8areaCON"]/ma_file_dict["d8slpCON"])*-0.00910)))"` – troubbble Jan 26 '16 at 17:33
  • Sounds like you want to put that formula in a function with arguments; then use `defined_function(**ma_file_dict)` to pass in the arguments.. – Martijn Pieters Jan 26 '16 at 18:09
  • @MartijnPieters Thank you, that looks like the solution I was looking for! For anyone else that happens across this posting, please see [this](http://stackoverflow.com/questions/36901/what-does-double-star-and-star-do-for-python-parameters) and [this](http://stackoverflow.com/questions/5710391/converting-python-dict-to-kwargs) if (like me) you are unfamiliar with the use of `**` and passing dictionaries to functions. Also, I converted the dictionary to a class which slightly improved the ease of writing the code, but I will try your solution instead. – troubbble Jan 26 '16 at 18:32

0 Answers0