Last active
October 19, 2015 10:36
-
-
Save Hornwitser/fdb9e6abd60ab6396ed9 to your computer and use it in GitHub Desktop.
Simple python configuration
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
| # Read the config file | |
| config = eval(open('cfg.py').read()) | |
| # Use it like any other dict | |
| config['Hello'] = 'Modified World' | |
| # Write the changed config file back | |
| lines = [' {!r}: {!r},'.format(k, config[k]) for k in sorted(config)] | |
| open('cfg.py', 'w').write('\n'.join(['# Modified config', '{']+lines+['}'])) | |
| # Sample cfg.py | |
| { | |
| 'Hello': 'World', | |
| 'entry 1': 'This is a simple configuration file', | |
| 'entry 2': 'loader and writer for python.', | |
| 'entry 3': 'It can read and write any object that', | |
| 'entry 4': 'can be reproduced with repr().', | |
| 'sample dict': {'key': 'nested dict', 'purpose': 'test'}, | |
| 'sample list': [3.14, 'mixed list', 3000], | |
| 'sample tuple': ('foo', 'bar', 123), | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment