Skip to content

Instantly share code, notes, and snippets.

@matthewryanscott
Created July 1, 2009 19:11
Show Gist options
  • Save matthewryanscott/138989 to your computer and use it in GitHub Desktop.
Save matthewryanscott/138989 to your computer and use it in GitHub Desktop.
from schevo.schema import *
schevo.schema.prep(locals())
import string
class SortedUniqueNames(F.String):
"""List of space-separated names, sorted case-insensitively."""
def as_list(self):
"""Return sorted list of unique names."""
value = self._value
if value is UNASSIGNED:
return []
else:
return sorted(set(value.split()), key=string.lower)
def convert(self, value, db=None):
"""Convert the value to a normalized string."""
value = F.String.convert(self, value, db)
if value is UNASSIGNED:
return value
# Split text separated by either commas or whitespace.
values = u' '.join(value.split(u',')).split()
return u' '.join(sorted(set(values), key=string.lower))
class MyEntity(E.Entity):
serial_number = f.string()
names = f.sorted_unique_names()
_key(serial_number)
# In client code you can now do things like this:
foo123 = db.MyEntity.findone(serial_number='foo123')
names_list = foo123.f.names.as_list()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment