Changeset 1838:e1c89f3252ea
- Timestamp:
- 11/06/09 16:39:15 (9 months ago)
- Author:
- Pablo Hoffman <pablo@…>
- Branch:
- default
- Message:
-
removed deprecated ScrapedItem? (previously kept for backwards compatibility)
- Files:
-
Legend:
- Unmodified
- Added
- Removed
-
|
r1606
|
r1838
|
|
| 8 | 8 | SPIDER_MODULES = ['googledir.spiders'] |
| 9 | 9 | NEWSPIDER_MODULE = 'googledir.spiders' |
| 10 | | DEFAULT_ITEM_CLASS = 'scrapy.item.ScrapedItem' |
| | 10 | DEFAULT_ITEM_CLASS = 'scrapy.item.Item' |
| 11 | 11 | USER_AGENT = '%s/%s' % (BOT_NAME, BOT_VERSION) |
| 12 | 12 | |
-
|
r1581
|
r1838
|
|
| 84 | 84 | __metaclass__ = ItemMeta |
| 85 | 85 | |
| 86 | | |
| 87 | | class ScrapedItem(BaseItem): |
| 88 | | |
| 89 | | def __init__(self, data=None): |
| 90 | | """ |
| 91 | | A ScrapedItem can be initialised with a dictionary that will be |
| 92 | | squirted directly into the object. |
| 93 | | """ |
| 94 | | import warnings |
| 95 | | warnings.warn("scrapy.item.ScrapedItem is deprecated, use scrapy.item.Item instead", |
| 96 | | DeprecationWarning, stacklevel=2) |
| 97 | | if isinstance(data, dict): |
| 98 | | for attr, value in data.iteritems(): |
| 99 | | setattr(self, attr, value) |
| 100 | | elif data is not None: |
| 101 | | raise TypeError("Initialize with dict, not %s" % data.__class__.__name__) |
| 102 | | |
| 103 | | def __repr__(self): |
| 104 | | """ |
| 105 | | Generate the following format so that items can be deserialized |
| 106 | | easily: ClassName({'attrib': value, ...}) |
| 107 | | """ |
| 108 | | reprdict = dict(items for items in self.__dict__.iteritems() \ |
| 109 | | if not items[0].startswith('_')) |
| 110 | | return "%s(%s)" % (self.__class__.__name__, repr(reprdict)) |
| 111 | | |
-
|
r1513
|
r1838
|
|
| 1 | 1 | import unittest |
| 2 | 2 | |
| 3 | | from scrapy.item import Item, Field, ScrapedItem |
| | 3 | from scrapy.item import Item, Field |
| 4 | 4 | |
| 5 | 5 | |
| … |
… |
|
| 128 | 128 | |
| 129 | 129 | |
| 130 | | # NOTE: ScrapedItem is deprecated and will be removed in the next stable |
| 131 | | # release, and so will these tests. |
| 132 | | |
| 133 | | class ScrapedItemTestCase(unittest.TestCase): |
| 134 | | |
| 135 | | def test_item(self): |
| 136 | | |
| 137 | | class MyItem(ScrapedItem): |
| 138 | | pass |
| 139 | | |
| 140 | | item = MyItem() |
| 141 | | self.assertEqual(repr(item), 'MyItem({})') |
| 142 | | |
| 143 | | item = ScrapedItem({'key': 'value'}) |
| 144 | | self.assertEqual(item.key, 'value') |
| 145 | | |
| 146 | | self.assertRaises(TypeError, ScrapedItem, 10) |
| 147 | | |
| 148 | 130 | if __name__ == "__main__": |
| 149 | 131 | unittest.main() |