Complex sort key lambda or equivalent

46 views
Skip to first unread message

Stefan Kögl

unread,
Jul 31, 2019, 8:00:34 PM7/31/19
to ObjectPath
Hey there,

is this possible right now with objectpath?

from natsort import natsorted

queryData = Tree([
{
"key": "i23",
"classification": "lastname1, firstname1 / apartment / Vienna / Alsergrund / thathouse / 1965"
},
{
"key": "i3076",
"classification": "lastname1, firstname1 / apartment / Vienna / Alsergrund / thathouse / 1978"
},
{
"key": "i347",
"classification": "lastname1, firstname1 / apartment / Vienna / Alsergrund / thathouse"
},
{
"key": "i169",
"classification": "lastname1, firstname1 / apartment / Vienna / Alsergrund / thathouse"
},
{
"key": "i2929",
"classification": "lastname1, firstname1 / apartment / Vienna / Alsergrund / thathouse"
},
{
"key": "i1347",
"classification": "lastname1, firstname1 / apartment / Vienna / 1999"
}
])

tmpResult = queryData.execute('$.*["apartment / Vienna" in @.classification]')
result = natsorted(tmpResult, key=lambda x: (x["classification"].split(" / ")[2:], x["key"]))

I would like to be able to combine the query and the complex sorting into one execute query.






Stefan Kögl

unread,
Aug 1, 2019, 5:49:43 PM8/1/19
to ObjectPath
After reading the code of parser and interpreter, especially lambda related grammar is missing. It's so sad ;)

Adrian Kalbarczyk

unread,
Aug 2, 2019, 9:20:45 AM8/2/19
to Stefan Kögl, ObjectPath
No, ObjectPath doesn't support lambdas. 

Greetings,
Adrian Kalbarczyk

http://kalbarczyk.co


--
You received this message because you are subscribed to the Google Groups "ObjectPath" group.
To unsubscribe from this group and stop receiving emails from it, send an email to objectpath+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/objectpath/ca8cc819-4c61-4eff-b86d-de1c6da6b774%40googlegroups.com.

Egil Möller

unread,
Sep 16, 2019, 7:52:40 AM9/16/19
to ObjectPath
 Heyas!

I've written another implementation of ObjectPath with a few extensions https://github.com/innovationgarage/sakstig and I just added the possibility to do what you wanted:


    >>> import sakstig
    >>> queryData = sakstig.QuerySet([
    ...     {"key": "i23",
    ...      "classification": "lastname1, firstname1 / apartment / Vienna / Alsergrund / thathouse / 1965"},
    ...     {"key": "i3076",
    ...      "classification": "lastname1, firstname1 / apartment / Vienna / Alsergrund / thathouse / 1978"},
    ...     {"key": "i347",
    ...      "classification": "lastname1, firstname1 / apartment / Vienna / Alsergrund / thathouse"},
    ...     {"key": "i169",
    ...      "classification": "lastname1, firstname1 / apartment / Vienna / Alsergrund / thathouse"},
    ...     {"key": "i2929",
    ...      "classification": "lastname1, firstname1 / apartment / Vienna / Alsergrund / thathouse"},
    ...     {"key": "i1347",
    ...      "classification": "lastname1, firstname1 / apartment / Vienna / 1999"
    ...     }
    ... ])
    >>> 
    >>> queryData.execute('sort($, slice(split(@.classification, "/"), [2, None])+[@.key]).*', compatibility=False)
    {'key': 'i1347', 'classification': 'lastname1, firstname1 / apartment / Vienna / 1999'}
    {'key': 'i169', 'classification': 'lastname1, firstname1 / apartment / Vienna / Alsergrund / thathouse'}
    {'key': 'i2929', 'classification': 'lastname1, firstname1 / apartment / Vienna / Alsergrund / thathouse'}
    {'key': 'i347', 'classification': 'lastname1, firstname1 / apartment / Vienna / Alsergrund / thathouse'}
    {'key': 'i23', 'classification': 'lastname1, firstname1 / apartment / Vienna / Alsergrund / thathouse / 1965'}
    {'key': 'i3076', 'classification': 'lastname1, firstname1 / apartment / Vienna / Alsergrund / thathouse / 1978'}

The key parameter to the sort function can now be a path, which is executed in the local context of the current row being sorted. It should return a single result, which the row will be sorted by.
 
As you can see, some functions, like slice, have a slightly larger domain than in the default ObjectPath implementation, e.g. some string functions can be applied to lists, etc (where it makes sense).

Also, note that I turned off full compatibility with ObjectPath in the call above, which among other things slightly affects how .* works, which makes some stuff less confusing but does break compatibility, check the documentation for details.
Reply all
Reply to author
Forward
0 new messages