def test_is_sequence_and_is_dict(): """Check sorted dict with is_sequence() and is_dict().""" assert common.is_dictionary( SortedDict(str, int, { 'a': 3, 'b': -1, 'c': 12 })) assert common.is_dictionary(SortedDict(int, float), ) assert not common.is_sequence(SortedDict(str, str))
def test_is_dictionary(): """ Test dictionary type checking function. """ assert common.is_dictionary({'a': 1, 'b': 2}) assert not common.is_dictionary((1, 2, 3)) assert not common.is_dictionary([1, 2, 3]) assert not common.is_dictionary({1, 2, 3}) assert not common.is_dictionary(()) assert not common.is_dictionary([]) assert not common.is_dictionary(set()) assert not common.is_dictionary('i am a string')
def __init__(self, key_type, val_type, kwargs=None): """ Initialize a typed SortedDict. :param key_type: expected type for keys. :param val_type: expected type for values. :param kwargs: (optional) dictionary-like object: initial values for sorted dict. """ self.__val_type = val_type self.__keys = SortedSet(key_type) self.__couples = {} if kwargs is not None: assert is_dictionary(kwargs) for key, value in kwargs.items(): self.put(key, value)
def fill(self, dct): """ Add given dict to this sorted dict. """ if dct: assert is_dictionary(dct) for key, value in dct.items(): self.put(key, value)
def validate(self, element): if not is_dictionary(element): raise exceptions.TypeException('dictionary', type(element)) for key, value in element.items(): self.key_type.validate(key) self.val_type.validate(value)
def test_common_utils_with_sorted_set(): """Check sorted set with is_sequence() and is_dictionary().""" assert common.is_sequence(SortedSet(int, (1, 2, 3))) assert common.is_sequence(SortedSet(int)) assert not common.is_dictionary(SortedSet(int, (1, 2, 3))) assert not common.is_dictionary(SortedSet(int))