Python类函数作为字典条目

时间:2016-07-20 18:12:44

标签: python dictionary

我正在编写一个python脚本,以便在给定MySQL查询的情况下生成Tableau Data Extract(.tde)文件。

.tde写一行需要使用set<column_type>(column_no, column_val)函数分别编写每一列。例如,将整数3写入行中的第二列将看起来像row.setInteger(1, 3)。将“Bob”这个词写在行的第4列看起来像row.setCharString(3, 'Bob')

当我遍历列时,我想根据列类型调用正确的set<column_type>()函数。我将这些功能保存在字典中:

tde_set_type_function_map = {
    Type.DATE: Row.setDate,
    Type.DATETIME: Row.setDateTime,
    Type.DOUBLE: Row.setDouble,
    Type.CHAR_STRING: Row.setCharString,
    Type.INTEGER: Row.setInteger,

}

for row in mysql:
      # Create new row
    tde_row = Row(table_def)   # Pass the table definition to the constructor
    for colno, col in enumerate(row):
        col_type = table_def.getColumnType(colno)
        setType = tde_set_type_function_map[col_type]
        print setType # e.g. <unbound method Row.setCharString>
        print type(setType) # e.g. <type 'instancemethod'>
        tde_row.setType(colno, col)
    tde_table.insert(tde_row)

tde_row.setType(colno, col)之后,我收到以下错误:

AttributeError: 'Row' object has no attribute 'setType'

我以前在字典中使用过函数,但从未在它们属于某个对象时使用过。有没有正确的方法来做到这一点?

0 个答案:

没有答案