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 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
|
======= ==============================
SEP 5
Title ItemBuilder API
Author Ismael Carnales, Pablo Hoffman
Created 2009-07-24
Status Obsoleted by :doc:`sep-008`
======= ==============================
=========================================
SEP-005: Detailed ``ItemBuilder`` API use
=========================================
Item class for examples:
.. code-block:: python
#!python
class NewsItem(Item):
url = fields.TextField()
headline = fields.TextField()
content = fields.TextField()
published = fields.DateField()
gSetting expanders
==================
.. code-block:: python
#!python
class NewsItemBuilder(ItemBuilder):
item_class = NewsItem
headline = reducers.Reducer(extract, remove_tags(), unquote(), strip)
This approach will override the Reducer class for ``BuilderFields`` depending
on their Item Field class:
* ``MultivaluedField`` = ``PassValue``
* ``TextField`` = ``JoinStrings``
* other = ``TakeFirst``
gSetting reducers
=================
.. code-block:: python
#!python
class NewsItemBuilder(ItemBuilder):
item_class = NewsItem
headline = reducers.TakeFirst(extract, remove_tags(), unquote(), strip)
published = reducers.Reducer(extract, remove_tags(), unquote(), strip)
As with the previous example this would select join_strings as the reducer for
content
gSetting expanders/reducers new way
===================================
.. code-block:: python
#!python
class NewsItemBuilder(ItemBuilder):
item_class = NewsItem
headline = BuilderField(extract, remove_tags(), unquote(), strip)
content = BuilderField(extract, remove_tags(), unquote(), strip)
class Reducer:
headline = TakeFirst
gExtending ``ItemBuilder``
==========================
.. code-block:: python
#!python
class SiteNewsItemBuilder(NewsItemBuilder):
published = reducers.Reducer(
extract, remove_tags(), unquote(), strip, to_date("%d.%m.%Y")
)
gExtending ``ItemBuilder`` using statich methods
================================================
.. code-block:: python
#!python
class SiteNewsItemBuilder(NewsItemBuilder):
published = reducers.Reducer(NewsItemBuilder.published, to_date("%d.%m.%Y"))
gUsing default_builder
======================
.. code-block:: python
#!python
class DefaultedNewsItemBuilder(ItemBuilder):
item_class = NewsItem
default_builder = reducers.Reducer(extract, remove_tags(), unquote(), strip)
This will use default_builder as the builder for every field in the item class.
As a reducer is not set reducers will be set based on Item Field classes.
gReset default_builder for a field
==================================
.. code-block:: python
#!python
class DefaultedNewsItemBuilder(ItemBuilder):
item_class = NewsItem
default_builder = reducers.Reducer(extract, remove_tags(), unquote(), strip)
url = BuilderField()
gExtending default ``ItemBuilder``
==================================
.. code-block:: python
#!python
class SiteNewsItemBuilder(NewsItemBuilder):
published = reducers.Reducer(
extract, remove_tags(), unquote(), strip, to_date("%d.%m.%Y")
)
gExtending default ``ItemBuilder`` using static methods
=======================================================
.. code-block:: python
#!python
class SiteNewsItemBuilder(NewsItemBuilder):
published = reducers.Reducer(NewsItemBuilder.default_builder, to_date("%d.%m.%Y"))
|