[英]Controlling how a custom class is sorted / indexed?
I am attempting to represent a calendar in a Class-based format. 我试图以基于类的格式表示日历。
Currently, the data is represented in an OrderedDict of the following form: 当前,数据以以下形式的OrderedDict表示:
from collections import OrderedDict
dictionary = OrderedDict()
dictionary[date] = {'attr1': ... ,
'attr2': ... ,
... }
I would ideally like to instantiate some sort of EnhancedDay
class which represents the data in the dictionary, with a corresponding EnhancedCalendar
class that holds a sorted container (list, OrderedDict?) of EnhancedDay
s. 我非常喜欢实例某种EnhancedDay
类代表在字典中的数据,并有相应的EnhancedCalendar
的保存排序容器类(列表,OrderedDict?) EnhancedDay
秒。 Features I'm looking at: 我正在查看的功能:
EnhancedDay
objects to be stored, sorted , as EnhancedCalendar.attr
要存储的一些EnhancedDay
对象, 排序为EnhancedCalendar.attr
EnhancedDay
s by date (ie I would like to use the date as a sort of 'key' so I could retrieve an EnhancedDay
through something similar to EnhancedCalendar.attr[date]
能够按日期检索/使用EnhancedDay
的功能(即,我想将日期用作一种“键”,以便我可以通过类似于EnhancedCalendar.attr[date]
来检索EnhancedDay
EnhancedDay
instances which are stored as part of a EnhancedCalendar
instance so that if I were to attempt to add an EnhancedDay
to EnhancedCalendar.attr
where there already was an EnhancedDay
object in EnhancedCalendar.attr
representing that date, the EnhancedDay
object would instead be merged "automatically." 一种方法来附加/修改的属性EnhancedDay
被存储为一个的一部分实例EnhancedCalendar
实例,以便如果我尝试将添加 EnhancedDay
到EnhancedCalendar.attr
那里已经是一个EnhancedDay
对象在EnhancedCalendar.attr
表示日期, EnhancedDay
对象将改为“自动”合并。 I think for #1, I basically understand the answer: 我认为对于#1,我基本上理解答案:
@total_ordering
decorator ( https://docs.python.org/2/library/functools.html#functools.total_ordering ), specify an __eq__
and other comparison operator to use the EnhancedDay
's date
attr, and be good-to-go. 我将使用@total_ordering
装饰器( https://docs.python.org/2/library/functools.html#functools.total_ordering ),指定__eq__
和其他比较运算符以使用EnhancedDay
的date
attr,并且会很好去。 EnhancedCalendar.attr[date].custom_append(EnhancedDay)
and have a merge occur OR if attr[date]
doesn't exist, create attr[date]
Again, not sure of the best/smoothest way to do this. EnhancedCalendar.attr[date].custom_append(EnhancedDay)
并进行合并,或者如果attr[date]
不存在,则创建attr[date]
再次,不确定执行此操作的最佳/最顺畅方法。 I'd love to use +=
. 我很乐意使用+=
。 Also: what would be the best datatype to store the EnhancedDay
instances in, in the EnhancedCalendar
instance? 另外:什么是存储的最佳数据类型EnhancedDay
实例中,在EnhancedCalendar
实例?
Alternatively, given what I'm trying to accomplish, some of my thinking might just be... off. 另外,考虑到我要完成的工作,我的某些想法可能会...落空。 Any help on that front would be appreciated. 在这方面的任何帮助将不胜感激。
Alternatively, this all may be way more trouble than it's worth, in which case I'm still interested in how you would theoretically accomplish these things using classes. 另外,这一切都可能比它值得的麻烦多,在这种情况下,我仍然对您如何使用类从理论上完成这些事情感兴趣。
For #2 There are a few ways to do this. 对于#2,有几种方法可以执行此操作。 One way might be to convert the date to some sort of canonical format, then use that as a string index into a dictionary. 一种方法可能是将日期转换为某种规范格式,然后将其用作字典的字符串索引。
For #3 take a look at https://docs.python.org/3/library/collections.abc.html#collections.abc.Sequence 对于#3,请查看https://docs.python.org/3/library/collections.abc.html#collections.abc.Sequence
It makes sure you implement the functions required for indexing and +=
operators to work the way you want. 它确保您实现索引和+=
运算符以所需方式工作所需的功能。 Specifically for +=
to work you need to override the __iadd__()
function. 专门为+=
起作用,您需要重写__iadd__()
函数。 For Calendar[date]
to do what you want you'll need to override the __getitem__()
and __setitem__()
functions -- however collections.DefaultDict nearly does what you want there so I might be tempted to base EnhancedCalendar off of that. 为了让Calendar[date]
做您想做的事情,您需要重写__getitem__()
和__setitem__()
函数-但是collections.DefaultDict几乎__setitem__()
您的要求,因此我可能会想以此为基础来增强EnhancedCalendar。
I think I would use a list datatype to store the days inside the calendar, then you would index by day-1. 我想我将使用列表数据类型在日历中存储日期,然后按第1天编制索引。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.