-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathitems.py
145 lines (111 loc) · 4.23 KB
/
items.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
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
#!/usr/bin/python
# -*- coding: utf-8 -*-
'''
Created on Jul 22, 2014
@author: zking
'''
import datetime
import pprint
from copy import deepcopy
################################################################################
# ITEM BASE CLASS
# Define your own item classes below
################################################################################
class Item(object):
'''
Item base class. Extend this class to build your own item types
Define the fields you want to use
items need to have an 'id' field, which serves as a unique identifier in the database
the field names 'created', 'updated' and 'hash' may not be used, because these names
are used internally
field types may be
* int
* long
* float
* str
* unicode
* buffer
* datetime.datetime
* datetime.date
Fields are defines as dictionary
keys are the field names, values are dictionaries defining a "type"
fields = {
'id': {'type': int},
}
fields may also have 'hash' set to True, resulting in the field's contents being part of the
items hash:
fields = {
'id': {'type': int, 'hash': True},
}
'''
# field definitions
fields = {
'id': {'type': int},
}
def __init__(self, **kwargs):
'''
the constructor deepcopies the fields for use in the actual instances
'''
#deepcopy fields for each instance
self._fields = {}
for k,v in self.fields.iteritems():
self._fields[k] = deepcopy(v)
if kwargs.has_key(k): self[k] = kwargs[k]
########################
# DICTIONARY INTERFACE #
########################
def __str__(self):
d = dict([(k,v['value'] if v.has_key('value') else None) for k,v in self._fields.iteritems()])
return pprint.pformat(d)
def __len__(self):
return len(self._fields)
def __setitem__(self, name, value):
if not self._fields.has_key(name): raise KeyError(name)
if value.__class__ != self._fields[name]['type']: raise ValueError('Type mismatch: %s: %s, %s' % (name, value.__class__.__name__, self._fields[name]['type'].__name__))
self._fields[name]['value'] = value
def __getitem__(self, name):
if not self._fields.has_key(name): raise KeyError(name)
try: return self._fields[name]['value']
except KeyError: return None
def __delitem__(self, name):
if not self._fields.has_key(name): raise KeyError(name)
try: del self._fields[name]['value']
except KeyError: pass
def __iter__(self):
for k,v in self._fields.iteritems():
try: value = v['value']
except KeyError: value = None
yield k, value
def __contains__(self, name):
return self._fields.has_key(name)
################################################################################
# YOUR ITEM CLASSES HERE
# EXTEND "Item" class and define your fields
################################################################################
class FlohmarktItem(Item):
fields = {
'id': {'type': unicode},
'title': {'type': unicode},
'timestamp':{'type': datetime.datetime},
'url': {'type': unicode},
'text': {'type': unicode, 'hash': True},
}
# ----------------------------------------------------------------------------------------------------
class LeerstandItem(Item):
fields = {
'id': {'type': unicode},
'name': {'type': unicode},
'link': {'type': unicode},
'inactive': {'type': unicode, 'hash': True},
'address': {'type': unicode},
'author': {'type': unicode},
'comments': {'type': unicode, 'hash': True},
}
# ----------------------------------------------------------------------------------------------------
class EdiktItem(Item):
fields = {
'id': {'type': unicode},
'title': {'type': unicode},
'url': {'type': unicode},
'text': {'type': unicode, 'hash': True},
}