Example #1
0
    def test_iter(self):
        book = Book(title="Foo",
                    isbn='abc123',
                    rrp=123.45,
                    num_pages=10,
                    fiction=True)
        target = adapters.ResourceAdapter(book)

        actual = list((f.name, str(v)) for f, v in field_iter_items(target))
        assert [('title', 'Foo'), ('isbn', 'abc123'), ('num_pages', '10'),
                ('rrp', '123.45'), ('fiction', 'True'), ('genre', 'None'),
                ('published', '[]'), ('authors', '[]'),
                ('publisher', 'None')] == actual
Example #2
0
    def test_iter(self):
        book = Book(title="Foo", rrp=123.45, num_pages=10, fiction=True)
        target = adapters.ResourceAdapter(book)

        actual = list((f.name, str(v)) for f, v in field_iter_items(target))
        self.assertListEqual([
            ('title', 'Foo'),
            ('num_pages', '10'),
            ('rrp', '123.45'),
            ('fiction', 'True'),
            ('genre', 'None'),
            ('published', '[]'),
            ('authors', '[]'),
            ('publisher', 'None')], actual)
Example #3
0
    def to_dict(self, include_virtual=True):
        """
        Convert this resource into a `dict` of field_name/value pairs.

        .. note::
            This method is not recursive, it only operates on this single resource, any sub resources are returned as
            is. The use case that prompted the creation of this method is within codecs when a resource must be
            converted into a type that can be serialised, these codecs then operate recursively on the returned `dict`.

        :param include_virtual: Include virtual fields when generating `dict`.

        """
        fields = self._meta.all_fields if include_virtual else self._meta.fields
        return dict((f.name, v) for f, v in field_iter_items(self, fields))
Example #4
0
    def to_dict(self, include_virtual=True):
        """
        Convert this resource into a `dict` of field_name/value pairs.

        .. note::
            This method is not recursive, it only operates on this single resource, any sub resources are returned as
            is. The use case that prompted the creation of this method is within codecs when a resource must be
            converted into a type that can be serialised, these codecs then operate recursively on the returned `dict`.

        :param include_virtual: Include virtual fields when generating `dict`.

        """
        fields = self._meta.all_fields if include_virtual else self._meta.fields
        return dict((f.name, v) for f, v in field_iter_items(self, fields))
Example #5
0
    def test_iter(self):
        book = Book(title="Foo", isbn="abc123", rrp=123.45, num_pages=10, fiction=True)
        target = adapters.ResourceAdapter(book)

        actual = list((f.name, str(v)) for f, v in field_iter_items(target))
        assert [
            ("title", "Foo"),
            ("isbn", "abc123"),
            ("num_pages", "10"),
            ("rrp", "123.45"),
            ("fiction", "True"),
            ("genre", "None"),
            ("published", "[]"),
            ("authors", "[]"),
            ("publisher", "None"),
        ] == actual
Example #6
0
 def to_dict(self, include_virtual=True):
     """
     Convert this resource into a dict
     """
     fields = self._meta.all_fields if include_virtual else self._meta.fields
     return dict((f.name, v) for f, v in field_iter_items(self, fields))
Example #7
0
 def to_dict(self, include_virtual=True):
     """
     Convert this resource into a dict
     """
     fields = self._meta.all_fields if include_virtual else self._meta.fields
     return {f.name: v for f, v in field_iter_items(self, fields)}
Example #8
0
        def generate():
            yield '\t'.join(f.name for f in field_iter(User)) + '\n'

            for user in USERS:
                yield '\t'.join(str(v) for _, v in field_iter_items(user)) + '\n'