我是开发和Django的新手,仍然在学习OOP,但我想从我创建的代码中更好地学习一些示例。我在我的视图文件中创建了一个函数。这个函数变得很长,可能我必须把它变长。我认为if语句可以写成一个函数,但不确定从哪里开始。有什么方法可以减少代码,使其更容易维护。代码如下:
def processView(request, pk):
process = ProcessInfo.objects.get(id=pk)
#bottlenecks = get_object_or_404(ProcessBottlenecks, process_info_id=process.id)
try:
details = ProcessDetails.objects.get(process_info_id=process.id)
except ProcessDetails.DoesNotExist:
details = None
try:
score = ProcessScoring.objects.get(process_score_id=pk)
except ProcessScoring.DoesNotExist:
score = None
try:
assumptions = ProcessAssumptions.objects.get(process_rel_process=process.id)
except ProcessAssumptions.DoesNotExist:
assumptions = None
color_bottleneck = None
color_quality = None
color_datacomplexity = None
color_technology = None
color_transformation = None
color_driversforchange = None
color_technology = None
#B
if score.tot_score_bottlenecks_scoring in range(0, 200):
color_bottleneck = 'badge-primary'
if score.tot_score_bottlenecks_scoring in range(100, 500):
color_bottleneck = 'badge-danger'
if score.tot_score_bottlenecks_scoring in range(500, 1000):
color_bottleneck = 'badge-warning'
if score.tot_score_bottlenecks_scoring >= 1000:
color_bottleneck = 'badge-success'
#P
if score.tot_score_dataquality in range(0, 200):
color_quality = 'badge-primary'
elif score.tot_score_dataquality in range(200, 500):
color_quality = 'badge-danger'
elif score.tot_score_dataquality in range(500, 100):
color_quality = 'badge-warning'
elif score.tot_score_dataquality >= 1000:
color_quality = 'badge-success'
#D
if score.tot_score_datacomplexity in range(0, 200):
color_datacomplexity = 'badge-primary'
if score.tot_score_datacomplexity in range(200, 500):
color_datacomplexity = 'badge-danger'
if score.tot_score_datacomplexity in range(500, 1000):
color_datacomplexity = 'badge-warning'
if score.tot_score_datacomplexity >= 1000:
color_datacomplexity = 'badge-success'
#TECHNOLOGY
if score.tot_score_technology in range(0, 200):
color_technology = 'badge-primary'
if score.tot_score_technology in range(200, 500):
color_technology = 'badge-danger'
if score.tot_score_technology in range(500, 1000):
color_technology = 'badge-warning'
if score.tot_score_technology >= 1000:
color_technology = 'badge-success'
#T
if score.tot_score_transformation in range(0, 200):
color_transformation = 'badge-primary'
if score.tot_score_transformation in range(200, 500):
color_transformation = 'badge-danger'
if score.tot_score_transformation in range(500, 1000):
color_transformation = 'badge-warning'
if score.tot_score_transformation >= 1000:
color_transformation = 'badge-success'
#DRIVERSFORCHANGE
if score.tot_score_driversforchange in range(0, 200):
color_driversforchange = 'badge-primary'
if score.tot_score_driversforchange in range(200, 500):
color_driversforchange = 'badge-danger'
if score.tot_score_driversforchange in range(500, 1000):
color_driversforchange = 'badge-warning'
if score.tot_score_driversforchange >= 1000:
color_driversforchange = 'badge-success'
#S
if score.tot_score_scalability in range(0, 200):
color_scalability = 'badge-primary'
if score.tot_score_scalability in range(200, 500):
color_scalability = 'badge-danger'
if score.tot_score_scalability in range(500, 1000):
color_scalability = 'badge-warning'
if score.tot_score_scalability >= 1000:
color_scalability = 'badge-success'
context ={
'process':process,
'details':details,
'score':score,
'assumptions':assumptions,
'color_bottleneck' : color_bottleneck,
'color_quality':color_quality,
'color_datacomplexity':color_datacomplexity,
'color_technology':color_technology,
'color_transformation' : color_transformation,
'color_driversforchange':color_driversforchange,
'color_scalability' : color_scalability
}
return render(request, 'process/process_view.html', context)这是来自forms.py的另一个例子。在这里,我有重复的表单字段,并创建了数百行
swivel= forms.CharField(
label='Swivel Activities',
required=False,
help_text='Are swivel chair activities present?',
widget=forms.Select(choices=CHECK))
process_objectives= forms.CharField(
label='Clear Process Objectives',
required=False,
help_text='Are process outcomes objective and measurable?',
widget=forms.Select(choices=CHECK))
stable_rules= forms.CharField(
label='Stable Rules',
required=False,
help_text='Are the decision rules governing the process stable?',
widget=forms.Select(choices=CHECK))
.....发布于 2020-06-21 00:58:33
最小化代码的一种方法是编写一个函数,您可以调用该函数来获取颜色。您似乎总是检查相同的范围和颜色,所以只需使用分数变量并创建可重用的函数即可。
def get_color(score):
if score in range(0, 200):
color = 'badge-primary'
elif score in range(100, 500):
color = 'badge-danger'
elif score in range(500, 1000):
color = 'badge-warning'
else:
color = 'badge-success'
return color
color_scalability = get_color(score.tot_score_scalability)
color_transformation = get_color(score.tot_score_transformation)
...当你重复你自己的时候,试着找到一个通用的方法。我希望这会有一点帮助,如果你有任何问题,请提问。
发布于 2020-06-21 01:30:05
您可以将if块分解为一个函数:
badge_selecting(attribute):
if attribute in range(0, 200):
color = 'badge-primary'
elif attribute in range(100, 500):
color = 'badge-danger'
elif attribute in range(500, 1000):
color = 'badge-warning'
elif attribute >= 1000:
color = 'badge-success'
return color你可以这样调用这个函数:
color_bottleneck = badge_selection(score.tot_score_bottlenecks_scoring)对于每个属性。
第二个问题的答案是相同的,因此您可以创建一个函数,例如:
def form_creation(label, help_text):
form = forms.CharField(
label=label,
required=False,
help_text=help_text,
widget=forms.Select(choices=CHECK))
return form如果您需要其他字段的变体,但这些属性是您希望在大多数情况下使用的属性,则可以使用默认参数值并创建如下函数:
def form_creation(label, help_text, required=False, widget=forms.Select(choices=CHECK)):所以你不需要把这些值放到所有的表单中,只需要改变它们的值即可。
https://stackoverflow.com/questions/62488720
复制相似问题