I don't know why the output in wrapped function hasn't been printed among decorator, my codes as below:
from functools import wraps
checkresultfile = './checkres1.log'
def log_detection_res(detect_items=''):
def process_reminder_decorator(check_func):
@wraps(check_func)
def addingReminder(*args, **kwargs):
process_begin_reminder = "=" * 10 detect_items "begin" "=" * 10
process_stop_reminder = "=" * 10 detect_items "end" "=" * 10
with open(checkresultfile, 'a') as res_file:
res_file.write(process_begin_reminder '\n')
check_func(*args, **kwargs)
res_file.write(process_stop_reminder '\n')
return check_func
return addingReminder
return process_reminder_decorator
@log_detection_res(detect_items='srlg conflict detection')
def relay_srlg_conflict_detect():
tplt = "{0:^30}\t{1:^20}\t{2:^20}\t{3:^10}\t{4:^20}\t{5:^20}\t{6:^20}\t{7:^20}\t{8:^10}"
output_file.write(
tplt.format("Conflict_Index", "INGRESS", "EGRESS", "Index", "Fault_Node", "Fault_link", "Router_Node",
"Route_link", "SRLG") "\n")
with open(checkresultfile, 'a') as output_file:
relay_srlg_conflict_detect()
The output in file is:
==========srlg conflict detection begin==========
==========srlg conflict detection end==========
Conflict_Index INGRESS EGRESS Index Fault_Node Fault_link Router_Node Route_link SRLG
what I am expecting is:
==========srlg conflict detection begin==========
Conflict_Index INGRESS EGRESS Index Fault_Node Fault_link Router_Node Route_link SRLG
==========srlg conflict detection end==========
CodePudding user response:
your problem is that you are trying to open on the same time the same file
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import time
from functools import wraps
checkresultfile = './checkres1.log'
def log_detection_res(detect_items=''):
def process_reminder_decorator(check_func):
@wraps(check_func)
def addingReminder(*args, **kwargs):
process_begin_reminder = "=" * 10 detect_items "begin" "=" * 10
process_stop_reminder = "=" * 10 detect_items "end" "=" * 10
with open(checkresultfile, 'a') as res_file:
res_file.write(process_begin_reminder '\n')
check_func(output_file=res_file, *args, **kwargs)
res_file.write(process_stop_reminder '\n')
return check_func
return addingReminder
return process_reminder_decorator
@log_detection_res(detect_items='srlg conflict detection')
def relay_srlg_conflict_detect(output_file):
tplt = "{0:^30}\t{1:^20}\t{2:^20}\t{3:^10}\t{4:^20}\t{5:^20}\t{6:^20}\t{7:^20}\t{8:^10}"
output_file.write(
tplt.format("Conflict_Index", "INGRESS", "EGRESS", "Index", "Fault_Node", "Fault_link", "Router_Node",
"Route_link", "SRLG") "\n")
relay_srlg_conflict_detect()
output
==========srlg conflict detectionbegin==========
Conflict_Index INGRESS EGRESS Index Fault_Node Fault_link Router_Node Route_link SRLG
==========srlg conflict detectionend==========
CodePudding user response:
The order you open the file is wrong.
Try this:
from functools import wraps
checkresultfile = './checkres1.log'
def log_detection_res(detect_items=''):
def process_reminder_decorator(check_func):
@wraps(check_func)
def addingReminder(*args, **kwargs):
with open(checkresultfile, 'a') as res_file:
res_file.write('=begin=' '\n')
check_func(*args, **kwargs)
with open(checkresultfile, 'a') as res_file:
res_file.write('=end=' '\n')
return check_func
return addingReminder
return process_reminder_decorator
@log_detection_res(detect_items='srlg conflict detection')
def relay_srlg_conflict_detect():
with open(checkresultfile, 'a') as output_file:
output_file.write('ctx' "\n")
relay_srlg_conflict_detect()