下面我将展示我的代码的一部分(我创建的方法),我想请问您有什么建议来提高可读性并使其更美观。
所以,我所做的就是读取一个 Excel 文件并基于展示列我确实在数据库中执行了一些查询。但我的问题是三元运算符,它被大量使用并且看起来很糟糕。
def add_new_patient_identifier(self, excel_file, sheet):
patient_identifiers = self.excel_reader.read_excel_sheet(excel_file, sheet)
header = self.excel_reader.get_header(patient_identifiers)
self.openmrsclient_db.execute("DELETE FROM idgen_identifier_source")
self.openmrsclient_db.execute("DELETE FROM idgen_seq_id_gen")
for identifier in patient_identifiers.iter_rows(min_row=2):
if identifier[header['Display']].value == "Yes":
field = str(identifier[header['Field']].value) if str(identifier[header['Field']].value) != 'None' else ''
description = str(identifier[header['Source Description']].value) if str(identifier[header['Source Description']].value) != 'None' else ''
next_sequence_value = str(identifier[header['Next Sequence Value']].value) if str(identifier[header['Next Sequence Value']].value) != 'None' else ''
base_character_set = str(identifier[header['Base Character Set']].value) if str(identifier[header['Base Character Set']].value) != 'None' else ''
first_identifier_base = str(identifier[header['First Identifier Base']].value) if str(identifier[header['First Identifier Base']].value) != 'None' else ''
suffix = str(identifier[header['Suffix']].value) if str(identifier[header['Suffix']].value) != 'None' else ''
min_length = str(identifier[header['Min Length']].value) if str(identifier[header['Min Length']].value) != 'None' else ''
max_length = str(identifier[header['Max Length']].value) if str(identifier[header['Max Length']].value) != 'None' else ''
type_identifier = str(identifier[header['Type']].value) if str(identifier[header['Type']].value) != 'None' else ''
identifier_type_id = str(self.openmrsclient_db.fetch(
"SELECT patient_identifier_type_id FROM patient_identifier_type WHERE name = '" + type_identifier + "'")[0][0])
self.openmrsclient_db.execute("INSERT INTO idgen_identifier_source (uuid, name, description, identifier_type, date_created, creator) \
VALUES (uuid(),'" + field + "', '" + description + "', '" + identifier_type_id + "', now(), '1')")
identifier_source_id = str(self.openmrsclient_db.fetch("SELECT id FROM idgen_identifier_source WHERE name = '" + field + "'")[0][0])
self.openmrsclient_db.execute("INSERT INTO idgen_seq_id_gen (id, next_sequence_value, base_character_set, first_identifier_base, prefix, suffix, min_length, max_length) \
VALUES (" + identifier_source_id + "," + next_sequence_value + "," + base_character_set+","+first_identifier_base+",'" +
field+"','"+suffix+"'," + min_length + "," + max_length + ")")
答案1
我想说,首先不要转换两次:
field = str(identifier[header['Field']].value)
field = '' if field == 'None' else field
您可以将该三元和访问器放在单独的函数中:
def load(identifier, header, field):
val = str(identifier[header[field]].value)
rval = '' if val == 'None' else rval
field = load(identifier, header, 'Field')
description = load(identifier, header, 'Source Description')
ETC。
另外,关于那些 SQL 语句:您正在做的事情对于 SQL 注入来说已经成熟了。如果 openmrsclient_db 支持准备好的语句,我强烈建议您使用它们。
例如,如果有人输入“0); DROP TABLE idgen_seq_id_gen” for “max_length”,数据库可能会很乐意删除该表。我承认我从未使用过 openmrsclient_db,但当与未经消毒的输入一起使用时,它可能容易受到 SQL 注入的影响。
答案2
所以,这就是我仅对变量所做的事情(因为查询现在不是优先级)
def add_new_patient_identifier(self, excel_file, sheet):
patient_identifiers = self.excel_reader.read_excel_sheet(excel_file, sheet)
header = self.excel_reader.get_header(patient_identifiers)
self.openmrsclient_db.execute("DELETE FROM idgen_identifier_source")
self.openmrsclient_db.execute("DELETE FROM idgen_seq_id_gen")
for identifier in patient_identifiers.iter_rows(min_row=2):
if self.load(identifier, header, 'Display') == "Yes":
field = self.load(identifier, header, 'Field')
description = self.load(identifier, header, 'Source Description')
next_sequence_value = self.load(identifier, header, 'Next Sequence Value')
base_character_set = self.load(identifier, header, 'Base Character Set')
first_identifier_base = self.load(identifier, header, 'First Identifier Base')
suffix = self.load(identifier, header, 'Suffix')
min_length = self.load(identifier, header, 'Min Length')
max_length = self.load(identifier, header, 'Max Length')
type_identifier = self.load(identifier, header, 'Type')
def load(self, key, header, field):
original_cell_value = key[header[field]].value
cell_value = '' if original_cell_value == 'None' else original_cell_value
return str(cell_value)