Skip to content

Instantly share code, notes, and snippets.

@svfat
Created May 28, 2015 07:58
Show Gist options
  • Save svfat/2cf263613f4d401671d7 to your computer and use it in GitHub Desktop.
Save svfat/2cf263613f4d401671d7 to your computer and use it in GitHub Desktop.
Config abstraction
# coding: utf-8
"""
Abstraction above configuration data:
Used to load any data in dictionary format into object,
as class properties.
Example:
data = {'threads':30, 'name':'Stan', 'something':[1, 'dd', 13]}
cfg = config.AbstractConfig(data)
print cfg.threads
print cfg.name
print cfg.something
"""
class AbstractConfig(object):
"""
Used to load and represent config
"""
def __init__(self, data, ignore_errors=False):
super(AbstractConfig, self).__init__()
self.__load(data, ignore_errors)
def __load(self, data, ignore_errors=False):
"""
Load dict into object, dict key-value pairs will be
represented as properties of object
"""
for k, v in data.iteritems():
if not getattr(self, k, None):
setattr(self, k, v)
else:
if not ignore_errors:
raise AttributeError('Attribute "%s" already exists' % k)
else:
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment