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.
Currently supported types are:
dictscrapy.item.Itemdataclass-based classesattrs-based classes
Requirements
- Python 3.5+
scrapy: optional, needed to interact withscrapyitemsdataclasses(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
Installation
itemadapter is available on PyPI, it can be installed with pip:
pip install itemadapter
License
itemadapter is distributed under a BSD-3 license.
Basic usage
The following is a simple example using a dataclass object.
Consider the following type definition:
from dataclasses import dataclass
from itemadapter import ItemAdapter, is_item
@dataclass
class InventoryItem:
name: str
price: float
stock: int
The ItemAdapter object can be treated much like a dictionary:
>>> obj = InventoryItem(name='foo', price=20.5, stock=10)
>>> is_item(obj)
True
>>> adapter = ItemAdapter(obj)
>>> len(adapter)
3
>>> adapter["name"]
'foo'
>>> adapter.get("price")
20.5
The wrapped object is modified in-place:
>>> adapter["name"] = "bar"
>>> adapter.update({"price": 12.7, "stock": 9})
>>> adapter.item
InventoryItem(name='bar', price=12.7, stock=9)
>>> adapter.item is obj
True
Converting to dict
The ItemAdapter class provides the asdict method, which converts
nested items recursively. Consider the following example:
from dataclasses import dataclass
from itemadapter import ItemAdapter
@dataclass
class Price:
value: int
currency: str
@dataclass
class Product:
name: str
price: Price
>>> item = Product("Stuff", Price(42, "UYU"))
>>> adapter = ItemAdapter(item)
>>> adapter.asdict()
{'name': 'Stuff', 'price': {'currency': 'UYU', 'value': 42}}
Note that just passing an adapter object to the dict built-in also works,
but it doesn't traverse the object recursively converting nested items:
>>> dict(adapter)
{'name': 'Stuff', 'price': Price(value=42, currency='UYU')}
Public 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).
Some additional methods are available:
get_field_meta(field_name: str) -> MappingProxyType
Return a MappingProxyType
object, which is a read-only mapping with metadata about the given field. If the item class does not
support field metadata, or there is no metadata for the given field, an empty object is returned.
The returned value is taken from the following sources, depending on the item type:
scrapy.item.Fieldforscrapy.item.Itemsdataclasses.field.metadatafordataclass-based itemsattr.Attribute.metadataforattrs-based items
field_names() -> KeysView
Return a keys view with the names of all the defined fields for the item.
asdict() -> dict
Return a dict object with the contents of the adapter. This works slightly different than
calling dict(adapter), because it's applied recursively to nested items (if there are any).
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, "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'>})
More 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)
Metadata
Release files for itemadapter 0.1.0
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.1.0.tar.gz | 8.0 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| itemadapter-0.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 15.1 kB
Release files / itemadapter-0.1.0.tar.gz
| Download URL | itemadapter-0.1.0.tar.gz |
|---|---|
| Size | 8.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
52159b4f97d82aa2968000ee8371b2114af56a2f44e4cd9142580d46eea39020
|
|
BLAKE2b-256 checksum How to use checksums |
d097545f6a1b76670c09899d36940482ea3137d79f1d82b7974a46c9b1741b7d
|
| 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/47.1.1 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.8.3
|
Release files / itemadapter-0.1.0-py3-none-any.whl
| Download URL | itemadapter-0.1.0-py3-none-any.whl |
|---|---|
| Size | 7.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
de3f6f567446122a48d496c10b5d830d7909f2399bc7ad51d61163c57967f480
|
|
BLAKE2b-256 checksum How to use checksums |
7dfb92f848fcfa85dc9f95370eaecb5c99b5230dd4fc5c6bae684f4ca59df973
|
| 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/47.1.1 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.8.3
|