Last active
February 27, 2018 19:28
-
-
Save sabinem/abb2befdb86f0b36eac8e967cf75d476 to your computer and use it in GitHub Desktop.
yaml_configuration_file_in_python
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
| ''' | |
| In this example I will show you how to read and write yaml configuration files in python | |
| (this is built for python 3.6, you can copy this into a Jupyter notebook and execute!) | |
| Background Information: | |
| ----------------------- | |
| yaml is a configuration language: | |
| - see this link for a good introduction on what is possible with yaml: https://learnxinyminutes.com/docs/yaml/ | |
| In python you need pyyaml to write and read yaml files: | |
| - install with: pip install pyyaml | |
| - see this link for the documentation: http://pyyaml.org/wiki/PyYAMLDocumentation | |
| ''' | |
| # Step1: import the library | |
| # ------------------------------------------------------ | |
| import yaml | |
| # Step 2: Read the Configuration file | |
| # ------------------------------------------------------ | |
| # - you can read yaml into a Python Class: that way you can make sure you get, what you expect | |
| # - normally you will read from file | |
| # - here we will also read from a string, so that you better see how the yaml file should look | |
| # make a configuration class, this here is the configuration of a wordpress xml parser | |
| class Configuration(yaml.YAMLObject): | |
| yaml_tag = u'!Config' | |
| def __init__(self, channel_info, post_types, status_types): | |
| self.channel_info = channel_info | |
| self.post_types = post_types | |
| self.status_types = status_types | |
| def __repr__(self): | |
| return "Configuration for {}".format(str(self.channel_info)) | |
| # now you can read from a configuration file directly into the class | |
| with open("config.yml", 'r') as ymlfile: | |
| cfg = yaml.load(ymlfile) | |
| # make sure you got the correct structure | |
| if not isinstance(cfg, Configuration): | |
| raise SyntaxError("{} is not a valid configuration file".format(ymlfile.name)) | |
| # you can also read from a string: | |
| config = """ | |
| !Config | |
| channel_info: [title, 'wp:base_site_url', description, language] | |
| post_types: [attachement, post, page] | |
| status_types: [draft, publish] | |
| """ | |
| cfg = yaml.load(config) | |
| print(cfg) | |
| # make sure you got the correct structure | |
| if not isinstance(cfg, Configuration): | |
| raise SyntaxError("{} is not a valid configuration file".format(ymlfile.name)) | |
| # Step 3: Write a configuration file | |
| # ------------------------------------------------------ | |
| # - you can reverse this procedure and write a configuration file from a python class | |
| with open("config_out.yml", 'w') as ymlfile: | |
| ymlfile.write(yaml.dump(cfg)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment