def dict_path_enter(path, key, value): if isinstance(value, str): return value, False elif isinstance(value, (tomlkit.items.Table, tomlkit.items.InlineTable)): return value.__class__(tomlkit.container.Container(), value.trivia, False), ItemsView(value) elif isinstance(value, (Mapping, dict)): return value.__class__(), ItemsView(value) elif isinstance(value, tomlkit.items.Array): return value.__class__([], value.trivia), enumerate(value) elif isinstance(value, (Sequence, list)): return value.__class__(), enumerate(value) elif isinstance(value, (Set, set)): return value.__class__(), enumerate(value) else: return value, False
def test_views(bi, data): """Optimized view APIs should be equivalent to using the corresponding MappingViews from :mod:`collections.abc`.""" for check, oracle in (bi.keys(), KeysView(bi)), (bi.values(), ValuesView(bi)), (bi.items(), ItemsView(bi)): # 0-arity methods: __len__, __iter__ assert check.__len__() == oracle.__len__() assert list(check.__iter__()) == list(oracle.__iter__()) # 1-arity methods: __contains__ arg = data.draw( st.PAIRS if isinstance(oracle, ItemsView) else st.ATOMS) assert check.__contains__(arg) == oracle.__contains__(arg) # Methods of set-like views if isinstance(oracle, ValuesView): continue arg = data.draw(st.KEYSVIEW_SET_OP_ARGS if isinstance( oracle, KeysView) else st.ITEMSVIEW_SET_OP_ARGS) for so in _SET_OPS: try: expect = so(oracle, arg) except TypeError: with pytest.raises(TypeError): so(check, arg) else: check_ = so(check, arg) assert check_ == expect, (check, so, arg) try: expect = so(arg, oracle) except TypeError: with pytest.raises(TypeError): so(arg, check) else: check_ = so(arg, check) assert check_ == expect, (check, so, arg)
def default_enter_fn(parents, key, value): # Responsible for getting the items to be traversed/updated in visit and exit respectively if isinstance(value, basestring): return value, False elif isinstance(value, Mapping): return value.__class__(), ItemsView(value) elif isinstance(value, Sequence): return value.__class__(), enumerate(value) elif isinstance(value, Set): return value.__class__(), enumerate(value) else: return value, False
def items(self): """Set-like object providing a view of index items. >>> index = Index() >>> index.update({'a': 1, 'b': 2, 'c': 3}) >>> items_view = index.items() >>> ('b', 2) in items_view True :return: items view """ return ItemsView(self)
def default_enter(path, key, value): # print('enter(%r, %r)' % (key, value)) if isinstance(value, basestring): return value, False elif isinstance(value, Mapping): return value.__class__(), ItemsView(value) elif isinstance(value, Sequence): return value.__class__(), enumerate(value) elif isinstance(value, Set): return value.__class__(), enumerate(value) else: # files, strings, other iterables, and scalars are not # traversed return value, False
def viewitems(self): """Set-like object providing a view of index items. >>> index = Index('/tmp/diskcache/index') >>> index.clear() >>> index.update({'a': 1, 'b': 2, 'c': 3}) >>> items_view = index.viewitems() >>> ('b', 2) in items_view True :return: items view """ return ItemsView(self)
def default_enter(path, key, value): # print('enter(%r, %r)' % (key, value)) try: iter(value) except TypeError: # print 'no enter' return value, False if isinstance(value, basestring): return value, False elif isinstance(value, Mapping): # print 'enter mapping ', type(value) return value.__class__(), ItemsView(value) elif isinstance(value, Sequence): # print 'enter sequence ', type(value) return value.__class__(), enumerate(value) elif isinstance(value, Set): # print 'enter set ', type(value) return value.__class__(), enumerate(value) # print 'no enter ', type(value) return value, False
def items(self): from collections.abc import ItemsView return ItemsView(self)
def _abc_itemsview_register(view_cls): ItemsView.register(view_cls)
def items(self): return ItemsView(self)
def viewitems(self): "OMD.viewitems() -> a set-like object providing a view on OMD's items" return ItemsView(self)
def mapping_items(self) -> tp.ItemsView[str, int]: """Get an iterator over the mapping items.""" return ItemsView(self.__hash_to_id)
def items(self, max_len: Optional[int] = None) -> Iterator: # type: ignore return Iterator(ItemsView(self), max_len=max_len)
def items(self, order=None): """Return an ItemsView in the given order, or the instance's ``order``, or the class' ``default_order``""" return ItemsView(self._to_ordered_dict(order or self.order))
def __hash__(self): return ItemsView(self._mapping)._hash() # pylint: disable=protected-access
def items(self): "D.items() -> a set-like object providing a view on D's items" return ItemsView(self)
def __hash__(self): return ItemsView(self._mapping)._hash()
def items(self, fallback=True): # pylint: disable=arguments-differ return ItemsView(self if fallback else self._without_fallback())
assert theEmptySet is EmptySet() assert theEmptySequence is EmptySequence() assert theEmptyThree is EmptyThree() if 1: #tmp assert not issubclass(EmptyMapping, Mapping) #MappingView.register(EmptyMapping) KeysView.register(EmptySet) ItemsView.register(EmptySet) ValuesView.register(EmptyCollection) assert issubclass(EmptySet, KeysView) assert issubclass(EmptySet, ItemsView) assert issubclass(EmptySet, Set) assert issubclass(EmptySet, MappingView) assert issubclass(EmptyCollection, ValuesView) assert issubclass(EmptyCollection, Collection) assert issubclass(EmptyCollection, MappingView) Mapping.register(EmptyMapping) Set.register(EmptySet) Sequence.register(EmptySequence) assert issubclass(EmptyMapping, Mapping)
def __json__(self): return ItemsView(self.data)
def __hash__(self): # lgtm [py/equals-hash-mismatch] """The hash of this bidict as determined by its items.""" if getattr(self, '_hash', None) is None: # pylint: disable=protected-access,attribute-defined-outside-init self._hash = ItemsView(self)._hash() return self._hash