/[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 765 by bh, Tue Apr 29 12:42:14 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  #  #
# Line 12  Classes for handling tables of data. Line 12  Classes for handling tables of data.
12  __version__ = "$Revision$"  __version__ = "$Revision$"
13    
14  import dbflib  import dbflib
 from Thuban.common import *  
15    
16  # the field types supported by a Table instance.  # the field types supported by a Table instance.
17  #FIELDTYPE_INT = "int"  FIELDTYPE_INT = "int"
18  #FIELDTYPE_STRING = "string"  FIELDTYPE_STRING = "string"
19  #FIELDTYPE_DOUBLE = "double"  FIELDTYPE_DOUBLE = "double"
20    
21    
22  # 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 24  dbflib_fieldtypes = {dbflib.FTString: FI
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:
# Line 39  class Table: Line 36  class Table:
36      # important that Thuban can work with read-only files. Therefore the      # important that Thuban can work with read-only files. Therefore the
37      # DBF file is opened only for reading initially. Only when      # 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      # write_record is called we try to open the DBF file for writing as
39      # 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
40      # for all IO afterwards.      # all IO afterwards.
41      #      #
42      # 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
43      # 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 83  class Table:
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          """          """
# Line 112  class Table: Line 170  class Table:
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

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26