My program is a pipeline that processes files. I have a dict (P) which stores directory Path's. All of these directory Path's are are relative to a common ROOT Path from which they are generated. The dict works when I define ROOT outside of the dict as follows:
# WORKS
from pathlib import Path
ROOT = Path("/very/long/path/")
P = {
"ROOT": ROOT,
"FS_TO_IDX": ROOT / "docs/",
"IDXD_FS": ROOT / "indexed_docs/",
}
This seems inelegant. Since ROOT is already an element of dict, I would prefer to use the ROOT value in generating the remaining dict values. However, I get "Undefined variable:P" when I do the following.
# FAILS
from pathlib import Path
P = {
"ROOT": Path("/very/long/path/"),
"FS_TO_IDX": P["ROOT"] / "docs/",
"IDXD_FS": P["ROOT"] / "indexed_docs/",
}
Is there a similar approach that would allow me to assign a dict value and then use that same key/value to define other values in the dict? For example, the walrus operator (:=) seems to provide similar behavior by allowing one to assign to variables within an expression and then use that variable.