[Chandler-dev] a new kind of enumeration type
Phillip J. Eby
pje at telecommunity.com
Wed Aug 30 14:38:36 PDT 2006
Because dictionaries aren't ordered.
At 02:07 PM 8/30/2006 -0700, Grant Baillie wrote:
>Hmm.... Why not just have the declaration order be the sort order?
>
>--Grant
>
>On 30 Aug, 2006, at 13:57, Andi Vajda wrote:
>
>>
>>While implementating Chandler's dashboard Bryan found that he
>>needed better control over the sorting of enumeration values.
>>
>>Currently, enumeration values are strings and are sorted as such.
>>For example, if one defines an enumeration with the schema API as
>>follows:
>>
>> class TriageEnum(schema.Enumeration):
>> values = "now", "later", "done"
>>
>>The values of this enumeration will be sorted as:
>> "done" < "later" < "now"
>>
>>
>>Enter a new type of enumeration, an enumeration of constants. Such
>>constants have a name and a value and are sorted on their value
>>first. Multiple constants in an enum may have the same value, their
>>name is used next in sorting.
>>
>>By using a dict for the enumeration's values instead of a tuple,
>>one can define such a constant enumeration with the schema API:
>>
>> class TriageEnum(schema.Enumeration):
>> values = {"now": 0, "later": 1, "done": -1}
>>
>>Upon definition, the TriageEnum class gets assigned three
>>attributes called 'now', 'later' and 'done' that contain such a
>>constant:
>>
>> TriageEnum.now -> TriageEnum.now
>> TriageEnum.now.value -> 0
>> TriageEnum.now.name -> 'now'
>>
>> repr(TriageEnum.now) -> 'TriageEnum.now'
>> str(TriageEnum.name) -> 'now'
>>
>>These constants are sorted by their values:
>> TriageEnum.done < TriageEnum.now < TriageEnum.later
>>
>>Andi..
More information about the chandler-dev
mailing list