Skip to content

Instantly share code, notes, and snippets.

@JakubBlaha
Last active April 26, 2020 20:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JakubBlaha/e4a6a2acec08132988ce3c30d7b088bc to your computer and use it in GitHub Desktop.
Save JakubBlaha/e4a6a2acec08132988ce3c30d7b088bc to your computer and use it in GitHub Desktop.
Python configparser boilerplate
[GENERAL]
playlist_url = 'https://example.com'
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
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