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 |
# |
# |
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: |
34 |
|
# |
35 |
|
# Most of the time Thuban only needs to read from a table and it is |
36 |
|
# important that Thuban can work with read-only files. Therefore the |
37 |
|
# DBF file is opened only for reading initially. Only when |
38 |
|
# write_record is called we try to open the DBF file for writing as |
39 |
|
# well. If that succeeds the read/write DBF file will be used for |
40 |
|
# all IO afterwards. |
41 |
|
# |
42 |
|
# It's important to use the same DBF file object for both reading |
43 |
|
# and writing to make sure that reading a records after writing |
44 |
|
# returns the new values. With two separate objects this wouldn't |
45 |
|
# work because a DBF file object buffers some data |
46 |
|
|
47 |
def __init__(self, filename): |
def __init__(self, filename): |
48 |
self.filename = filename |
self.filename = filename |
49 |
self.dbf = dbflib.DBFFile(filename) |
self.dbf = dbflib.DBFFile(filename) |
50 |
|
|
51 |
|
# If true, self.dbf is open for writing. |
52 |
|
self._writable = 0 |
53 |
|
|
54 |
def Destroy(self): |
def Destroy(self): |
55 |
self.dbf.close() |
self.dbf.close() |
56 |
self.dbf = None |
self.dbf = None |
73 |
type = dbflib_fieldtypes[type] |
type = dbflib_fieldtypes[type] |
74 |
return type, name, width, prec |
return type, name, width, prec |
75 |
|
|
76 |
|
def field_info_by_name(self, fieldName): |
77 |
|
count = self.field_count() |
78 |
|
|
79 |
|
for i in range(count): |
80 |
|
info = self.field_info(i) |
81 |
|
if info[1] == fieldName: |
82 |
|
return info |
83 |
|
|
84 |
|
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 |
""" |
""" |
162 |
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 |
163 |
order. |
order. |
164 |
""" |
""" |
165 |
writable_dbf = dbflib.DBFFile(self.filename, "r+b") |
if not self._writable: |
166 |
writable_dbf.write_record(record, values) |
new_dbf = dbflib.DBFFile(self.filename, "r+b") |
167 |
writable_dbf.close() |
self.dbf.close() |
168 |
|
self.dbf = new_dbf |
169 |
|
self._writable = 1 |
170 |
|
self.dbf.write_record(record, values) |
171 |
|
self.dbf.commit() |
172 |
|
|
173 |
|
|
174 |
|
|
175 |
|
# Temporary backwards compatibility |
176 |
|
Table = DBFTable |