我有一套记录。如何将所有的“if”替换为“dict”?
department_dict = {} #I'm stuck at this point.
for department in reconciliation_act.reconciliation_act_line_ids.mapped("department_id.name"):
if department == 'CPA':
....
if department == 'New MB':
new_mb_department_no_discount.insert(1, reconciliation_act.partner_id.commercial_company_name)
new_mb_department_no_discount.insert(2, reconciliation_act.currency_id.display_name)
new_mb_department_no_discount.insert(7, sales_new_mb)
new_mb_department_no_discount.insert(8, 'New MB')
if reconciliation_act.reconciliation_act_date == reconciliation_act.period_to:
new_mb_department_no_discount.insert(11, empty_string)
new_mb_department_no_discount.insert(12, 'начисление выручки')
else:
sheet.write(row, 11, reconciliation_act.period_to, date_period)
new_mb_department_no_discount.insert(13, 'корректировка выручки прошлых периодов')
for col_num, data in enumerate(new_mb_department_no_discount):
sheet.write(row, col_num, data, col_format)
sheet.write(row, 0, reconciliation_act.reconciliation_act_date, date)
sheet.write(row, 4, total_new_mb, col_format_number)
sheet.write(row, 5, date_with_customer_ref, date)
row += 1
if department == 'Alfasearch':
....
if department == 'inApp':
....我逐行为每个部门做一份报告。以下是我所做工作的一个例子
发布于 2022-07-13 14:03:08
我找到了一个解决办法,就这样做了
sales_cpa = 'Sales CPA'
sales_inapp = 'Sales Inapp'
sales_alfasearch = 'Sales Alfasearch'
sales_new_mb = 'Sales New MB'
departments_dict = {
'CPA': sales_cpa,
'New MB': sales_new_mb,
'Alfasearch': sales_alfasearch,
'inApp': sales_inapp,
}
for reconciliation_act in account_reconciliation_act_ids:
row += 1
date_from = reconciliation_act.period_from.strftime("%d.%m") if reconciliation_act.period_from else "?"
date_to = reconciliation_act.period_to.strftime("%d.%m") if reconciliation_act.period_to else "?"
customer_ref = reconciliation_act.customer_ref or ''
date_with_customer_ref = f"{date_from}-{date_to} {customer_ref}"
for department in reconciliation_act.reconciliation_act_line_ids.mapped("department_id"):
total_by_departments = sum(reconciliation_act.reconciliation_act_line_ids.filtered(
lambda rec: rec.department_id.id == department.id).mapped("price_subtotal"))
total_by_departments_with_discount = sum(reconciliation_act.reconciliation_act_discount_ids.filtered(
lambda rec: rec.department_id.id == department.id).mapped("discount_amount"))
departments = ['', '', '', '', '', '', '']
departments.insert(1, reconciliation_act.partner_id.commercial_company_name)
departments.insert(2, reconciliation_act.currency_id.display_name)
departments.insert(7, departments_dict.get(department.name))
departments.insert(8, department.name)
if reconciliation_act.reconciliation_act_date == reconciliation_act.period_to:
departments.insert(11, empty_string)
departments.insert(12, revenue_accrual)
else:
sheet.write(row, 11, reconciliation_act.period_to, date_period)
departments.insert(13, revenue_adjustment_from_previous_periods)
for col_num, data in enumerate(departments):
sheet.write(row, col_num, data, col_format)
sheet.write(row, 0, reconciliation_act.reconciliation_act_date, date)
if reconciliation_act.apply_discount:
total_by_departments = \
total_by_departments - total_by_departments_with_discount or total_by_departments
sheet.write(row, 4, total_by_departments, col_format_number)
sheet.write(row, 5, date_with_customer_ref, date)
buyer = reconciliation_act.reconciliation_act_discount_ids.filtered(
lambda rec: rec.department_id.id == department.id).buyer_id.name or ''
sheet.write(row, 9, buyer, col_format)
row += 1发布于 2022-07-12 07:24:53
def handle_NEWMB_department(new_mb_department_no_discount,
reconciliation_act,
sales_new_mb,
empty_string,
date_period,
sheet,
row,
col_format,
date,
total_new_mb,
col_format_number,
date_with_customer_ref):
new_mb_department_no_discount.insert(1, reconciliation_act.partner_id.commercial_company_name)
new_mb_department_no_discount.insert(2, reconciliation_act.currency_id.display_name)
new_mb_department_no_discount.insert(7, sales_new_mb)
new_mb_department_no_discount.insert(8, 'New MB')
if reconciliation_act.reconciliation_act_date == reconciliation_act.period_to:
new_mb_department_no_discount.insert(11, empty_string)
new_mb_department_no_discount.insert(12, 'начисление выручки')
else:
sheet.write(row, 11, reconciliation_act.period_to, date_period)
new_mb_department_no_discount.insert(13, 'корректировка выручки прошлых периодов')
for col_num, data in enumerate(new_mb_department_no_discount):
sheet.write(row, col_num, data, col_format)
sheet.write(row, 0, reconciliation_act.reconciliation_act_date, date)
sheet.write(row, 4, total_new_mb, col_format_number)
sheet.write(row, 5, date_with_customer_ref, date)
print("New MB")
def handle_CPA_department(**kwargs):
print("CPA")
department_dict = {"CPA": handle_CPA_department, "New MB": handle_NEWMB_department}
for department in reconciliation_act.reconciliation_act_line_ids.mapped("department_id.name"):
department_dict.get(department)(new_mb_department_no_discount,
reconciliation_act,
sales_new_mb,
empty_string,
date_period,
sheet,
row,
col_format,
date,
total_new_mb,
col_format_number,
date_with_customer_ref)这能解决问题吗?
发布于 2022-07-12 22:37:35
就像其他人说的,删除if语句不会使它运行得更快。但为了便于讨论,我将向您展示如何按照您的要求进行操作,实际上是使用一个迪克来选择操作:
def handle_CPA_department():
# do whatever you do with CPA ....
def handle_new_MB_department():
new_mb_department_no_discount.insert(1, reconciliation_act.partner_id.commercial_company_name)
new_mb_department_no_discount.insert(2, reconciliation_act.currency_id.display_name)
# etc...
# Write other functions to handle other departments
# This is where you map department names to functions
department_dict = {'CPA': handle_CPA_department,
'New MB': handle_new_MB_department,
# Plus other entries for other departments
}
for department in reconciliation_act.reconciliation_act_line_ids.mapped("department_id.name"):
# Call the function associated with the department
handler = department_dict[department]
handler()
# Or do it on one line: department_dict[department]()这种方法有几个缺点。首先,它不是最易读的代码--您必须注意,department_dict将部门名称映射到函数。第二,每个handle_*函数都必须使用相同的参数(正如我编写的那样,没有参数)。
一个相关的替代方法是摆脱dict,只使用if/elif
def handle_CPA_department():
# do whatever you do with CPA ....
def handle_new_MB_department():
new_mb_department_no_discount.insert(1, reconciliation_act.partner_id.commercial_company_name)
new_mb_department_no_discount.insert(2, reconciliation_act.currency_id.display_name)
# etc...
# Write other functions to handle other departments
for department in reconciliation_act.reconciliation_act_line_ids.mapped("department_id.name"):
if department == 'CPA':
handle_CPA_department()
elif department == 'New MB':
handle_new_MB_department()
# More elif's for additional departments
else:
print("Unrecognized department")这种方法更易读,因为它使用了显而易见且熟悉的if/elif。这种方法还允许您将数据作为参数传递给每个函数,并且与前面的示例不同,您传递的数据不必对每个部门都相同。
https://stackoverflow.com/questions/72948355
复制相似问题