将名称与工作表上的魔术位置相关联

时间:2018-10-16 15:09:34

标签: python class factory xlrd

我正在编写一个python类,以基于默认的xls工作表布局解析多个xls工作表。我有一个可行的方法,但我正在寻找一种基于Python的,更可靠的方法,用于基于魔术单元格位置的查找表查找单元格值。例如:

def magic(template, pars, ws):
    ''' return the magic page paramameter
    '''
    return pars[3] if template < pars[2] else ws.cell(pars[0],pars[1]).value

# code within class definition (magic is outside of class)
# MAGIC_CELLS contain the row,col of a named cell along with the template version
# where it was added and the default in case an earlier file version is parsed        
MAGIC_CELLS = {'status'       : (1, 1, 205,'unknown'),
               'backanno'     : (5, 4, 205, True),
               'is_top_level' : (2, 1, 205, True)
              }
self.fns ={key:lambda ws: magic(self.template, pars, ws) for key,pars in MAGIC_CELLS.items()}

我想将fns词典转换为类中的真实函数,因此可以按以下方式调用它们:

specs = IpConfTable(self.status(ws), self.backanno(ws), self.is_top_level(ws))

而不是笨拙的:

specs = IpConfTable(self.fns['status'](ws), self.fns['backanno'](ws), self.fns['is_top_level'](ws))

我愿意采用其他任何一种方法来将魔术数字部分收集在一个地方并保持通话的可读性。

0 个答案:

没有答案