def dict_sorted(origin: dict, is_reverse: bool = False) -> dict:
    ''' Sort dictionary.
    '''
    return dict(
        sorted(assertion.is_dict(origin).items(),
               key=lambda x: x[0],
               reverse=assertion.is_bool(is_reverse)))
 def test_is_dict(self):
     data = [
             # (val, expect)
             (True, {"test": "1"}, {"test": "1"},),
             (False, ["test", "1"], ["test", "1"],),
             ]
     validate_with_fail(self, "is_dict",
         lambda v,expect: self.assertEqual(assertion.is_dict(v), expect), data)
def string_replaced_by_tag(src: str, tags: dict, prefix: str='$') -> str:
    ''' Replace the target word in a string by tags.
    '''
    tmp = assertion.is_str(src)
    for k, v in assertion.is_dict(tags).items():
        if assertion.is_str(prefix) in tmp:
            tmp = re.sub(r'\{}{}'.format(prefix, k), v, tmp)
        else:
            return tmp
    return tmp
示例#4
0
 def set_from_asset(self, asset: dict) -> None:
     for elm in self.__ASSET_ELEMENTS__:
         if elm.upper() in assertion.is_dict(asset):
             if elm.lower() == 'persons':
                 self.set_persons(asset[elm.upper()])
             elif elm.lower() == 'stages':
                 self.set_stages(asset[elm.upper()])
             elif elm.lower() == 'days':
                 self.set_days(asset[elm.upper()])
             elif elm.lower() == 'times':
                 self.set_times(asset[elm.upper()])
             elif elm.lower() == 'items':
                 self.set_items(asset[elm.upper()])
             elif elm.lower() == 'words':
                 self.set_words(asset[elm.upper()])
             elif elm.lower() == 'rubis':
                 self.set_rubis(asset[elm.upper()])
             else:
                 msg = f'Asset name mismatch!: {elm.upper()}'
                 LOG.error(msg)
def combine_dict(a: dict, b: dict) -> dict:
    ''' Combine one dictionary from two dictionaries.
    '''
    return {**assertion.is_dict(a), **assertion.is_dict(b)}