File: sep-002.rst

package info (click to toggle)
python-scrapy 2.13.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,664 kB
  • sloc: python: 52,028; xml: 199; makefile: 25; sh: 7
file content (116 lines) | stat: -rw-r--r-- 2,696 bytes parent folder | download | duplicates (2)
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
=======  ==========================
SEP      3
Title    List fields API
Author   Pablo Hoffman
Created  2009-07-21
Status   Obsolete by :ref:`sep-008`
=======  ==========================

=========================
SEP-002 - List fields API
=========================

This page presents different usage scenarios for the new multi-valued field,
called !ListField.

Proposed Implementation
=======================

.. code-block:: python

   #!python
   from scrapy.item.fields import BaseField


   class ListField(BaseField):
       def __init__(self, field, default=None):
           self._field = field
           super(ListField, self).__init__(default)

       def to_python(self, value):
           if hasattr(value, "__iter__"):  # str/unicode not allowed
               return [self._field.to_python(v) for v in value]
           else:
               raise TypeError("Expected iterable, got %s" % type(value).__name__)

       def get_default(self):
           # must return a new copy to avoid unexpected behaviors with mutable defaults
           return list(self._default)

Usage Scenarios
===============

Defining a list field
---------------------

.. code-block:: python

   #!python
   from scrapy.item.models import Item
   from scrapy.item.fields import ListField, TextField, DateField, IntegerField


   class Article(Item):
       categories = ListField(TextField)
       dates = ListField(DateField, default=[])
       numbers = ListField(IntegerField, [])

Another case of products and variants which highlights the fact that it's
important to instantiate !ListField with field instances, not classes:

.. code-block:: python

   #!python
   from scrapy.item.models import Item
   from scrapy.item.fields import ListField, TextField


   class Variant(Item):
       name = TextField()


   class Product(Variant):
       variants = ListField(ItemField(Variant))

Assigning a list field
----------------------

.. code-block:: python

   #!python
   i = Article()

   i["categories"] = []
   i["categories"] = ["politics", "sport"]
   i["categories"] = ["test", 1]  # -> raises TypeError
   i["categories"] = asd  # -> raises TypeError

   i["dates"] = []
   i["dates"] = ["2009-01-01"]  # raises TypeError? (depends on TextField)

   i["numbers"] = ["1", 2, "3"]
   i["numbers"]  # returns [1, 2, 3]

Default values
--------------

.. code-block:: python

   #!python
   i = Article()

   i["categories"]  # raises KeyError
   i.get("categories")  # returns None

   i["numbers"]  # returns []

Appending values
----------------

.. code-block:: python

   #!python
   i = Article()

   i["categories"] = ["one", "two"]
   i["categories"].append(3)  # XXX: should this fail?