-
Notifications
You must be signed in to change notification settings - Fork 1
/
item.py
50 lines (34 loc) · 1.18 KB
/
item.py
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
from UserDict import DictMixin # Item wrapper
# Placeholder for required fields in the Item() class.
class Field(object):
pass
# Item class -- taken from Scrapy
class BaseItem(object):
pass
class DictItem(DictMixin, BaseItem):
fields = {}
def __init__(self):
self._values = {}
def __getitem__(self, key):
return self._values[key]
def __setitem__(self, key, value):
if key in self.fields:
self._values[key] = value
else:
raise KeyError("%s does not support field: %s" % (self.__class__.__name__, key))
def __delitem__(self, key):
del self._values[key]
def __getattr__(self, name):
if name in self.fields:
raise AttributeError("Use item[%r] to get field value" % name)
raise AttributeError(name)
def __setattr__(self, name, value):
if not name.startswith('_'):
raise AttributeError("Use item[%r] = %r to set field value" % (name, value))
super(DictItem, self).__setattr__(name, value)
def keys(self):
return self._values.keys()
def __repr__(self):
return '<Item: %s>' % self._values
class Item(BaseItem):
pass