我们从Python开源项目中,提取了以下14个代码示例,用于说明如何使用collections.ItemsView()。
def test_MutableMapping_subclass(self): # Test issue 9214 mymap = UserDict() mymap['red'] = 5 self.assertIsInstance(mymap.keys(), Set) self.assertIsInstance(mymap.keys(), KeysView) self.assertIsInstance(mymap.items(), Set) self.assertIsInstance(mymap.items(), ItemsView) mymap = UserDict() mymap['red'] = 5 z = mymap.keys() | {'orange'} self.assertIsInstance(z, set) list(z) mymap['blue'] = 7 # Shouldn't affect 'z' self.assertEqual(sorted(z), ['orange', 'red']) mymap = UserDict() mymap['red'] = 5 z = mymap.items() | {('orange', 3)} self.assertIsInstance(z, set) list(z) mymap['blue'] = 7 # Shouldn't affect 'z' self.assertEqual(sorted(z), [('orange', 3), ('red', 5)])
def test_abc_registry(self): d = dict(a=1) self.assertIsInstance(d.viewkeys(), collections.KeysView) self.assertIsInstance(d.viewkeys(), collections.MappingView) self.assertIsInstance(d.viewkeys(), collections.Set) self.assertIsInstance(d.viewkeys(), collections.Sized) self.assertIsInstance(d.viewkeys(), collections.Iterable) self.assertIsInstance(d.viewkeys(), collections.Container) self.assertIsInstance(d.viewvalues(), collections.ValuesView) self.assertIsInstance(d.viewvalues(), collections.MappingView) self.assertIsInstance(d.viewvalues(), collections.Sized) self.assertIsInstance(d.viewitems(), collections.ItemsView) self.assertIsInstance(d.viewitems(), collections.MappingView) self.assertIsInstance(d.viewitems(), collections.Set) self.assertIsInstance(d.viewitems(), collections.Sized) self.assertIsInstance(d.viewitems(), collections.Iterable) self.assertIsInstance(d.viewitems(), collections.Container)
def items(self): return ItemsView(self)
def items(self): "D.items() -> a set-like object providing a view on D's items" return ItemsView(self)
def sort(value): """ The filter to return the sorted order of a list """ if isinstance(value, (ItemsView, list)): return sorted(value) return value
def items(self): """ Get all the items of the configuration (key/value pairs). :return tuple: The items of the configuration. """ return ItemsView(self)
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 items(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.items() >>> ('b', 2) in items_view True :return: items view """ return ItemsView(self)
def viewitems(self): return collections.ItemsView(self)
def __eq__(self, other): if not isinstance(other, (Message, Mapping)): return NotImplemented return dict(ItemsView(self)) == dict(ItemsView(other))
def items(message: Message) -> ItemsView: return ItemsView(message)
def update(self, tablename, update_args, query_args=None): """Very simple "generic" update helper. will generate the update statement, converting the `update_args` dict into "key = value, key = value" statements, and converting the `query_args` dict into "key = value AND key = value" statements. string values will be wrapped in 'quotes', while other types will be left as their python representation. """ if not self.transaction: raise DatabaseError("No transaction in progress") query = "UPDATE {} SET ".format(tablename) arglist = [] qnum = 1 if isinstance(update_args, dict): update_args = update_args.items() if isinstance(update_args, (list, tuple, ItemsView)): setstmts = [] for k, v in update_args: setstmts.append("{} = ${}".format(k, qnum)) qnum += 1 arglist.append(v) query += ', '.join(setstmts) else: raise DatabaseError("expected dict or list for update_args") if isinstance(query_args, dict): query_args = query_args.items() if isinstance(query_args, (list, tuple, ItemsView)): query += " WHERE " wherestmts = [] # TODO: support OR somehow? for k, v in query_args: wherestmts.append("{} = ${}".format(k, qnum)) qnum += 1 arglist.append(v) query += ' AND '.join(wherestmts) elif query_args is not None: raise DatabaseError("expected dict or list or None for query_args") resp = await self.connection.execute(query, *arglist) if resp and resp[0].startswith("ERROR:"): raise DatabaseError(resp) return resp