File: declaring-loaders.rst

package info (click to toggle)
python-itemloaders 1.3.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 320 kB
  • sloc: python: 1,547; makefile: 78
file content (55 lines) | stat: -rw-r--r-- 1,697 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
.. currentmodule:: itemloaders

.. _declaring-loaders:

Declaring Item Loaders
======================

Item Loaders are declared by using a class definition syntax. Here is an example::

    from itemloaders import ItemLoader
    from itemloaders.processors import TakeFirst, MapCompose, Join

    class ProductLoader(ItemLoader):

        default_output_processor = TakeFirst()

        name_in = MapCompose(str.title)
        name_out = Join()

        # using a built-in processor
        price_in = MapCompose(str.strip)

        # using a function
        def price_out(self, values):
            return float(values[0])

    loader = ProductLoader()
    loader.add_value('name', 'plasma TV')
    loader.add_value('price', '999.98')
    loader.load_item()
    # {'name': 'Plasma Tv', 'price': 999.98}

As you can see, input processors are declared using the ``_in`` suffix while
output processors are declared using the ``_out`` suffix. And you can also
declare a default input/output processors using the
:attr:`ItemLoader.default_input_processor` and
:attr:`ItemLoader.default_output_processor` attributes.

The precedence order, for both input and output processors, is as follows:

1.  Item Loader field-specific attributes: ``field_in`` and ``field_out`` (most
    precedence)

2.  Field metadata (``input_processor`` and ``output_processor`` keys).

    Check out `itemadapter field metadata
    <https://github.com/scrapy/itemadapter#metadata-support>`_ for more
    information.

    .. versionadded:: 1.0.1

3. Item Loader defaults: :meth:`ItemLoader.default_input_processor` and
   :meth:`ItemLoader.default_output_processor` (least precedence)

See also: :ref:`extending-loaders`.