Python collections.ItemsView() Examples

The following are code examples for showing how to use collections.ItemsView(). They are extracted from open source Python projects. You can vote up the examples you like or vote down the ones you don't like. You can also save this page to your account.

Example 1
Project: oil   Author: oilshell   File: test_dictviews.py    (license) View Source Project 6 votes vote down vote up
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) 
Example 2
Project: python2-tracer   Author: extremecoders-re   File: test_dictviews.py    (license) View Source Project 6 votes vote down vote up
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) 
Example 3
Project: Python_Master-the-Art-of-Design-Patterns   Author: PacktPublishing   File: 6_16_dictsorted.py    (license) View Source Project 6 votes vote down vote up
def items(self):
        return ItemsView(self) 
Example 4
Project: xarray-simlab   Author: benbovy   File: utils.py    (BSD 3-Clause "New" or "Revised" License) View Source Project 5 votes vote down vote up
def items(self):
        "D.items() -> a set-like object providing a view on D's items"
        return ItemsView(self) 
Example 5
Project: KidTasks   Author: jima80525   File: tasks_extras.py    (license) View Source Project 5 votes vote down vote up
def sort(value):
    """ The filter to return the sorted order of a list """
    if isinstance(value, (ItemsView, list)):
        return sorted(value)
    return value 
Example 6
Project: central   Author: viniciuschiele   File: core.py    (license) View Source Project 5 votes vote down vote up
def items(self):
        """
        Get all the items of the configuration (key/value pairs).
        :return tuple: The items of the configuration.
        """
        return ItemsView(self) 
Example 7
Project: python-diskcache   Author: grantjenks   File: persistent.py    (license) View Source Project 5 votes vote down vote up
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) 
Example 8
Project: python-diskcache   Author: grantjenks   File: persistent.py    (license) View Source Project 5 votes vote down vote up
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) 
Example 9
Project: zippy   Author: securesystemslab   File: test_collections.py    (license) View Source Project 5 votes vote down vote up
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)]) 
Example 10
Project: Deploy_XXNET_Server   Author: jzp820927   File: request_environment.py    (license) View Source Project 5 votes vote down vote up
def viewitems(self):
    return collections.ItemsView(self) 
Example 11
Project: venom   Author: biosustain   File: message.py    (license) View Source Project 5 votes vote down vote up
def __eq__(self, other):
        if not isinstance(other, (Message, Mapping)):
            return NotImplemented
        return dict(ItemsView(self)) == dict(ItemsView(other)) 
Example 12
Project: venom   Author: biosustain   File: message.py    (license) View Source Project 5 votes vote down vote up
def items(message: Message) -> ItemsView:
    return ItemsView(message) 
Example 13
Project: Python-Journey-from-Novice-to-Expert   Author: PacktPublishing   File: 6_16_dictsorted.py    (license) View Source Project 5 votes vote down vote up
def items(self):
        return ItemsView(self) 
Example 14
Project: toshi-services-lib   Author: toshiapp   File: database.py    (license) View Source Project 4 votes vote down vote up
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