2 |
# Authors: |
# Authors: |
3 |
# Bernhard Herzog <[email protected]> |
# Bernhard Herzog <[email protected]> |
4 |
# Jan-Oliver Wagner <[email protected]> |
# Jan-Oliver Wagner <[email protected]> |
5 |
|
# Frank Koormann <[email protected]> |
6 |
# |
# |
7 |
# This program is free software under the GPL (>=v2) |
# This program is free software under the GPL (>=v2) |
8 |
# Read the file COPYING coming with Thuban for details. |
# Read the file COPYING coming with Thuban for details. |
230 |
|
|
231 |
# DBF specific interface parts. |
# DBF specific interface parts. |
232 |
|
|
233 |
|
def Width(self, col): |
234 |
|
"""Return column width""" |
235 |
|
return self.column_map[col].width |
236 |
|
|
237 |
def Destroy(self): |
def Destroy(self): |
238 |
self.dbf.close() |
self.dbf.close() |
239 |
self.dbf = None |
self.dbf = None |
351 |
return min(values), max(values) |
return min(values), max(values) |
352 |
|
|
353 |
def UniqueValues(self, col): |
def UniqueValues(self, col): |
354 |
"""Return a sorted list of all unique values in the column col""" |
"""Return a sorted list of all unique values in the column col |
355 |
|
|
356 |
|
col can be either column index or name. |
357 |
|
""" |
358 |
dict = {} |
dict = {} |
359 |
|
|
360 |
for i in range(self.NumRows()): |
for i in range(self.NumRows()): |
365 |
values.sort() |
values.sort() |
366 |
return values |
return values |
367 |
|
|
368 |
|
def Width(self, col): |
369 |
|
"""Return the maximum width of values in the column |
370 |
|
|
371 |
|
The return value is the the maximum length of string |
372 |
|
representation of the values in the column (represented by index |
373 |
|
or name). |
374 |
|
""" |
375 |
|
max = 0 |
376 |
|
|
377 |
|
type = self.column_map[col].type |
378 |
|
index = self.column_map[col].index |
379 |
|
values = [row[index] for row in self.data] |
380 |
|
if not values: |
381 |
|
return None |
382 |
|
|
383 |
|
if type == FIELDTYPE_DOUBLE: |
384 |
|
format = "%.12f" |
385 |
|
elif type == FIELDTYPE_INT: |
386 |
|
format = "%d" |
387 |
|
else: |
388 |
|
format = "%s" |
389 |
|
for value in values: |
390 |
|
l = len(format % value) |
391 |
|
if l > max: |
392 |
|
max = l |
393 |
|
|
394 |
|
return max |
395 |
|
|
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 |
""" |
""" |
404 |
# TODO: Allow values to be a dictionary and write the single |
# TODO: Allow values to be a dictionary and write the single |
405 |
# fields that are specified. |
# fields that are specified. |
406 |
self.data[record] = values |
self.data[record] = values |
407 |
|
|
408 |
|
|
409 |
|
def table_to_dbf(table, filename): |
410 |
|
"""Create the dbf file filename from the table""" |
411 |
|
dbf = dbflib.create(filename) |
412 |
|
|
413 |
|
dbflib_fieldtypes = {FIELDTYPE_STRING: dbflib.FTString, |
414 |
|
FIELDTYPE_INT: dbflib.FTInteger, |
415 |
|
FIELDTYPE_DOUBLE: dbflib.FTDouble} |
416 |
|
|
417 |
|
# Initialise the header. Distinguish between DBFTable and others. |
418 |
|
for col in table.Columns(): |
419 |
|
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) |
425 |
|
|
426 |
|
for i in range(table.NumRows()): |
427 |
|
record = table.ReadRowAsDict(i) |
428 |
|
dbf.write_record(i, record) |
429 |
|
dbf.close() |
430 |
|
|
431 |
|
def table_to_csv(table, filename): |
432 |
|
"""Export table to csv file.""" |
433 |
|
|
434 |
|
file = open(filename,"w") |
435 |
|
columns = table.Columns() |
436 |
|
if columns: |
437 |
|
header = "#%s" % columns[0].name |
438 |
|
for col in columns[1:]: |
439 |
|
header = header + ",%s" % col.name |
440 |
|
header = header + "\n" |
441 |
|
file.write(header) |
442 |
|
|
443 |
|
for i in range(table.NumRows()): |
444 |
|
record = table.ReadRowAsDict(i) |
445 |
|
if len(record): |
446 |
|
line = "%s" % record[columns[0].name] |
447 |
|
for col in columns[1:]: |
448 |
|
line = line + ",%s" % record[col.name] |
449 |
|
line = line + "\n" |
450 |
|
file.write(line) |
451 |
|
file.close() |
452 |
|
|