From af2d927c1f3cadc1d8094e8ea795a0c3e6ee2c3a Mon Sep 17 00:00:00 2001 From: feng626 <1304903146@qq.com> Date: Thu, 30 Jun 2022 17:06:50 +0800 Subject: [PATCH] perf: del pandas --- apps/assets/task_handlers/backup/handlers.py | 47 +++++++++++--------- requirements/requirements.txt | 2 - 2 files changed, 25 insertions(+), 24 deletions(-) diff --git a/apps/assets/task_handlers/backup/handlers.py b/apps/assets/task_handlers/backup/handlers.py index 600f8a2da..311d8e395 100644 --- a/apps/assets/task_handlers/backup/handlers.py +++ b/apps/assets/task_handlers/backup/handlers.py @@ -1,6 +1,6 @@ import os import time -import pandas as pd +from openpyxl import Workbook from collections import defaultdict, OrderedDict from django.conf import settings @@ -48,7 +48,7 @@ class BaseAccountHandler: _fields = cls.get_header_fields(v) header_fields.update(_fields) else: - header_fields[field] = v.label + header_fields[field] = str(v.label) return header_fields @classmethod @@ -59,7 +59,7 @@ class BaseAccountHandler: data = cls.unpack_data(serializer.data) row_dict = {} for field, header_name in header_fields.items(): - row_dict[header_name] = data[field] + row_dict[header_name] = str(data[field]) return row_dict @@ -72,24 +72,24 @@ class AssetAccountHandler(BaseAccountHandler): return filename @classmethod - def create_df(cls): - df_dict = defaultdict(list) + def create_data_map(cls): + data_map = defaultdict(list) sheet_name = AuthBook._meta.verbose_name accounts = AuthBook.get_queryset().select_related('systemuser') if not accounts.first(): - return df_dict + return data_map header_fields = cls.get_header_fields(AccountSecretSerializer(accounts.first())) for account in accounts: account.load_auth() row = cls.create_row(account, AccountSecretSerializer, header_fields) - df_dict[sheet_name].append(row) - for k, v in df_dict.items(): - df_dict[k] = pd.DataFrame(v) + if sheet_name not in data_map: + data_map[sheet_name].append(list(row.keys())) + data_map[sheet_name].append(list(row.values())) logger.info('\n\033[33m- 共收集 {} 条资产账号\033[0m'.format(accounts.count())) - return df_dict + return data_map class AppAccountHandler(BaseAccountHandler): @@ -101,19 +101,19 @@ class AppAccountHandler(BaseAccountHandler): return filename @classmethod - def create_df(cls): - df_dict = defaultdict(list) + def create_data_map(cls): + data_map = defaultdict(list) accounts = Account.get_queryset().select_related('systemuser') for account in accounts: account.load_auth() app_type = account.type sheet_name = AppType.get_label(app_type) row = cls.create_row(account, AppAccountSecretSerializer) - df_dict[sheet_name].append(row) - for k, v in df_dict.items(): - df_dict[k] = pd.DataFrame(v) + if sheet_name not in data_map: + data_map[sheet_name].append(list(row.keys())) + data_map[sheet_name].append(list(row.values())) logger.info('\n\033[33m- 共收集{}条应用账号\033[0m'.format(accounts.count())) - return df_dict + return data_map handler_map = { @@ -142,15 +142,18 @@ class AccountBackupHandler: if not handler: continue - df_dict = handler.create_df() - if not df_dict: + data_map = handler.create_data_map() + if not data_map: continue filename = handler.get_filename(self.plan_name) - with pd.ExcelWriter(filename) as w: - for sheet, df in df_dict.items(): - sheet = sheet.replace(' ', '-') - getattr(df, 'to_excel')(w, sheet_name=sheet, index=False) + + wb = Workbook(filename) + for sheet, data in data_map.items(): + ws = wb.create_sheet(str(sheet)) + for row in data: + ws.append(row) + wb.save(filename) files.append(filename) timedelta = round((time.time() - time_start), 2) logger.info('步骤完成: 用时 {}s'.format(timedelta)) diff --git a/requirements/requirements.txt b/requirements/requirements.txt index 7ecf78ef1..6c509c88c 100644 --- a/requirements/requirements.txt +++ b/requirements/requirements.txt @@ -57,8 +57,6 @@ html2text==2020.1.16 pyzipper==0.3.5 python3-saml==1.12.0 websocket-client==1.2.3 -numpy==1.22.0 -pandas==1.3.5 pyjwkest==1.4.2 jsonfield2==4.0.0.post0 geoip2==4.5.0