Created
September 3, 2021 21:46
-
-
Save rfinnie/eef4bb06edf2981649d458bf410e7443 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import yaml | |
class YamlEdit: | |
def __init__(self, filename): | |
self.filename = filename | |
def __enter__(self): | |
with open(self.filename) as f: | |
self.yaml_obj = yaml.safe_load(f) | |
return self.yaml_obj | |
def __exit__(self, exc_type, exc_val, exc_tb): | |
with open(self.filename, "w") as f: | |
yaml.dump(self.yaml_obj, f, default_flow_style=False) | |
if __name__ == "__main__": | |
with YamlEdit("file.yaml") as y: | |
y["foo"] = "bar" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment