Last active
April 14, 2020 12:53
-
-
Save gaycookie/b0e285f0787dee15350a19d7d547b8cd to your computer and use it in GitHub Desktop.
YAML ConfigParser
This file contains 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
from yaml import safe_load, YAMLError | |
class ConfigParser: | |
def __init__(self, file_name): | |
self.config = self.__load(file_name) | |
def __load(self, file_name=''): | |
if len(file_name) == 0: | |
file_name = 'config.yaml' | |
with open(file_name, 'r') as stream: | |
try: | |
return safe_load(stream) | |
except YAMLError as exc: | |
print(exc) | |
def get(self): | |
return self.config |
This file contains 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
AppName: "ConfigParser" |
This file contains 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
from config import ConfigParser | |
Config = ConfigParser("config.yaml").get() | |
print(Config['AppName']) |
This file contains 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
PyYAML==5.3.1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment