Apply slice or replace function to a set

59 views
Skip to first unread message

Marek Nos

unread,
Dec 12, 2014, 3:53:18 PM12/12/14
to objec...@googlegroups.com
I'm askin if its possible to use one of the powerful slice or replace functions on multiple elements at once.

I've data set like:
{"products":
  {"instock": 0, "code": 123456789},
  {"instock": 1, "code": 23456789}
  {"instock": 1, "code": 3456789}
}
and query like:
$.products[@.instock > 0].code
which returns Generator I can itterate or print. But I would like to get only first 4 characters of the code.

Result would be like
1234
2345
3456

Is that possible with Objectpath? To extend slice/replace and other string functions to be able to work on a set?

Thanks
Marek

Adrian Kalbarczyk

unread,
Dec 12, 2014, 4:30:30 PM12/12/14
to Marek Nos, objec...@googlegroups.com
Try this:

>>> slice(["sss","aaa"],[0,2])
[
"sss",
"aaa"
]
>>> slice(map(str, $.products[@.instock > 0].code), [0,3])
[
"1234",
"2345"
]

Use map(int, ...) to convert array of strings back into numbers. Mind that this query might be somewhat slow.
If that doesn't work, try the newest version (from github), if still doesn't, send a bug on GH.

Hope that helps.

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.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages