[Chandler-dev] Defining a sort order for schema.Enumerations
Bryan Stearns
stearns at osafoundation.org
Mon Jun 19 15:58:12 PDT 2006
Hi pje,
I need to sort ContentItems by triageStatus, where 'triageStatus' is a
TriageEnum attribute:
class TriageEnum(schema.Enumeration):
schema.kindInfo(displayName=_(u"Triage Status Enum"))
values = "now", "later", "done"
class ContentItem(schema.Item):
triageStatus = schema.One(TriageEnum, indexed=True,
displayName=_(u"Triage Status"),
initialValue="now")
Currently, we're sorting by the raw attribute value -- essentially,
key=lambda x: x.triageStatus -- but this sorts alphabetically: "done",
"later", "now"; it's really supposed to sort in the order the values are
given.
* My initial thought was to add something like this to the TriageEnum
declaration:
class TriageEnum(schema.Enumeration):
...
ordering = dict((v, i) for (i, v) in enumerate(values))
because then I could do something like key=lambda x:
ContentItem.triageStatus.type.ordering[x.triageStatus]
However, this would only work for TriageStatus, and this seems like
something useful for all schema.Enumeration-based attributes.
* so I was thinking about ways to generalize this to any
schema.Enumeration-based type... This may make you cringe, but adding
this line to the end of EnumerationClass.__init__ did the trick:
class EnumerationClass(Activator):
def __init__(cls,name,bases,cdict):
[...]
cls.ordering = dict((v, i) for (i, v) in enumerate(cls.values))
Or can you suggest a better way to accomplish this? (I'd also considered
just figuring out the values index within the lambda, but this seemed
inefficient, especially since there's no .index on tuples :-( ...)
Thanks,
...Bryan
More information about the chandler-dev
mailing list