我真的可以使用一些帮助来实现 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)
我正在努力在Report中实现 getitem ,以及 iter ,在ReportDeque中实现下一个方法。我似乎无法找到我需要的所有这些工作的例子。
也许应该使用生成器函数来对collections.deque容器进行排序。 有各种各样的生成器以不同的方式对deque进行排序会很好。
以下是我的测试用例。要在下面的代码中运行unittest,请输入:
python -m unittest test_reports
输出结束于此帖。
提前致谢...
------------------- test_reports.py snip ------------------------ -
#!/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 snip ------------------------ -
$ 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)
答案 0 :(得分:2)
首先,您需要使报表对象具有可比性,因此您可以在没有明确键的情况下进行订购。您可能应该阅读丰富的比较,但__cmp__
可以解决这个问题。
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 __cmp__(self, other):
return cmp(self.send_time, other.send_time)
在测试list.sort()
时,这就是让测试通过的全部内容。 sorted(list)
和sorted(deque)
的测试也应该有效,但有一个问题。由于您正在寻求__getitem__
实施方面的帮助,我认为您认为sorted()
正在进行此类排序,并会对您的双端队列进行排序。这不是它的工作原理。 sorted(iterable)
将返回一个新的排序列表,其中包含您的iterable项。
如果你真的想要对你的双端队列进行排序,你必须在deque.sort()
方法中实现deque排序算法来做到这一点,我不知道哪种算法更有效率排序deque(我甚至不确定这样做是否有意义),但我认为你可能更容易重建deque并利用python非常有效的排序算法:
class ReportDeque(deque):
"""
Container for processing, sorting Report objects
"""
def sort(self, *args, **kwargs):
items = [self.pop() for x in xrange(len(self))]
items.sort(*args, **kwargs)
self.extend(items)
这应该让你的所有考试都通过。
<强>更新强>
如果您希望在send_time相等时使用period_length
消除歧义,则只需将其添加到__cmp__
,如下所示:
def __cmp__(self, other):
cmp((self.send_time, self.period_length),
(other.send_time, other.period_length))