Created
March 18, 2013 12:01
-
-
Save empr/5186709 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
# see flask.config | |
import imp | |
class Config(dict): | |
def __init__(self, filename=None): | |
if filename: | |
self.from_pyfile(filename) | |
else: | |
dict.__init__({}) | |
def from_pyfile(self, filename): | |
d = imp.new_module('config') | |
d.__file__ = filename | |
execfile(filename, d.__dict__) | |
for key in dir(d): | |
self[key] = getattr(d, key) |
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
SPAM = 1 | |
HAM = 'foo bar baz' | |
EGGS = ( | |
'foo', | |
'bar', | |
'baz' | |
) |
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 | |
from config import Config | |
class TestConfig(unittest.TestCase): | |
def test_basic(self): | |
c = Config('my_config') | |
self.assertEqual(c['SPAM'], 1) | |
self.assertEqual(c['HAM'], 'foo bar baz') | |
eggs = ('foo', 'bar', 'baz') | |
self.assertEqual(c['EGGS'], eggs) | |
if __name__ == '__main__': | |
unittest.main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment