有没有一种方法可以压缩此代码,而不是使用大的if-else块?

时间:2019-06-28 16:05:33

标签: python openpyxl

编写一个将样式应用于单元格范围的函数,想知道是否存在某种元编程方式来避免在此处具有较大的if-else块。就像能够从要从Type-> attribute映射的字典中获取要设置的属性一样。

def _apply_style_to_range(self, cell_range, style):
    for row in self.worksheet[cell_range]:
        for cell in row:
            if isinstance(style, openpyxl.styles.Alignment):
                self.worksheet[cell.coordinate].alignment = style
            elif isinstance(style, openpyxl.styles.Border):
                self.worksheet[cell.coordinate].border = style
            elif isinstance(style, openpyxl.styles.PatternFill):
                self.worksheet[cell.coordinate].fill = style
            elif isinstance(style, openpyxl.styles.Font):
                self.worksheet[cell.coordinate].font = style
            elif style in Numbers.all_number_formats: # these are custom strings I came up with so no defining type
                self.worksheet[cell.coordinate].number_format = style
            else:
                raise ValueError('unhandled style {} unable to be applied'.format(style))

1 个答案:

答案 0 :(得分:0)

这就是我能想到的

$userresponses->whereBetween('created_at',[$request->start_date,$request->end_date])
->orWhere('created_at', '=', $request->start_date)
->orWhere('created_at', '=', $request->end_date);