1 |
# Copyright (c) 2001, 2002 by Intevation GmbH |
# Copyright (c) 2001, 2002, 2003 by Intevation GmbH |
2 |
# Authors: |
# Authors: |
3 |
# Bernhard Herzog <[email protected]> |
# Bernhard Herzog <[email protected]> |
4 |
|
# Jan-Oliver Wagner <[email protected]> |
5 |
# |
# |
6 |
# This program is free software under the GPL (>=v2) |
# This program is free software under the GPL (>=v2) |
7 |
# Read the file COPYING coming with Thuban for details. |
# Read the file COPYING coming with Thuban for details. |
25 |
dbflib.FTInteger: FIELDTYPE_INT, |
dbflib.FTInteger: FIELDTYPE_INT, |
26 |
dbflib.FTDouble: FIELDTYPE_DOUBLE} |
dbflib.FTDouble: FIELDTYPE_DOUBLE} |
27 |
|
|
28 |
class Table: |
class MemoryTable: |
29 |
|
|
30 |
""" |
"""Quite simple table implementation that operates on a list of tuples. |
31 |
Represent a table of data. |
All of the data are kept in the memory.""" |
32 |
|
|
33 |
|
def __init__(self, fields, data): |
34 |
|
"""Initialize the MemoryTable |
35 |
|
|
36 |
|
Parameters: |
37 |
|
fields -- List of (name, field_type) pairs |
38 |
|
data -- List of tuples, one for each row of data |
39 |
|
""" |
40 |
|
self.fields = fields |
41 |
|
self.data = data |
42 |
|
|
43 |
|
def field_count(self): |
44 |
|
return len(self.fields) |
45 |
|
|
46 |
|
def field_info(self, index): |
47 |
|
name, type = self.fields[index] |
48 |
|
return (type, name) |
49 |
|
|
50 |
|
def record_count(self): |
51 |
|
return len(self.data) |
52 |
|
|
53 |
|
def read_record(self, index): |
54 |
|
return dict([(self.fields[i][0], self.data[index][i]) |
55 |
|
for i in range(len(self.fields))]) |
56 |
|
|
57 |
Currently this is basically just a wrapper around dbflib. |
def write_record(self, record, values): |
58 |
|
# TODO: Check for correct lenght and perhaps also |
59 |
|
# for correct types in case values is a tuple. How to report problems? |
60 |
|
# TODO: Allow values to be a dictionary and write the single |
61 |
|
# fields that are specified. |
62 |
|
self.data[record] = values |
63 |
|
|
64 |
|
|
65 |
|
class DBFTable: |
66 |
|
|
67 |
|
""" |
68 |
|
Table interface for the data in a DBF file |
69 |
""" |
""" |
70 |
|
|
71 |
|
# Implementation strategy regarding writing to a DBF file: |
72 |
|
# |
73 |
|
# Most of the time Thuban only needs to read from a table and it is |
74 |
|
# important that Thuban can work with read-only files. Therefore the |
75 |
|
# DBF file is opened only for reading initially. Only when |
76 |
|
# write_record is called we try to open the DBF file for writing as |
77 |
|
# well. If that succeeds the read/write DBF file will be used for |
78 |
|
# all IO afterwards. |
79 |
|
# |
80 |
|
# It's important to use the same DBF file object for both reading |
81 |
|
# and writing to make sure that reading a records after writing |
82 |
|
# returns the new values. With two separate objects this wouldn't |
83 |
|
# work because a DBF file object buffers some data |
84 |
|
|
85 |
def __init__(self, filename): |
def __init__(self, filename): |
86 |
self.filename = filename |
self.filename = filename |
87 |
self.dbf = dbflib.DBFFile(filename) |
self.dbf = dbflib.DBFFile(filename) |
88 |
|
|
89 |
|
# If true, self.dbf is open for writing. |
90 |
|
self._writable = 0 |
91 |
|
|
92 |
def Destroy(self): |
def Destroy(self): |
93 |
self.dbf.close() |
self.dbf.close() |
94 |
self.dbf = None |
self.dbf = None |
111 |
type = dbflib_fieldtypes[type] |
type = dbflib_fieldtypes[type] |
112 |
return type, name, width, prec |
return type, name, width, prec |
113 |
|
|
114 |
|
def field_info_by_name(self, fieldName): |
115 |
|
count = self.field_count() |
116 |
|
|
117 |
|
for i in range(count): |
118 |
|
info = self.field_info(i) |
119 |
|
if info[1] == fieldName: |
120 |
|
return info |
121 |
|
|
122 |
|
return None |
123 |
|
|
124 |
|
def field_range(self, fieldName): |
125 |
|
"""Finds the first occurences of the minimum and maximum values |
126 |
|
in the table for the given field. |
127 |
|
|
128 |
|
This assumes that the standard comparison operators (<, >, etc.) |
129 |
|
will work for the given data. |
130 |
|
|
131 |
|
Returns a tuple ((min, rec), (max, rec)) where: |
132 |
|
min is the minimum value |
133 |
|
max is the maximum value |
134 |
|
rec is the record number where the value was found. One |
135 |
|
should check that the record number of min is not |
136 |
|
the same as the record number of max. |
137 |
|
|
138 |
|
Returns None if there are no records |
139 |
|
|
140 |
|
""" |
141 |
|
|
142 |
|
|
143 |
|
count = self.record_count() |
144 |
|
|
145 |
|
if count == 0: |
146 |
|
return None |
147 |
|
|
148 |
|
rec = self.read_record(0) |
149 |
|
|
150 |
|
min = rec[fieldName] |
151 |
|
min_rec = 0 |
152 |
|
|
153 |
|
max = rec[fieldName] |
154 |
|
max_rec = 0 |
155 |
|
|
156 |
|
for i in range(1, count): |
157 |
|
rec = self.read_record(i) |
158 |
|
data = rec[fieldName] |
159 |
|
|
160 |
|
if data < min: |
161 |
|
min = data |
162 |
|
min_rec = rec |
163 |
|
elif data > max: |
164 |
|
max = data |
165 |
|
max_rec = rec |
166 |
|
|
167 |
|
return ((min, min_rec), (max, max_rec)) |
168 |
|
|
169 |
|
def GetUniqueValues(self, fieldName): |
170 |
|
"""Return a list of all unique entries in the table for the given |
171 |
|
field name. |
172 |
|
""" |
173 |
|
|
174 |
|
dict = {} |
175 |
|
|
176 |
|
for i in range(0, self.record_count()): |
177 |
|
rec = self.read_record(i) |
178 |
|
data = rec[fieldName] |
179 |
|
|
180 |
|
if not dict.has_key(data): |
181 |
|
dict[data] = 0 |
182 |
|
|
183 |
|
return dict.keys() |
184 |
|
|
185 |
def read_record(self, record): |
def read_record(self, record): |
186 |
"""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 |
187 |
""" |
""" |
200 |
If it's a sequence, all fields must be present in the right |
If it's a sequence, all fields must be present in the right |
201 |
order. |
order. |
202 |
""" |
""" |
203 |
writable_dbf = dbflib.DBFFile(self.filename, "r+b") |
if not self._writable: |
204 |
writable_dbf.write_record(record, values) |
new_dbf = dbflib.DBFFile(self.filename, "r+b") |
205 |
writable_dbf.close() |
self.dbf.close() |
206 |
|
self.dbf = new_dbf |
207 |
|
self._writable = 1 |
208 |
|
self.dbf.write_record(record, values) |
209 |
|
self.dbf.commit() |
210 |
|
|
211 |
|
|
212 |
|
|
213 |
|
# Temporary backwards compatibility |
214 |
|
Table = DBFTable |