使用merge_sort测试logging.debug
In [64]: %paste
import logging
logging.basicConfig(level=logging.DEBUG, format="%(asctime)s - %(funcName)s - %(message)s")
def merge_sort(A):
if len(A) <= 1:
return A
mid = len(A) // 2
left = merge_sort(A[:mid])
logging.debug(f"left is {str(left)}")
right = merge_sort(A[mid:])
logging.debug(f"right is {str(right)}")
result = []
j, k = 0, 0
while j < len(left) and k < len(right):
if left[j] <= right[k]:
result.append(left[j])
j += 1
else:
result.append(right[k])
k += 1
while j < len(left):
result.append(left[j])
j += 1
while k < len(right):
result.append(right[k])
k += 1
logging.debug(f"result is {str(result)}.")
return result
## -- End pasted text --
运行
In [70]: random.shuffle(A)
In [71]: A
Out[71]: [23, 9, 2, 3, 5, 9]
In [72]: merge_sort(A)
2018-12-18 10:31:54,588 - DEBUG - left is [23]
2018-12-18 10:31:54,588 - DEBUG - left is [9]
2018-12-18 10:31:54,589 - DEBUG - right is [2]
2018-12-18 10:31:54,589 - DEBUG - result is [2, 9].
2018-12-18 10:31:54,589 - DEBUG - right is [2, 9]
2018-12-18 10:31:54,589 - DEBUG - result is [2, 9, 23].
2018-12-18 10:31:54,589 - DEBUG - left is [2, 9, 23]
2018-12-18 10:31:54,589 - DEBUG - left is [3]
2018-12-18 10:31:54,589 - DEBUG - left is [5]
2018-12-18 10:31:54,589 - DEBUG - right is [9]
2018-12-18 10:31:54,589 - DEBUG - result is [5, 9].
2018-12-18 10:31:54,589 - DEBUG - right is [5, 9]
2018-12-18 10:31:54,589 - DEBUG - result is [3, 5, 9].
2018-12-18 10:31:54,589 - DEBUG - right is [3, 5, 9]
2018-12-18 10:31:54,589 - DEBUG - result is [2, 3, 5, 9, 9, 23].
Out[72]: [2, 3, 5, 9, 9, 23]
请注意,funcName
是DEBUG而不是merge_sort
。
这怎么可能?