Skip to content

Instantly share code, notes, and snippets.

@empr
Created March 18, 2013 12:01
Show Gist options
  • Save empr/5186709 to your computer and use it in GitHub Desktop.
Save empr/5186709 to your computer and use it in GitHub Desktop.
# 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)
SPAM = 1
HAM = 'foo bar baz'
EGGS = (
'foo',
'bar',
'baz'
)
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