我真的可以在如何实现getitem、iter方法或生成器函数方面使用一些帮助来对我创建的类和类容器进行排序。
我创建了一个具有 send_time (datetime) 和 period_length (int) 属性的 Report 类。我还为 Reports 创建了一个 ReportDeque 容器,它继承自 collections.deque。
我需要为类和它的容器添加排序功能。
到目前为止,我的排序工作正常,但希望 list.sort() 样式正常工作。
sortedList = sorted(list, key=lambda report: report.send_time)
sortedDeque = sorted(deque, key=lambda report: report.send_time)
我正在努力实现ReportDeque中的 getitem 以及 ReportDeque 中的iter和 next 方法。我似乎无法找到使所有这些工作所需的示例。
也许应该使用生成器函数对 collections.deque 容器进行排序。如果有各种生成器以不同的方式对双端队列进行排序,那就太好了。
下面是我的测试用例。要在以下代码中运行单元测试,请键入:
python -m unittest test_reports
输出在这篇文章的末尾。
提前致谢...
------------------- test_reports.py 片段 --------------
#!/usr/bin/env python
from datetime import datetime
from collections import deque
import unittest
import inspect
class Report(object):
"""
Contains all information contained in a report
"""
def __init__(self, periodStart, periodEnd, sendTime):
self.period_start = periodStart
self.period_end = periodEnd
self.send_time = sendTime
self.send_timestamp = (sendTime - datetime(1970, 1, 1)).total_seconds()
self.period_length = (periodEnd - periodStart).total_seconds()
#def __getitem__(self, key):
class ReportDeque(deque):
"""
Container for processing, sorting Report objects
"""
#def __iter__(self)
#def next(self)
class TestReports(unittest.TestCase):
def setUp(self):
self.list = []
self.deque = ReportDeque()
# send_time 12/4/13, day length report
report = Report(datetime(2013, 12, 3, 0), datetime(2013, 12, 3, 23), datetime(2013, 12, 4, 0))
self.list.append(report)
self.deque.append(report)
# send_time 12/3/13, day length report
report = Report(datetime(2013, 12, 2, 0), datetime(2013, 12, 2, 23), datetime(2013, 12, 3, 0))
self.list.append(report)
self.deque.append(report)
# send_time 12/2/13, day length report
report = Report(datetime(2013, 12, 1, 0), datetime(2013, 12, 1, 23), datetime(2013, 12, 2, 0))
self.list.append(report)
self.deque.append(report)
# sorted with key function works
self.sortedList = sorted(self.list, key=lambda report: report.send_time)
self.sortedDeque = sorted(self.deque, key=lambda report: report.send_time)
def test_sort_deque_send_time(self):
self.print_inspect()
# deque does not have sort method. How to sort it?
self.deque.sort()
firstReport = self.deque[0]
print "send_time {} period_length {}".format(firstReport.send_time, firstReport.period_length)
self.assertEqual(firstReport.send_time, datetime(2013, 12, 2, 0, 0, 0, 0))
def test_sort_list_send_time(self):
self.print_inspect()
# list.sort() not working. How to implement __get_item___?
self.list.sort()
firstReport = self.list[0]
print "send_time {} period_length {}".format(firstReport.send_time, firstReport.period_length)
self.assertEqual(firstReport.send_time, datetime(2013, 12, 2, 0, 0, 0, 0))
def test_sorted_deque_send_time(self):
self.print_inspect()
firstReport = self.sortedDeque[0]
print "send_time {} period_length {}".format(firstReport.send_time, firstReport.period_length)
self.assertEqual(firstReport.send_time, datetime(2013, 12, 2, 0, 0, 0, 0))
def test_sorted_list_send_time(self):
self.print_inspect()
firstReport = self.sortedList[0]
print "send_time {} period_length {}".format(firstReport.send_time, firstReport.period_length)
self.assertEqual(firstReport.send_time, datetime(2013, 12, 2, 0, 0, 0, 0))
def print_inspect(self):
calling_function = inspect.stack()[1][3]
print "\nin {}()".format(calling_function)
if __name__ == "__main__":
unittest.main()
------------------- test_reports.py 片段 --------------
$ python -m unittest test_reports
in test_sort_deque_send_time()
E
in test_sort_list_send_time()
send_time 2013-12-04 00:00:00 period_length 82800.0
F
in test_sorted_deque_send_time()
send_time 2013-12-02 00:00:00 period_length 82800.0
.
in test_sorted_list_send_time()
send_time 2013-12-02 00:00:00 period_length 82800.0
.
======================================================================
ERROR: test_sort_deque_send_time (test_reports.TestReports)
----------------------------------------------------------------------
Traceback (most recent call last):
File "test_reports.py", line 51, in test_sort_deque_send_time
self.deque.sort()
AttributeError: 'ReportsDeque' object has no attribute 'sort'
======================================================================
FAIL: test_sort_list_send_time (test_reports.TestReports)
----------------------------------------------------------------------
Traceback (most recent call last):
File "test_reports.py", line 62, in test_sort_list_send_time
self.assertEqual(firstReport.send_time, datetime(2013, 12, 2, 0, 0, 0, 0))
AssertionError: datetime.datetime(2013, 12, 4, 0, 0) != datetime.datetime(2013, 12, 2, 0, 0)
----------------------------------------------------------------------
Ran 4 tests in 0.011s
FAILED (failures=1, errors=1)