Hey Andreas,
Here is some example code for ext.db. I can't recall for sure, but
I think I've seen you on the ndb lists; it also has a computed
property, and would be similar to this.
class Example(db.Model):
name = db.StringProperty()
@db.ComputedProperty
def name_(self):
"""Normalize the name, so that it can be sorted / filtered intuitively."""
# remove extension and white space
cleaned = self.name.rsplit('.', 1)[0]
cleaned = cleaned.strip()
# handle ints
if cleaned.isdigit():
return int(cleaned)
# case normalize
lowered = cleaned.lower()
# handle "other" cases.
return process_mixed_name(lowered)
# To query, ordered by normalized name:
Example.all().order('name_')
You could also handle floats in a way similar to integers. In the
process_mixed_name you'll need to decide how you want to normalize
things containing letters and digits. The easiest way might be to
zero-left pad the integers contained within.
Robert
On Wed, Jan 25, 2012 at 06:56, Andreas <[email protected]> wrote:
> how would i do this?
> and how could i influence the behaviour of the order of a query result?
>
> On Jan 25, 2012, at 2:38 AM, Robert Kluin wrote:
>
> Hey Andreas,
> You could actually include this as a computed property on your entity, the
> order by the computed property. I often do this to normalize (lower-case,
> strip punctuation, etc...) names for searching.
>
>
> Robert
>
>
>
>
> On Tuesday, January 24, 2012, Andreas <[email protected]> wrote:
>> im actually wondering why calling .order() on a string property returns an
>> ascii order and not a human expected order.
>> i understand that this is the default behaviour of most (all?) languages
>> but who the hell would like an ascii order on a query?
>>
>> lets say we store filenames as a StringProperty() and we want to order on
>> them.
>>
>> lets say the filenames are:
>> 1.jpg
>> 2.jpg
>> 11.jpg
>> 12.jpg
>>
>> right now the order returned would be:
>> 1.jpg
>> 11.jpg
>> 12.jpg
>> 2.jpg
>>
>> i actually dont know who actually would be happy with an order like that.
>>
>> what it should return is:
>> 1.jpg
>> 2.jpg
>> 11.jpg
>> 12.jpg
>>
>> of course!!!
>>
>> right now i have to add a custom sort function to sort results in memory
>> after fetching them. which also means i cant use this for batching and
>> keys_only queries.
>> this is pretty sad!
>>
>> this function helps:
>>
>> def sort_nicely(l, reverse=False):
>> """ Sort the given list in the way that humans expect. """
>> convert = lambda text: int(text) if text.isdigit() else text
>> alphanum_key = lambda entry: [convert(e) for e in re.split('([0-9]+)',
>> entity.filename)]
>> l.sort(key=alphanum_key)
>> if reverse:
>> l.reverse()
>> return l
>>
>> so why not including a function like that into the query language?
>>
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "Google App Engine" group.
>> To post to this group, send email to [email protected].
>> To unsubscribe from this group, send email to
>> [email protected].
>> For more options, visit this group at
>> http://groups.google.com/group/google-appengine?hl=en.
>>
>>
>
> --
> ------
> Robert Kluin
> Ezox Systems, LLC
>
>
>
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Google App Engine" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/google-appengine?hl=en.
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Google App Engine" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/google-appengine?hl=en.
--
You received this message because you are subscribed to the Google Groups
"Google App Engine" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/google-appengine?hl=en.