Last active
April 26, 2020 20:17
-
-
Save JakubBlaha/e4a6a2acec08132988ce3c30d7b088bc to your computer and use it in GitHub Desktop.
Python configparser boilerplate
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
[GENERAL] | |
playlist_url = 'https://example.com' | |
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
from configparser import ConfigParser | |
DEFAULTS = { | |
'GENERAL': { | |
'playlist_url': '' | |
} | |
} | |
FILENAME = 'config.ini' | |
config = ConfigParser() | |
config.read_dict(DEFAULTS) | |
config.read(FILENAME) | |
with open(FILENAME, 'w') as f: | |
config.write(f) | |
# For easier importing | |
conf = config['GENERAL'] | |
# Validation | |
try: | |
assert conf['playlist_url'], 'playlist_url must be a string' | |
except AssertionError as ex: | |
print(ex) | |
input('Invalid config! Press enter to exit...') | |
raise |
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
from config import conf | |
print(conf['playlist_url']) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment