itemadapter
The ItemAdapter class is a wrapper for data container objects, providing a
common interface to handle objects of different types in an uniform manner,
regardless of their underlying implementation.
This package started as an initiative to support dataclass objects as items
[1]. It was extracted out to a standalone package
in order to allow it to be used independently.
Currently supported types are:
- Classes that implement the
MutableMappinginterface, including but not limited to: dataclass-based classesattrs-based classes
Requirements
- Python 3.5+
dataclasses(stdlib in Python 3.7+, or its backport in Python 3.6): optional, needed to interact withdataclass-based itemsattrs: optional, needed to interact withattrs-based items
API
ItemAdapter class
class itemadapter.adapter.ItemAdapter(item: Any)
ItemAdapter implements the
MutableMapping interface,
providing a dict-like API to manipulate data for the object it wraps
(which is modified in-place).
Two additional methods are available:
get_field_meta(field_name: str) -> MappingProxyType
Return a MappingProxyType
object with metadata about the given field, or raise TypeError if the item class does not
support field metadata.
The returned value is taken from the following sources, depending on the item type:
dataclasses.field.metadatafordataclass-based itemsattr.Attribute.metadataforattrs-based itemsscrapy.item.Fieldforscrapy.item.Items
field_names() -> List[str]
Return a list with the names of all the defined fields for the item.
is_item function
itemadapter.utils.is_item(obj: Any) -> bool
Return True if the given object belongs to one of the supported types,
False otherwise.
Metadata support
scrapy.item.Item, dataclass and attrs objects allow the inclusion of
arbitrary field metadata, which can be retrieved with the
ItemAdapter.get_field_meta method. The definition procedure depends on the
underlying type.
scrapy.item.Item objects
>>> from scrapy.item import Item, Field
>>> from itemadapter import ItemAdapter
>>> class InventoryItem(Item):
... name = Field(serializer=str)
... value = Field(serializer=int, limit=100)
...
>>> adapter = ItemAdapter(InventoryItem(name="foo", value=10))
>>> adapter.get_field_meta("name")
mappingproxy({'serializer': <class 'str'>})
>>> adapter.get_field_meta("value")
mappingproxy({'serializer': <class 'int'>, 'limit': 100})
dataclass objects
>>> from dataclasses import dataclass, field
>>> @dataclass
... class InventoryItem:
... name: str = field(metadata={"serializer": str})
... value: int = field(metadata={"serializer": int, "limit": 100})
...
>>> adapter = ItemAdapter(InventoryItem(name="foo", value=10))
>>> adapter.get_field_meta("name")
mappingproxy({'serializer': <class 'str'>})
>>> adapter.get_field_meta("value")
mappingproxy({'serializer': <class 'int'>, 'limit': 100})
attrs objects
>>> import attr
>>> @attr.s
... class InventoryItem:
... name = attr.ib(metadata={"serializer": str})
... value = attr.ib(metadata={"serializer": int})
...
>>> adapter = ItemAdapter(InventoryItem(name="foo", value=10))
>>> adapter.get_field_meta("name")
mappingproxy({'serializer': <class 'str'>})
>>> adapter.get_field_meta("value")
mappingproxy({'serializer': <class 'int'>})
Other types
In fact, any supported object with a fields
attribute which values are mappings works:
>>> class DictWithFields(dict):
... fields = {
... "name": {"serializer": str},
... "value": {"serializer": int, "limit": 100},
... }
...
>>> adapter = ItemAdapter(DictWithFields(name="foo", value=10))
>>> adapter.get_field_meta("name")
mappingproxy({'serializer': <class 'str'>})
>>> adapter.get_field_meta("value")
mappingproxy({'serializer': <class 'int'>, 'limit': 100})
Examples
scrapy.item.Item objects
>>> from scrapy.item import Item, Field
>>> from itemadapter import ItemAdapter
>>> class InventoryItem(Item):
... name = Field()
... price = Field()
...
>>> item = InventoryItem(name="foo", price=10)
>>> adapter = ItemAdapter(item)
>>> adapter.item is item
True
>>> adapter["name"]
'foo'
>>> adapter["name"] = "bar"
>>> adapter["price"] = 5
>>> item
{'name': 'bar', 'price': 5}
dict
>>> from itemadapter import ItemAdapter
>>> item = dict(name="foo", price=10)
>>> adapter = ItemAdapter(item)
>>> adapter.item is item
True
>>> adapter["name"]
'foo'
>>> adapter["name"] = "bar"
>>> adapter["price"] = 5
>>> item
{'name': 'bar', 'price': 5}
dataclass objects
>>> from dataclasses import dataclass
>>> from itemadapter import ItemAdapter
>>> @dataclass
... class InventoryItem:
... name: str
... price: int
...
>>> item = InventoryItem(name="foo", price=10)
>>> adapter = ItemAdapter(item)
>>> adapter.item is item
True
>>> adapter["name"]
'foo'
>>> adapter["name"] = "bar"
>>> adapter["price"] = 5
>>> item
InventoryItem(name='bar', price=5)
attrs objects
>>> import attr
>>> from itemadapter import ItemAdapter
>>> @attr.s
... class InventoryItem:
... name = attr.ib()
... price = attr.ib()
...
>>> item = InventoryItem(name="foo", price=10)
>>> adapter = ItemAdapter(item)
>>> adapter.item is item
True
>>> adapter["name"]
'foo'
>>> adapter["name"] = "bar"
>>> adapter["price"] = 5
>>> item
InventoryItem(name='bar', price=5)
[1]: dataclass objects as items:
issue and
pull request
Metadata
Release files for itemadapter 0.0.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| itemadapter-0.0.3.tar.gz | 6.9 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| itemadapter-0.0.3-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 13.0 kB
Release files / itemadapter-0.0.3.tar.gz
| Download URL | itemadapter-0.0.3.tar.gz |
|---|---|
| Size | 6.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
a40db2e93cfc79c7f8e7d7570e7b17f8fa058ab35e2cf3d03a2394c766efcaf3
|
|
BLAKE2b-256 checksum How to use checksums |
f629022f72e3a37f5dd96ea457ce5970c5b9d68de64fb6e8fecc20865794218c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.8.2
|
Release files / itemadapter-0.0.3-py3-none-any.whl
| Download URL | itemadapter-0.0.3-py3-none-any.whl |
|---|---|
| Size | 6.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
3e3c2acf2beebfd3f752a9cf8541af3afabdc6b96a4e3023fe7be6e60500babb
|
|
BLAKE2b-256 checksum How to use checksums |
a1c7eaad6eb5af691a833f5b894e2aef371115227f340e416f408e8dae6544c0
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.8.2
|