Last active
August 29, 2015 14:12
-
-
Save jasonkeene/13472555a547d1775987 to your computer and use it in GitHub Desktop.
Idea for class based settings.
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
from .local import config | |
config.render(globals()) |
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 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 |
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
from .base import BaseConfig | |
class DevConfig(BaseConfig): | |
DEBUG = True |
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
from .dev import DevConfig | |
config = DevConfig() |
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
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