230 |
|
|
231 |
# DBF specific interface parts. |
# DBF specific interface parts. |
232 |
|
|
|
def Precision(self, col): |
|
|
"""Return column precision""" |
|
|
return self.column_map[col].prec |
|
|
|
|
233 |
def Width(self, col): |
def Width(self, col): |
234 |
"""Return column width""" |
"""Return column width""" |
235 |
return self.column_map[col].width |
return self.column_map[col].width |
368 |
def Width(self, col): |
def Width(self, col): |
369 |
"""Return the maximum width of values in the column |
"""Return the maximum width of values in the column |
370 |
|
|
371 |
The return value is the the maximum length of string representation |
The return value is the the maximum length of string |
372 |
of the values in the column (represented by index or name).""" |
representation of the values in the column (represented by index |
373 |
|
or name). |
374 |
|
""" |
375 |
max = 0 |
max = 0 |
376 |
|
|
377 |
type = self.column_map[col].type |
type = self.column_map[col].type |
378 |
index = self.column_map[col].index |
index = self.column_map[col].index |
379 |
values = [row[index] for row in self.data] |
values = [row[index] for row in self.data] |
381 |
return None |
return None |
382 |
|
|
383 |
if type == FIELDTYPE_DOUBLE: |
if type == FIELDTYPE_DOUBLE: |
384 |
prec = self.Precision(col) |
format = "%.12f" |
|
format = "%%.%df" % prec |
|
385 |
elif type == FIELDTYPE_INT: |
elif type == FIELDTYPE_INT: |
386 |
format = "%d" |
format = "%d" |
387 |
else: |
else: |
393 |
|
|
394 |
return max |
return max |
395 |
|
|
|
def Precision(self, col): |
|
|
"""Return the precision of the column |
|
|
|
|
|
The return value is the maximum number of numeric characters after the |
|
|
decimal if column type is double. Else precision zero is returned. |
|
|
The column can be represented by index or name. |
|
|
""" |
|
|
|
|
|
type = self.column_map[col].type |
|
|
if type == FIELDTYPE_DOUBLE: |
|
|
index = self.column_map[col].index |
|
|
values = [row[index] for row in self.data] |
|
|
if not values: |
|
|
return 0 |
|
|
|
|
|
max = 0 |
|
|
for value in values: |
|
|
l = len(str(value % 1)) |
|
|
if l > max: |
|
|
max = l |
|
|
if max > 2: |
|
|
return max - 2 |
|
|
else: |
|
|
return 0 |
|
|
else: |
|
|
return 0 |
|
|
|
|
396 |
def Dependencies(self): |
def Dependencies(self): |
397 |
"""Return an empty sequence. The MemoryTable doesn't depend on anything |
"""Return an empty sequence. The MemoryTable doesn't depend on anything |
398 |
""" |
""" |
416 |
|
|
417 |
# Initialise the header. Distinguish between DBFTable and others. |
# Initialise the header. Distinguish between DBFTable and others. |
418 |
for col in table.Columns(): |
for col in table.Columns(): |
|
prec = table.Precision(col.name) |
|
419 |
width = table.Width(col.name) |
width = table.Width(col.name) |
420 |
|
if col.type == FIELDTYPE_DOUBLE: |
421 |
|
prec = getattr(col, "prec", 12) |
422 |
|
else: |
423 |
|
prec = 0 |
424 |
dbf.add_field(col.name, dbflib_fieldtypes[col.type], width, prec) |
dbf.add_field(col.name, dbflib_fieldtypes[col.type], width, prec) |
425 |
|
|
426 |
for i in range(table.NumRows()): |
for i in range(table.NumRows()): |