Skip to content

Instantly share code, notes, and snippets.

@jasonkeene
Last active August 29, 2015 14:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jasonkeene/13472555a547d1775987 to your computer and use it in GitHub Desktop.
Save jasonkeene/13472555a547d1775987 to your computer and use it in GitHub Desktop.
Idea for class based settings.
from .local import config
config.render(globals())
import string
class late_bound(object):
def __init__(self, getter):
self.getter = getter
def __get__(self, instance, owner):
return self.getter(instance)
class BaseConfig(object):
DEBUG = False
@late_bound
def TEMPLATE_DEBUG(self):
return self.DEBUG
def __setattr__(self, name, value):
self.__dict__[name] = value
def __delattr__(self, name):
del self.__dict__[name]
def render(self, namespace):
namespace.clear()
for k in dir(self):
if not (set(k) - set(string.ascii_uppercase + '_') or k.startswith('_')):
namespace[k] = getattr(self, k)
return namespace
from .base import BaseConfig
class DevConfig(BaseConfig):
DEBUG = True
from .dev import DevConfig
config = DevConfig()
from pprint import pprint
import settings
if __name__ == '__main__':
pprint(settings.__dict__)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment