1 |
bh |
257 |
# Copyright (c) 2001, 2002 by Intevation GmbH |
2 |
bh |
6 |
# Authors: |
3 |
|
|
# Bernhard Herzog <[email protected]> |
4 |
|
|
# |
5 |
|
|
# This program is free software under the GPL (>=v2) |
6 |
|
|
# Read the file COPYING coming with Thuban for details. |
7 |
|
|
|
8 |
|
|
""" |
9 |
|
|
Classes for handling tables of data. |
10 |
|
|
""" |
11 |
|
|
|
12 |
|
|
__version__ = "$Revision$" |
13 |
|
|
|
14 |
|
|
import dbflib |
15 |
|
|
|
16 |
|
|
# the field types supported by a Table instance. |
17 |
|
|
FIELDTYPE_INT = "int" |
18 |
|
|
FIELDTYPE_STRING = "string" |
19 |
|
|
FIELDTYPE_DOUBLE = "double" |
20 |
|
|
|
21 |
|
|
|
22 |
|
|
# map the dbflib constants for the field types to our constants |
23 |
|
|
dbflib_fieldtypes = {dbflib.FTString: FIELDTYPE_STRING, |
24 |
|
|
dbflib.FTInteger: FIELDTYPE_INT, |
25 |
|
|
dbflib.FTDouble: FIELDTYPE_DOUBLE} |
26 |
|
|
|
27 |
|
|
class Table: |
28 |
|
|
|
29 |
|
|
""" |
30 |
|
|
Represent a table of data. |
31 |
|
|
|
32 |
|
|
Currently this is basically just a wrapper around dbflib. |
33 |
|
|
""" |
34 |
|
|
|
35 |
|
|
def __init__(self, filename): |
36 |
|
|
self.filename = filename |
37 |
|
|
self.dbf = dbflib.DBFFile(filename) |
38 |
|
|
|
39 |
bh |
257 |
def Destroy(self): |
40 |
|
|
self.dbf.close() |
41 |
|
|
self.dbf = None |
42 |
|
|
|
43 |
bh |
6 |
def record_count(self): |
44 |
|
|
"""Return the number of records""" |
45 |
|
|
return self.dbf.record_count() |
46 |
|
|
|
47 |
|
|
def field_count(self): |
48 |
|
|
"""Return the number of fields in a record""" |
49 |
|
|
return self.dbf.field_count() |
50 |
|
|
|
51 |
|
|
def field_info(self, field): |
52 |
|
|
"""Return a tuple (type, name, width, prec) for the field no. field |
53 |
|
|
|
54 |
|
|
type is the data type of the field, name the name, width the |
55 |
|
|
field width in characters and prec the decimal precision. |
56 |
|
|
""" |
57 |
|
|
type, name, width, prec = self.dbf.field_info(field) |
58 |
|
|
type = dbflib_fieldtypes[type] |
59 |
|
|
return type, name, width, prec |
60 |
|
|
|
61 |
|
|
def read_record(self, record): |
62 |
|
|
"""Return the record no. record as a dict mapping field names to values |
63 |
|
|
""" |
64 |
|
|
return self.dbf.read_record(record) |
65 |
|
|
|
66 |
|
|
|