How to use the anyconfig.compat.OrderedDict function in anyconfig

To help you get started, we’ve selected a few anyconfig examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github ssato / python-anyconfig / tests / backend / configobj.py View on Github external
# sub-sub-section is *in* 'sub-section'
        # which is in 'section 1'
        'keyword 7' = 'value 8'

[section 2]    # an inline comment
keyword8 = "value 9"
keyword9 = value10     # an inline comment
# The 'final_comment'
# Which also may be several lines
"""

_ML_0 = """A multiline value,
that spans more than one line :-)
The line breaks are included in the value."""

CNF_0 = ODict((('keyword1', 'value1'),
               ('keyword 2', 'value 2'),
               ('section 1',
                ODict((('keyword 3', 'value 3'),
                       ('keyword 4', ['value4', 'value 5', 'value 6']),
                       ('sub-section',
                        ODict((('keyword 5', 'value 7'),
                               ('keyword 6', _ML_0),
                               ('sub-sub-section',
                                ODict((('keyword 7', 'value 8'), ))))))))),
               ('section 2',
                ODict((('keyword8', 'value 9'), ('keyword9', 'value10'))))))


class HasParserTrait(TBC.HasParserTrait):

    psr = TT.Parser()
github ssato / python-anyconfig / tests / backend / toml.py View on Github external
from __future__ import absolute_import

try:
    import anyconfig.backend.toml as TT
except ImportError:
    import tests.common
    raise tests.common.skip_test()

import tests.backend.common as TBC

from anyconfig.compat import OrderedDict as ODict


_DOB = TT.toml.loads("dob = 1979-05-27T07:32:00Z")['dob']

CNF = ODict((('title', 'TOML Example'),
             ('owner',
              ODict((('name', 'Tom Preston-Werner'),
                     ('dob', _DOB)))),
             ('database',
              ODict((('server', '192.168.1.1'),
                     ('ports', [8001, 8001, 8002]),
                     ('connection_max', 5000),
                     ('enabled', True)))),
             ('servers',
              ODict((('alpha',
                      ODict((('ip', '10.0.0.1'), ('dc', 'eqdc10')))),
                     ('beta',
                      ODict((('ip', '10.0.0.2'), ('dc', 'eqdc10'))))))),
             ('clients',
              ODict((('data', [['gamma', 'delta'], [1, 2]]),
                     ('hosts', ['alpha', 'omega']))))))
github ssato / python-anyconfig / tests / backend / common.py View on Github external
import tests.common
import anyconfig.ioinfo

from anyconfig.compat import OrderedDict
from tests.common import to_bytes as _bytes


CNF_0 = OrderedDict((("DEFAULT", OrderedDict((("a", "0"), ("b", "bbb"),
                                              ("c", "5")))),
                     ("sect0", OrderedDict((("a", "0"), ("b", "bbb"),
                                            ("c", "5"),
                                            ("d", "x,y,z"))))))
CNF_1 = copy.deepcopy(CNF_0)
CNF_1["sect0"]["d"] = CNF_1["sect0"]["d"].split()

CNF_2 = OrderedDict((("a", 0.1), ("b", _bytes("bbb")),
                     ("sect0", OrderedDict((("c", [_bytes("x"), _bytes("y"),
                                                   _bytes("z")]), )))))


def read_from_res(filename):
    return open(os.path.join(tests.common.resdir(), filename)).read()


class MyDict(dict):
    pass


class HasParserTrait(object):

    psr = None  # Must be a parser instance.
    cnf_s = None  # Do.
github ssato / python-anyconfig / tests / common.py View on Github external
def test_dicts_equal(self):
        dic0 = {'a': 1}
        dic1 = OrderedDict((('a', [1, 2, 3]),
                            ('b', OrderedDict((('c', "CCC"), )))))
        dic2 = dic1.copy()
        dic2["b"] = None

        dic3 = OrderedDict((('b', OrderedDict((('c', "CCC"), ))),
                            ('a', [1, 2, 3])))

        self.assertTrue(dicts_equal({}, {}))
        self.assertTrue(dicts_equal(dic0, dic0))
        self.assertTrue(dicts_equal(dic1, dic1))
        self.assertTrue(dicts_equal(dic2, dic2))
        self.assertTrue(dicts_equal(dic1, dic3))

        self.assertFalse(dicts_equal(dic0, {}))
        self.assertFalse(dicts_equal(dic0, dic1))
        self.assertFalse(dicts_equal(dic1, dic2))
        self.assertFalse(dicts_equal(dic1, dic3, ordered=True))
github ssato / python-anyconfig / tests / dicts.py View on Github external
# maybe the error message depends on python version.
        # self.assertEqual(msg, 'list index out of range')

        (val, msg) = TT.get(dic, "/a/b/d/-")
        self.assertTrue(val is None)
        self.assertTrue(bool(msg))
        # Likewise.
        # self.assertEqual(msg, 'list indices must be integers...')


class Test_10_update_with_replace(unittest.TestCase):

    ac_merge = TT.MS_REPLACE

    dic = OrderedDict((("a", 1), ("b", [1, 3]), ("c", "abc"), ("f", None)))
    other = OrderedDict((("a", 2), ("b", [0, 1]),
                         ("c", OrderedDict((("d", "d"), ("e", 1)))),
                         ("d", "d")))

    def assert_dicts_equal(self, dic, upd, ref):
        if not is_dict_like(upd):
            upd = OrderedDict(upd)

        self.assertTrue(all(dic[k] == upd[k] for k in upd.keys()))
        self.assertTrue(all(dic[k] == ref[k] for k in ref.keys()
                            if k not in upd))

    def assert_updated(self, other):
        dic = copy.deepcopy(self.dic)
        TT.merge(dic, other, ac_merge=self.ac_merge)
        self.assert_dicts_equal(dic, other, self.dic)
github ssato / python-anyconfig / tests / api.py View on Github external
CNF_XML_1 = {'config': {'@attrs': {'name': 'foo'},
                        'a': '0',
                        'b': {'@attrs': {'id': 'b0'}, '@text': 'bbb'},
                        'c': None,
                        'sect0': {'d': 'x, y, z'},
                        'list1': [{'item': '0'}, {'item': '1'},
                                  {'item': '2'}],
                        'list2': {'@attrs': {'id': 'list2'},
                                  '@children': [{'item': 'i'},
                                                {'item': 'j'}]}}}

NULL_CNTNR = TT.anyconfig.dicts.convert_to({})


class MyODict(anyconfig.compat.OrderedDict):
    pass


def _is_file_object(obj):
    try:
        return isinstance(obj, file)
    except NameError:  # python 3.x
        return isinstance(obj, io.IOBase)


class Test_10_find(unittest.TestCase):

    psrs = anyconfig.backends.Parsers().list()

    def _assert_isinstances(self, obj, clss, msg=False):
        self.assertTrue(any(isinstance(obj, cls) for cls in clss),
github ssato / python-anyconfig / tests / dicts.py View on Github external
def assert_dicts_equal(self, dic, upd, ref):
        if not is_dict_like(upd):
            upd = OrderedDict(upd)

        self.assertTrue(all(dic[k] == upd[k] for k in upd.keys()
                            if k not in ref))
        self.assertTrue(all(dic[k] == ref[k] for k in ref.keys()))
github ssato / python-anyconfig / tests / common.py View on Github external
def test_dicts_equal(self):
        dic0 = {'a': 1}
        dic1 = OrderedDict((('a', [1, 2, 3]),
                            ('b', OrderedDict((('c', "CCC"), )))))
        dic2 = dic1.copy()
        dic2["b"] = None

        dic3 = OrderedDict((('b', OrderedDict((('c', "CCC"), ))),
                            ('a', [1, 2, 3])))

        self.assertTrue(dicts_equal({}, {}))
        self.assertTrue(dicts_equal(dic0, dic0))
        self.assertTrue(dicts_equal(dic1, dic1))
        self.assertTrue(dicts_equal(dic2, dic2))
        self.assertTrue(dicts_equal(dic1, dic3))

        self.assertFalse(dicts_equal(dic0, {}))
        self.assertFalse(dicts_equal(dic0, dic1))
        self.assertFalse(dicts_equal(dic1, dic2))
github ssato / python-anyconfig / anyconfig / mergeabledict.py View on Github external
True
    >>> all(md0[k] == ref[k] for k in ref.keys() if k not in upd)
    True
    >>> all(md1[k] == upd[k] for k in upd.keys())
    True
    >>> all(md1[k] == ref[k] for k in ref.keys() if k not in upd)
    True
    >>> list(md0.keys())
    ['a', 'b', 'c', 'f', 'd']
    >>> list(md1.keys())
    ['a', 'b', 'c', 'f', 'd']
    """
    pass


class UpdateWoReplaceOrderedDict(UpdateWoReplaceDict, OrderedDict):
    """
    Similar to UpdateWoReplaceDict but keep keys' order like OrderedDict.

    >>> od0 = OrderedDict((("a", 1), ("b", [1, 3]), ("c", "abc"),
    ...                    ("f", None)))
    >>> md0 = UpdateWoReplaceOrderedDict(od0)
    >>> ref = md0.copy()
    >>> md1 = UpdateWoReplaceOrderedDict((("a", 2), ("b", [0, 1]),
    ...                                   ("c", "xyz"), ("d", None)))
    >>> md0.update(md1)
    >>> all(md0[k] == md1[k] for k in md1.keys() if k not in ref)
    True
    >>> all(md0[k] == ref[k] for k in ref.keys())
    True
    >>> list(md0.keys())
    ['a', 'b', 'c', 'f', 'd']