3

構成ファイルのデータに属性を使用できるようにするソリューションを探しています。

私はこのようなことができるようにしたいと思います:

config = Config('config.ini')
print config.section1.user
print config.section2.password

ConfigParser を使用すると、次config['section1']['user']のようなことができることは知っていますが、それはあまりにも醜いです。もっとうまくできないでしょうか?

このソリューションは、Python 2.5 以降で動作する必要があります。

4

4 に答える 4

2

それは醜くはありません。ドット表記は、別のカスタム クラスのオブジェクト内におそらく何らかのカスタム クラスのオブジェクトがあることを意味します。より実現可能な方法は、辞書 (ブラケット表記を使用する) を使用することです。

しかし、あなたが主張するなら、おそらく次のようにコードを翻訳することができます:

def config2object(config):
    """
    Convert dictionary into instance allowing access to dictionary keys using
    dot notation (attributes).
    """
    class ConfigObject(dict):
        """
        Represents configuration options' group, works like a dict
        """
        def __init__(self, *args, **kwargs):
            dict.__init__(self, *args, **kwargs)
        def __getattr__(self, name):
            return self[name]
        def __setattr__(self, name, val):
            self[name] = val
    if isinstance(config, dict):
        result = ConfigObject()
        for key in config:
            result[key] = config2object(config[key])
        return result
    else:
        return config

そして、テストは期待される結果を示しています。

>>> c1 = {
    'conf1': {
        'key1': 'aaa',
        'key2': 12321,
        'key3': False,
        },
    'conf2': 'bbbb',
    }
>>> c1
{'conf2': 'bbbb', 'conf1': {'key3': False, 'key2': 12321, 'key1': 'aaa'}}
>>> c2 = config2object(c1)
>>> c2.conf1
{'key3': False, 'key2': 12321, 'key1': 'aaa'}
>>> c2.conf1.key1
'aaa'
>>> c2.conf1.key3
False
>>> c2.conf2
'bbbb'

編集: Sven MarnachConfig('config.ini')は、カスタム クラスのインスタンスであると指摘しました。これはディクショナリではなく、非常に便利なカスタム メソッドがいくつかありますが、一部の構成オプションにアクセスできなくなる可能性があります (名前の競合がある場合)。したがって、推奨されるアプローチは、前述のソリューションを使用するのではなく、ブラケット表記を使用して構成オプションにアクセスすることです。

于 2012-03-19T16:42:14.210 に答える
1

私もドット表記を使用して、ConfigParser が構成ファイルから読み取った属性にアクセスしたいと考えていました。( github で利用可能)。

ConfigParser を拡張する私の試みは次のとおりです。

from ConfigParser import ConfigParser as BaseClass

SPACE = " "
UNDERSCORE = "_"


def internal_name(name, needle=SPACE, replacement=UNDERSCORE):
    return name.replace(needle, replacement)


def reverse_name_internalization(name):
    return internal_name(name, needle=UNDERSCORE, replacement=SPACE)


class DotNotationConfigParser(BaseClass, object):

    def __init__(self, coersion_map=None, *args, **kwargs):
        super(DotNotationConfigParser, self).__init__(*args, **kwargs)

        self.optionxform = internal_name
        self.section_attr = None

    def get_internalized_section(self, section):
        if self.has_section(section):
            return internal_name(section)

    def __set_section_obj(self, internalized_section):
        if self.has_section(internalized_section):
            section = internalized_section
        else:
            section = reverse_name_internalization(internalized_section)

        if self.get_internalized_section(section):
            # set an attr to an object instance with section items
            obj = type('', (), dict(self.items(section)))()
            setattr(self, internalized_section, obj)

    def __getattr__(self, attr):
        try:
            return super(DotNotationConfigParser, self).__getattribute__(attr)
        except AttributeError:
            section = attr
            self.__set_section_obj(section)
            return super(DotNotationConfigParser, self).__getattribute__(attr)


try:
    from cStringIO import StringIO
except ImportError:
    from StringIO import StringIO
configuration_file = """
[section 1]
foo = the foo value
bar = the bar value
[section 2]
index = 3
repeat = False
[section_n]
string = This is some text.
ip = 10.0.1.1
"""
configuration_file = StringIO(configuration_file)

parser = DotNotationConfigParser()
parser.readfp(configuration_file)

assert parser.section_1.foo == 'the foo value'
assert parser.section_1.bar == 'the bar value'
assert type(parser.section_2.index) is not int
for section_name in ('section_1', 'section_2', 'section_n'):
    section = getattr(parser, section_name)
    options = [option for option in dir(section)
               if not option.startswith('__')]
    for option in options:
        print section_name, ": ", getattr(section, option)

print "dot notation", parser.section_1.foo
于 2015-05-23T23:43:04.603 に答える