示例#1
0
    def test_dictofstrings(self):
        """Test dict of strings"""
        # test default separators
        self.assertEqual(DictOfStrings.SEPARATOR_DICT, ';')
        self.assertEqual(DictOfStrings.SEPARATOR_KEY_VALUE, ':')

        # start with simple one because the conversion to string is ordered
        dest = {'a': 'b'}
        txt = DictOfStrings.SEPARATOR_KEY_VALUE.join(dest.items()[0])

        res = DictOfStrings(txt)
        self.assertEqual(res, dest)
        self.assertEqual(str(res), txt)

        # test with auto convert list to dict
        class Tmp(DictOfStrings):
            KEYLESS_ENTRIES = ['first']
            __str__ = DictOfStrings.__str__

        dest2 = {'first': 'first_value'}
        dest2.update(dest)
        txt2 = DictOfStrings.SEPARATOR_DICT.join([dest2['first'], txt])
        res = Tmp(txt2)
        self.assertEqual(res, dest2)
        self.assertEqual(str(res), txt2)

        # retest with space separated separator
        txt2 = txt.replace(DictOfStrings.SEPARATOR_KEY_VALUE,
                           DictOfStrings.SEPARATOR_KEY_VALUE + ' ')
        res = ListOfStrings(
            txt2.replace(DictOfStrings.SEPARATOR_DICT,
                         DictOfStrings.SEPARATOR_DICT + ' '))

        # more complex one
        dest = {'a': 'b', 'c': 'd'}
        tmp = [
            DictOfStrings.SEPARATOR_KEY_VALUE.join(item)
            for item in dest.items()
        ]
        txt = DictOfStrings.SEPARATOR_DICT.join(tmp)

        res = DictOfStrings(txt)
        self.assertEqual(res, dest)

        # test ALLOWED_KEYS
        class Tmp(DictOfStrings):
            ALLOWED_KEYS = ['x']

        self.assertErrorRegex(ValueError, "allowed", Tmp, txt)
 def _from_string(self, txt):
     """Convert from string
         # shorthand
         filename;level:<int>;dest:<string> -> {'filename': filename, 'level': level, 'dest': dest}
         # full dict notation
         filename:filename;level:<int>;dest:<string> -> {'filename': filename, 'level': level, 'dest': dest}
     """
     res = DictOfStrings._from_string(self, txt)
     if 'level' in res:
         res['level'] = int(res['level'])
     return res
示例#3
0
 def _from_string(self, txt):
     """Convert from string
         # shorthand
         filename;level:<int>;dest:<string> -> {'filename': filename, 'level': level, 'dest': dest}
         # full dict notation
         filename:filename;level:<int>;dest:<string> -> {'filename': filename, 'level': level, 'dest': dest}
     """
     res = DictOfStrings._from_string(self, txt)
     if 'level' in res:
         res['level'] = int(res['level'])
     return res