我正在尝试检查这些值是否相等,但不确定是否可以缩短在此处生成的代码行数。我的目标是返回一个明显的错误,可以在Ajax调用中返回该错误,以便前端可以向用户发送消息。
下面的代码是我到目前为止已经尝试过的代码,但是我只是不确定如何继续减少此代码。
def check_ip_conflicts(primary_dns, secondary_dns, ip_start, ip_end, subnet_mask, gateway, left1bits):
primary_dns = list(map(int, primary_dns.split(".")))
secondary_dns = list(map(int, secondary_dns.split(".")))
ip_start = list(map(int, ip_start.split(".")))
ip_end = list(map(int, ip_end.split(".")))
subnet = list(map(int, subnet_mask.split(".")))
gateway = list(map(int, gateway.split(".")))
network = []
total_ips = num_ips_in_range(left1bits)
###bit wise & may have to change from ip_start to Primary DNS. Ask Dan possibly
for i in range(4):
network.append(ip_start[i] & subnet[i])
if primary_dns == secondary_dns:
return "PRIMARY DNS EQUALS SECONDARY DNS ERROR"
if primary_dns == subnet:
return "PRIMARY DNS EQUALS SUBNET ERROR"
if primary_dns == gateway:
return "PRIMARY DNS EQUALS GATEWAY ERROR"
if primary_dns == ip_start:
return "PRIMARY DNS EQUALS STARTING IP ERROR"
if primary_dns == ip_end:
return "PRIMARY DNS EQUALS ENDING IP ERROR"
if secondary_dns == subnet:
return "SECONDARY DNS EQUALS SUBNET ERROR"
if secondary_dns == gateway:
return "SECONDARY DNS EQUALS GATEWAY ERROR"
if secondary_dns == ip_start:
return "SECONDARY DNS EQUALS STARTING IP ERROR"
if secondary_dns == ip_end:
return "SECONDARY DNS EQUALS ENDING IP ERROR"
if subnet == gateway:
return "SUBNET EQUALS GATEWAY ERROR"
if subnet == ip_start:
return "PRIMARY DNS EQUALS STARTING IP ERROR"
if subnet == ip_end:
return "PRIMARY DNS EQUALS ENDING IP ERROR"
if gateway == ip_start:
return "GATEWAY EQUALS STARTING IP ERROR"
if gateway == ip_end:
return "GATEWAY EQUALS ENDING IP ERROR"
答案 0 :(得分:1)
将所有值放入字典中。然后,您可以遍历字典以查找任何冲突。
def check_ip_conflicts(primary_dns, secondary_dns, ip_start, ip_end, subnet_mask, gateway, left1bits):
primary_dns = list(map(int, primary_dns.split(".")))
secondary_dns = list(map(int, secondary_dns.split(".")))
ip_start = list(map(int, ip_start.split(".")))
ip_end = list(map(int, ip_end.split(".")))
subnet = list(map(int, subnet_mask.split(".")))
gateway = list(map(int, gateway.split(".")))
network = []
total_ips = num_ips_in_range(left1bits)
###bit wise & may have to change from ip_start to Primary DNS. Ask Dan possibly
for i in range(4):
network.append(ip_start[i] & subnet[i])
checks = {"PRIMARY DNS": primary_dns, "SECONDARY DNS": secondary_dns, "SUBNET": subnet, "GATEWAY": gateway, "STARTING IP": ip_start, "ENDING IP": ip_end}
for type1, value1 in checks.items():
for type2, value2 in checks.items():
if type1 != type2 and value1 == value2:
return f"{type1} EQUALS {type2} ERROR"
return "NO ERROR"
答案 1 :(得分:0)
一种使事情变得更短,更友好的方法是
def check_ip_conflicts(primary_dns, secondary_dns, ip_start, ip_end, subnet_mask, gateway, left1bits):
primary_dns = [int(x) for x in primary_dns.split(".")]
secondary_dns = [int(x) for x in secondary_dns.split(".")]
ip_start = [int(x) for x in ip_start.split(".")]
ip_end = [int(x) for x in ip_end.split(".")]
subnet = [int(x) for x in subnet_mask.split(".")]
gateway = [int(x) for x in gateway.split(".")]
network = []
total_ips = num_ips_in_range(left1bits)
###bit wise & may have to change from ip_start to Primary DNS. Ask Dan possibly
for i in range(4):
network.append(ip_start[i] & subnet[i])
return [
error
for (flag, error) in [
(primary_dns == secondary_dns, "PRIMARY DNS EQUALS SECONDARY DNS ERROR"),
(primary_dns == subnet, "PRIMARY DNS EQUALS SUBNET ERROR"),
(primary_dns == gateway, "PRIMARY DNS EQUALS GATEWAY ERROR"),
(primary_dns == ip_start, "PRIMARY DNS EQUALS STARTING IP ERROR"),
(primary_dns == ip_end, "PRIMARY DNS EQUALS ENDING IP ERROR"),
(secondary_dns == subnet, "SECONDARY DNS EQUALS SUBNET ERROR"),
(secondary_dns == gateway, "SECONDARY DNS EQUALS GATEWAY ERROR"),
(secondary_dns == ip_start, "SECONDARY DNS EQUALS STARTING IP ERROR"),
(secondary_dns == ip_end, "SECONDARY DNS EQUALS ENDING IP ERROR"),
(subnet == gateway, "SUBNET EQUALS GATEWAY ERROR"),
(subnet == ip_start, "PRIMARY DNS EQUALS STARTING IP ERROR"),
(subnet == ip_end, "PRIMARY DNS EQUALS ENDING IP ERROR"),
(gateway == ip_start, "GATEWAY EQUALS STARTING IP ERROR"),
(gateway == ip_end, "GATEWAY EQUALS ENDING IP ERROR"),
]
if flag
]
这会更改函数以返回错误列表,而不仅是遇到的第一个错误。