Last active
December 15, 2015 01:39
-
-
Save empr/5181315 to your computer and use it in GitHub Desktop.
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
import ConfigParser | |
class DictConfigParser(ConfigParser.SafeConfigParser): | |
def __getitem__(self, name): | |
d = {} | |
for item in self.items(name): | |
d[item[0]] = item[1] | |
return d |
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
import unittest | |
import io | |
from dictconfigparser import DictConfigParser | |
s = """ | |
[mysqld] | |
user = mysql | |
pid-file = /var/run/mysqld/mysqld.pid | |
skip-external-locking | |
old_passwords = 1 | |
skip-bdb | |
skip-innodb | |
""" | |
class TestDictConfigParser(unittest.TestCase): | |
def test_basic(self): | |
config = DictConfigParser(allow_no_value=True) | |
config.readfp(io.BytesIO(s)) | |
self.assertEqual(config['mysqld']['user'], 'mysql') | |
self.assertEqual(config['mysqld']['pid-file'], '/var/run/mysqld/mysqld.pid') | |
self.assertEqual(config['mysqld']['skip-external-locking'], '') | |
self.assertEqual(config['mysqld']['old_passwords'], '1') | |
self.assertEqual(config['mysqld']['skip-bdb'], '') | |
self.assertEqual(config['mysqld']['skip-innodb'], '') | |
with self.assertRaises(KeyError): | |
config['mysqld']['foo'] | |
if __name__ == '__main__': | |
unittest.main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment