24 |
dbflib.FTInteger: FIELDTYPE_INT, |
dbflib.FTInteger: FIELDTYPE_INT, |
25 |
dbflib.FTDouble: FIELDTYPE_DOUBLE} |
dbflib.FTDouble: FIELDTYPE_DOUBLE} |
26 |
|
|
27 |
class Table: |
class DBFTable: |
28 |
|
|
29 |
""" |
""" |
30 |
Represent a table of data. |
Table interface for the data in a DBF file |
|
|
|
|
Currently this is basically just a wrapper around dbflib. |
|
31 |
""" |
""" |
32 |
|
|
33 |
# Implementation strategy regarding writing to a DBF file: |
# Implementation strategy regarding writing to a DBF file: |
83 |
|
|
84 |
return None |
return None |
85 |
|
|
86 |
|
def field_range(self, fieldName): |
87 |
|
"""Finds the first occurences of the minimum and maximum values |
88 |
|
in the table for the given field. |
89 |
|
|
90 |
|
This assumes that the standard comparison operators (<, >, etc.) |
91 |
|
will work for the given data. |
92 |
|
|
93 |
|
Returns a tuple ((min, rec), (max, rec)) where: |
94 |
|
min is the minimum value |
95 |
|
max is the maximum value |
96 |
|
rec is the record number where the value was found. One |
97 |
|
should check that the record number of min is not |
98 |
|
the same as the record number of max. |
99 |
|
|
100 |
|
Returns None if there are no records |
101 |
|
|
102 |
|
""" |
103 |
|
|
104 |
|
|
105 |
|
count = self.record_count() |
106 |
|
|
107 |
|
if count == 0: |
108 |
|
return None |
109 |
|
|
110 |
|
rec = self.read_record(0) |
111 |
|
|
112 |
|
min = rec[fieldName] |
113 |
|
min_rec = 0 |
114 |
|
|
115 |
|
max = rec[fieldName] |
116 |
|
max_rec = 0 |
117 |
|
|
118 |
|
for i in range(1, count): |
119 |
|
rec = self.read_record(i) |
120 |
|
data = rec[fieldName] |
121 |
|
|
122 |
|
if data < min: |
123 |
|
min = data |
124 |
|
min_rec = rec |
125 |
|
elif data > max: |
126 |
|
max = data |
127 |
|
max_rec = rec |
128 |
|
|
129 |
|
return ((min, min_rec), (max, max_rec)) |
130 |
|
|
131 |
|
def GetUniqueValues(self, fieldName): |
132 |
|
"""Return a list of all unique entries in the table for the given |
133 |
|
field name. |
134 |
|
""" |
135 |
|
|
136 |
|
dict = {} |
137 |
|
|
138 |
|
for i in range(0, self.record_count()): |
139 |
|
rec = self.read_record(i) |
140 |
|
data = rec[fieldName] |
141 |
|
|
142 |
|
if not dict.has_key(data): |
143 |
|
dict[data] = 0 |
144 |
|
|
145 |
|
return dict.keys() |
146 |
|
|
147 |
def read_record(self, record): |
def read_record(self, record): |
148 |
"""Return the record no. record as a dict mapping field names to values |
"""Return the record no. record as a dict mapping field names to values |
149 |
""" |
""" |
170 |
self.dbf.write_record(record, values) |
self.dbf.write_record(record, values) |
171 |
self.dbf.commit() |
self.dbf.commit() |
172 |
|
|
173 |
|
|
174 |
|
|
175 |
|
# Temporary backwards compatibility |
176 |
|
Table = DBFTable |