Reputation: 825
I have some paths like (simplified): /rootFolder/subfolder_1/subsubfolder_1/
paths = {
'rootFolder':'rootFolder/'
'subFolder_1':paths[rootFolder]+'subFolder/'
'subsubFolder_1:paths[subfolder]+'subsubFolder/'
}
Basically, I want a static dictionary of paths, but I also want to keep dependencies so I can make just one change in the future. I am unsure if that's doable, and if, then how? Do you have some better ideas than mine? Thanks in advance for help.
Upvotes: 2
Views: 105
Reputation: 9240
You can use functions as values for the keys. Because functions are not called before the dict
is constructed, there'll be no NameError
.
In [320]: paths = { 'root': lambda: '/', 'usr': lambda: os.path.join(paths['root'](), 'usr')}
In [321]: paths['root']()
Out[321]: '/'
In [322]: paths['usr']()
Out[322]: '/usr'
In [323]: paths['root'] = lambda: '//'
In [324]: paths['usr']()
Out[324]: '//usr'
If you don't want do paths['root']()
, simply subclass dict
.
from types import FunctionType
import os
class DynDict(dict):
def __getitem__(self, key):
val = dict.__getitem__(self, key)
return val() if type(val) is FunctionType else val
paths = DynDict(root='/', tmp=lambda: os.path.join(paths['root'], 'tmp'))
print(paths['root'], paths['tmp'])
paths['root'] = '/var/'
print(paths['root'], paths['tmp'])
Output;
/ /tmp
/var/ /var/tmp
Upvotes: 2
Reputation: 103
paths = {
'rootFolder' : "'rootFolder/'",
'subFolder_1' : "eval(paths['rootFolder']) + 'subFolder/'",
'subsubFolder_1': "eval(paths['subFolder_1'])+ 'subsubFolder/'"
}
access via:
eval(paths["subsubFolder_1"])
# 'rootFolder/subFolder/subsubFolder/'
you can encapsulate the eval-usage by a function getPath("keyInDict") which do the user-unfriendly part of accessing the dict
Upvotes: 0
Reputation: 649
You can just create a function to return the dictionary that you want.
def path_dict(root):
return {'rootFolder':root,
'subFolder_1':root+'subFolder/'
'subsubFolder_1:root+'subfolder/subsubFolder/'
}
Then,
>>> path_dict('rootFolder/')
{'subsubfolder_1': 'rootFolder/subfolder/subsubfolder/', 'rootFolder': 'rootFolder/', 'subfolder_1': 'rootFolder/subfolder/'}
If you want to use keys in the dictionary to build off of, you can do it by initializing the dictionary in several steps.
paths = {'rootFolder':'rootFolder/'}
paths['subFolder_1'] = paths['rootFolder']+'subFolder/'
It is not a one-line instantiation of the directory, but you can combine the two to have a one-line instantiation with less code reuse.
def path_dict(root):
paths = {'rootFolder':root}
paths['subFolder_1'] = paths['rootFolder']+'subFolder/'
paths['subsubFolder_1'] = paths['subFolder_1']+'subsubFolder/'
return paths
Upvotes: 0