I'm trying to use a YAML config file in my Python script, one of the problems I've found is that I can't access other attributes, so I need to duplicate a lot of content.
For example
root_path: /root
script_path: root_path + /scripts
This doesn't exists of course, but is there some way to achieve this? Because there are a lot of contents that I can't duplicate because when one changes, I need to change it everywhere...
I've also looked at creating my own join function
root_path: &ROOT /root
script_path: !join [*ROOT, '/scripts']
def join(loader, node):
seq = loader.construct_sequence(node)
return ''.join([str(i) for i in seq])
yaml.add_constructor('!join', join)
But I need to set a &VARNAME every time, it would be nice to automatically set the parameter key as the referenceable object...
Well, I've been working on a solution because nothing was what I wanted and everything was so complex I wanted to die...
This solutions transforms a
%something%string into the value forsomething.It works perfectly, this is an example
With this method,
script_pathwill become/root/scripts.Usage is easy, just load a
Yamlfile the normal way, and call replace.Then our new
configwill store the replaced values, it doesn't overwrite the original.ymlfile of course. I hope you find this useful, I really do because it was exactly what I needed.I used a percentage
%token, but you can change it to whatever you want and change the method to make it work with regex (some tokens are used by regex that's why I used%)