/[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 257 by bh, Thu Aug 15 12:47:59 2002 UTC revision 628 by jonathan, Wed Apr 9 10:09:15 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 32  class Table: Line 32  class Table:
32      Currently this is basically just a wrapper around dbflib.      Currently this is basically just a wrapper around dbflib.
33      """      """
34    
35        # Implementation strategy regarding writing to a DBF file:
36        #
37        # Most of the time Thuban only needs to read from a table and it is
38        # important that Thuban can work with read-only files. Therefore the
39        # DBF file is opened only for reading initially. Only when
40        # write_record is called we try to open the DBF file for writing as
41        # well. If that succeeds the read/write DBF file will be used for
42        # all IO afterwards.
43        #
44        # It's important to use the same DBF file object for both reading
45        # and writing to make sure that reading a records after writing
46        # returns the new values. With two separate objects this wouldn't
47        # work because a DBF file object buffers some data
48    
49      def __init__(self, filename):      def __init__(self, filename):
50          self.filename = filename          self.filename = filename
51          self.dbf = dbflib.DBFFile(filename)          self.dbf = dbflib.DBFFile(filename)
52    
53            # If true, self.dbf is open for writing.
54            self._writable = 0
55    
56      def Destroy(self):      def Destroy(self):
57          self.dbf.close()          self.dbf.close()
58          self.dbf = None          self.dbf = None
# Line 58  class Table: Line 75  class Table:
75          type = dbflib_fieldtypes[type]          type = dbflib_fieldtypes[type]
76          return type, name, width, prec          return type, name, width, prec
77    
78        def field_info_by_name(self, fieldName):
79            count = self.field_count()
80    
81            for i in range(count):
82                info = self.field_info(i)
83                if info[1] == fieldName:
84                    return info
85    
86            return None
87    
88        def field_range(self, fieldName):
89            """Finds the first occurences of the minimum and maximum values
90            in the table for the given field.
91    
92            This assumes that the standard comparison operators (<, >, etc.)
93            will work for the given data.
94    
95            Returns a tuple ((min, rec), (max, rec)) where:
96                min is the minimum value
97                max is the maximum value
98                rec is the record number where the value was found. One
99                    should check that the record number of min is not
100                    the same as the record number of max.
101    
102            Returns None if there are no records
103    
104            """
105    
106    
107            count = self.record_count()
108    
109            if count == 0:
110                return None
111    
112            rec = self.read_record(0)
113    
114            min = rec[fieldName]
115            min_rec = 0
116    
117            max = rec[fieldName]
118            max_rec = 0
119    
120            for i in range(1, count):
121                rec = self.read_record(i)
122                data = rec[fieldName]
123    
124                if data < min:
125                    min = data
126                    min_rec = rec
127                elif data > max:
128                    max = data
129                    max_rec = rec
130    
131            return ((min, min_rec), (max, max_rec))
132    
133        def GetUniqueValues(self, fieldName):
134            """Return a list of all unique entries in the table for the given
135            field name.
136            """
137    
138            dict = {}
139    
140            for i in range(0, self.record_count()):
141                rec = self.read_record(i)
142                data = rec[fieldName]
143    
144                if not dict.has_key(data):
145                    dict[data] = 0
146    
147            return dict.keys()
148    
149      def read_record(self, record):      def read_record(self, record):
150          """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
151          """          """
152          return self.dbf.read_record(record)          return self.dbf.read_record(record)
153    
154            def write_record(self, record, values):
155            """Write the values into the record
156    
157            The values parameter may either be a dictionary or a sequence.
158    
159            If it's a dictionary the keys must be the names of the fields
160            and their value must have a suitable type. Only the fields
161            actually contained in the dictionary are written. Fields for
162            which there's no item in the dict are not modified.
163    
164            If it's a sequence, all fields must be present in the right
165            order.
166            """
167            if not self._writable:
168                new_dbf = dbflib.DBFFile(self.filename, "r+b")
169                self.dbf.close()
170                self.dbf = new_dbf
171                self._writable = 1
172            self.dbf.write_record(record, values)
173            self.dbf.commit()
174    

Legend:
Removed from v.257  
changed lines
  Added in v.628

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26