/[thuban]/branches/WIP-pyshapelib-bramz/Thuban/Model/table.py
ViewVC logotype

Diff of /branches/WIP-pyshapelib-bramz/Thuban/Model/table.py

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 467 by jonathan, Wed Mar 5 18:18:38 2003 UTC revision 806 by jan, Fri May 2 16:43:59 2003 UTC
# Line 1  Line 1 
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.
# Line 12  Classes for handling tables of data. Line 13  Classes for handling tables of data.
13  __version__ = "$Revision$"  __version__ = "$Revision$"
14    
15  import dbflib  import dbflib
 from Thuban.common import *  
16    
17  # the field types supported by a Table instance.  # the field types supported by a Table instance.
18  #FIELDTYPE_INT = "int"  FIELDTYPE_INT = "int"
19  #FIELDTYPE_STRING = "string"  FIELDTYPE_STRING = "string"
20  #FIELDTYPE_DOUBLE = "double"  FIELDTYPE_DOUBLE = "double"
21    
22    
23  # map the dbflib constants for the field types to our constants  # map the dbflib constants for the field types to our constants
# Line 25  dbflib_fieldtypes = {dbflib.FTString: FI Line 25  dbflib_fieldtypes = {dbflib.FTString: FI
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        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      Currently this is basically just a wrapper around dbflib.      """
68        Table interface for the data in a DBF file
69      """      """
70    
71      # Implementation strategy regarding writing to a DBF file:      # Implementation strategy regarding writing to a DBF file:
# Line 39  class Table: Line 74  class Table:
74      # important that Thuban can work with read-only files. Therefore the      # important that Thuban can work with read-only files. Therefore the
75      # DBF file is opened only for reading initially. Only when      # 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      # write_record is called we try to open the DBF file for writing as
77      # well. If that succeeds the dbf read/write DBF file will be used      # well. If that succeeds the read/write DBF file will be used for
78      # for all IO afterwards.      # all IO afterwards.
79      #      #
80      # It's important to use the same DBF file object for both reading      # 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      # and writing to make sure that reading a records after writing
# Line 86  class Table: Line 121  class Table:
121    
122          return None          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          """          """
# Line 112  class Table: Line 208  class Table:
208          self.dbf.write_record(record, values)          self.dbf.write_record(record, values)
209          self.dbf.commit()          self.dbf.commit()
210    
211    
212    
213    # Temporary backwards compatibility
214    Table = DBFTable

Legend:
Removed from v.467  
changed lines
  Added in v.806

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26