简体   繁体   English

控制自定义类的排序/索引方式?

[英]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: 我正在查看的功能:

  1. a number of EnhancedDay objects to be stored, sorted , as EnhancedCalendar.attr 要存储的一些EnhancedDay对象, 排序EnhancedCalendar.attr
  2. ability to retrieve / work with 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
  3. a way to append/modify attributes of 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实例,以便如果我尝试将添加 EnhancedDayEnhancedCalendar.attr那里已经是一个EnhancedDay对象在EnhancedCalendar.attr表示日期, EnhancedDay对象将改为“自动”合并。

I think for #1, I basically understand the answer: 我认为对于#1,我基本上理解答案:

  1. I would use the @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__和其他比较运算符以使用EnhancedDaydate attr,并且会很好去。
  2. No idea. 不知道。 There are a few posts on SO on this topic, but I haven't found any that meet my needs. 关于此主题,有几篇关于SO的文章,但我还没有找到满足我需求的文章。
  3. The best way I know would be to write a custom merging function (something akin to a custom_append). 我知道的最好方法是编写一个自定义合并功能(类似于custom_append)。 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.

相关问题 使用自定义类/函数作为排序 function 中的键 - using custom class/function as key in sorted function 如何在此 class 中获得排序成员? - How to get sorted members in this class? 如何制作一个排序的字典类? - How to make a sorted dictionary class? 如何创建基于列和值范围的多索引序列,并检查其排序是否正确? - How do I create multi indexed series based on columns and range of values and check if it sorted properly? 为什么在对排序索引进行索引查找时进行PerformanceWarning? - Why PerformanceWarning when indexed lookup on sorted index? 如何从 Pandas (Python) 中的排序时间序列索引的数据帧中的列中的所有值生成统计特征? - How can I generate statistical features from all values in a column from dataframe indexed by a sorted timeseries in Pandas (Python)? 控制元类中的上下文管理器 - Controlling context manager in a meta class Python class 合并排序的文件,如何改进? - Python class to merge sorted files, how can this be improved? 如何在python中实现可以比较,排序等的类 - How to implement a class in python that can be compared, sorted, etc 如何将我的 dict 值类 { } 更改为 [ ] 并按键对其进行排序 - How to change my dict values class { } to [ ] and sorted it by keys
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM