我的测试类生成了所需的绘图,但我每次都必须手动停止执行 - 控制台继续显示“实例化测试”;谁能发现为什么执行永远不会停止?任何关于增加我的代码的“Pythonic-ness”的提示也将不胜感激!
(在 Mac OS X 10.11.6 上的 PyCharm CE 2016.2.1 中运行的 Python 3.5)
# A program to test various sorting algorithms. It generates random lists of various sizes,
# sorts them, and then tests that:
# a. the list is in ascending order
# b. the set of elements is the same as the original list
# c. record the time taken
import random
import timeit
from unittest import TestCase
import matplotlib.pyplot as plt
from Sorter import insertionsort, mergesort, quicksort
class TestSort(TestCase):
def test_sorting(self):
times_insertionsort = [] # holds the running times for insertion sort
times_quicksort = [] # holds the running times for quick sort
times_mergesort = [] # holds the running times for merge sort
lengths = [] # holds the array lengths
# determine the number of lists to be created
for i in range(0, 13):
# initialise a new empty list
pre_sort = []
# determine the list's length, then populate the list with 'random' data
for j in range(0, i * 100):
pre_sort.append(random.randint(0, 1000))
# record the length of the list
lengths.append(len(pre_sort))
# record the time taken by quicksort to sort the list
start_time = timeit.default_timer()
post_quicksort = quicksort(pre_sort)
finish_time = timeit.default_timer()
times_quicksort.append((finish_time - start_time) * 1000)
# record the time taken by insertionsort to sort the list
start_time = timeit.default_timer()
post_insertionsort = insertionsort(pre_sort)
finish_time = timeit.default_timer()
times_insertionsort.append((finish_time - start_time) * 1000)
# record the time taken by mergesort to sort the list
start_time = timeit.default_timer()
post_mergesort = mergesort(pre_sort)
finish_time = timeit.default_timer()
times_mergesort.append((finish_time - start_time) * 1000)
# check that:
# a. the list is in ascending order
# b. the set of elements is the same as the original list
for k in range(0, len(pre_sort) - 1):
self.assertTrue(post_insertionsort[k] in pre_sort)
self.assertTrue(post_insertionsort[k] <= post_insertionsort[k + 1])
self.assertTrue(post_mergesort[k] == post_insertionsort[k])
self.assertTrue(post_mergesort[k] == post_quicksort[k])
# plot the results
plt.plot(lengths, times_insertionsort, 'r')
plt.plot(lengths, times_quicksort, 'g')
plt.plot(lengths, times_mergesort, 'b')
plt.xlabel('List size')
plt.ylabel('Execution time (ms)')
plt.show()
您需要关闭显示绘图的窗口,以便脚本(在本例中为您的测试)继续/完成/完成。 来自文档,强调我的:
当您想在显示器上查看绘图时,用户界面后端将需要启动 GUI 主循环。这就是
的作用。它告诉 matplotlib 启动迄今为止创建的所有图形窗口并启动主循环。 因为这个主循环默认是阻塞的(即脚本执行被暂停),所以你应该只在每个脚本最后调用一次。关闭最后一个窗口后将恢复脚本执行。 因此,如果您仅使用 matplotlib 生成图像并且不需要用户界面窗口,则不需要调用 show(请参阅生成图像而不出现窗口和什么)是后端吗?)。show()
或者不要使用
show()
进行测试,而只是生成一个图像,以便稍后进行验证。
我在 Django 中也遇到了同样的错误。我发现其中一项迁移未应用。
实际上,我在 Pycharm 中的以下脚本遇到了同样的问题,它停留在实例化测试上:
def product_except_itself(array: list[int]) -> list[int]:
n = len(array)
result_array = [1] * n
result_array[1] = array[0]
for i in range(1, n):
result_array[i] = array[i - 1] * result_array[i - 1]
temporary = array[n - 1]
for i in range(1, n):
result_array[n - 1 - i] *= temporary
temporary *= array[n - 1 - i]
return result_array
array = [int(x) for x in input().split()]
print(product_except_itself(array))
不幸的是,我是如此愚蠢和沮丧,忘记了不将输入包含到主包装器中中,如下所示:
# previous code
if __name__ == '__main__':
array = [int(x) for x in input().split()]
print(product_except_itself(array))
因此,任何遇到相同问题的人请确保所有从控制台的输入到您的函数都像这样包装