小编Bra*_*ant的帖子

如何在日期时间对类进行排序,对 collections.deque 进行排序

我真的可以在如何实现getitemiter方法或生成器函数方面使用一些帮助来对我创建的类和类容器进行排序。

我创建了一个具有 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)
Run Code Online (Sandbox Code Playgroud)

我正在努力实现Report 中的getitem以及ReportDeque 中的iter和 next 方法。我似乎无法找到使所有这些工作所需的示例。

也许应该使用生成器函数对 collections.deque 容器进行排序。拥有各种生成器以不同方式对双端队列进行排序会很好。

下面是我的测试用例。要在以下代码中运行单元测试,请键入:

  python -m unittest test_reports
Run Code Online (Sandbox Code Playgroud)

输出在这篇文章的末尾。

提前致谢...

------------------- 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 …
Run Code Online (Sandbox Code Playgroud)

python sorting comparison unit-testing python-2.7

4
推荐指数
1
解决办法
5045
查看次数

标签 统计

comparison ×1

python ×1

python-2.7 ×1

sorting ×1

unit-testing ×1