From cfe4e93dc7c6fa231fc6ce20a31ad0828475c3e1 Mon Sep 17 00:00:00 2001 From: Denis Ivanov Date: Tue, 3 Dec 2024 13:21:36 +0700 Subject: [PATCH] refactor if/elif/else construction --- drf_excel/fields.py | 19 +++++++++---------- drf_excel/renderers.py | 9 ++++----- 2 files changed, 13 insertions(+), 15 deletions(-) diff --git a/drf_excel/fields.py b/drf_excel/fields.py index f6ad915..a80060c 100644 --- a/drf_excel/fields.py +++ b/drf_excel/fields.py @@ -56,7 +56,7 @@ def init_value(self, value): def custom_mapping(self): if type(self.mapping) is str: return self.value.get(self.mapping) - elif callable(self.mapping): + if callable(self.mapping): return self.mapping(self.value) return self.value @@ -89,9 +89,9 @@ def init_value(self, value): with contextlib.suppress(Exception): if isinstance(self.drf_field, IntegerField) and type(value) is not int: return int(value) - elif isinstance(self.drf_field, FloatField) and type(value) is not float: + if isinstance(self.drf_field, FloatField) and type(value) is not float: return float(value) - elif ( + if ( isinstance(self.drf_field, DecimalField) and type(value) is not Decimal ): return Decimal(value) @@ -122,7 +122,7 @@ def _parse_date(self, value, setting_format, iso_parse_func): parsed_datetime = datetime.datetime.strptime(value, parse_format) if isinstance(self.drf_field, TimeField): return parsed_datetime.time() - elif isinstance(self.drf_field, DateField): + if isinstance(self.drf_field, DateField): return parsed_datetime.date() return parsed_datetime @@ -136,12 +136,12 @@ def init_value(self, value): return self._parse_date( value, "DATETIME_FORMAT", parse_datetime ).replace(tzinfo=None) - elif ( + if ( isinstance(self.drf_field, DateField) and type(value) is not datetime.date ): return self._parse_date(value, "DATE_FORMAT", parse_date) - elif ( + if ( isinstance(self.drf_field, TimeField) and type(value) is not datetime.time ): @@ -177,10 +177,9 @@ def prep_value(self) -> Any: ): # array of array; write as json return json.dumps(self.value, ensure_ascii=False) - else: - # Flatten the array into a comma separated string to fit - # in a single spreadsheet column - return self.list_sep.join(map(str, self.value)) + # Flatten the array into a comma separated string to fit + # in a single spreadsheet column + return self.list_sep.join(map(str, self.value)) class XLSXBooleanField(XLSXField): diff --git a/drf_excel/renderers.py b/drf_excel/renderers.py index ba29313..9eba07e 100644 --- a/drf_excel/renderers.py +++ b/drf_excel/renderers.py @@ -270,8 +270,7 @@ def _get_label(parent_label, label_sep, obj): if parent_label else str(v.label) ) - else: - return False + return False _header_dict = {} _fields = serializer.get_fields() @@ -378,11 +377,11 @@ def _drf_to_xlsx_field(self, key, value) -> XLSXField: if isinstance(field, BooleanField): return XLSXBooleanField(boolean_display=self.boolean_display, **kwargs) - elif isinstance(field, (IntegerField, FloatField, DecimalField)): + if isinstance(field, (IntegerField, FloatField, DecimalField)): return XLSXNumberField(**kwargs) - elif isinstance(field, (DateTimeField, DateField, TimeField)): + if isinstance(field, (DateTimeField, DateField, TimeField)): return XLSXDateField(**kwargs) - elif ( + if ( isinstance(field, ListField) or isinstance(value, Iterable) and not isinstance(value, str)