Source: Asyncode Blog

Asyncode Blog ObjectPath 0.5 is here!

We are excited to announce the release of version 0.5 of ObjectPath. This release brings:up to 10x performance gains (5x on average) compared to ObjectPath 0.4, new matches operator, slice() function, support for regular expressions,better language consistency.Besides changes to the language itself, in this release we have entirely dropped XML support, added all shell command, moved Javascript implementation to a separate repository on Github and we have written tens of new tests to prevent functionality degradation (achieving 100% interpreter's code coverage). We have also integrated TravisCI, Coveralls.io and Landscape.io to better track code quality.5x improved performance ObjectPath has become 5 times faster on average, and in some cases performance has improved 10 times. This has been achieved by moving interpreter to generators and heavy use of list comprehensions. Now even complex queries are computed in a fraction of time it takes to parse a JSON document. In our tests on a 200MB file reading and parsing took 18 seconds and executing the query - 0.5 second.To find out more, check out ObjectPath gets 5 times faster!Regular expressionsThe new version of ObjectPath introduces native support for regular expressions. The notation is borrowed from Javascript and it looks as follows:/myRegularExpression//[0-9][0-9]/We intend this notation to be universally used in all types of queries. If you come across a use case where this new notation should work but doesn't, please post an issue on github.Additionally, we have introduced a new function re() that allows for building regular expression strings on the fly. Use it when the contents of regex string are computed from JSON document.re("[0-9]"+"[0-9]")We also added matches operator to allow simple regex usage:/[0-9][0-9]/ matches "23" -> trueBetter language consistency - the behavior of .. slightly changesTo make the language more consistent in its purpose, we have modified the behavior of recursive descent operator. Previously it was:Scan the whole document for keys equal to the right hand side of .. operator and return the value under each key. If .. operator returns an array of one object, return the object.That means that if .. found two arrays, it would return an array with both of them ([[], []]). The same applied to returning an object when the array length was 1 - this property in some cases caused problems. The new behavior is more user friendly and always returns an array of objects. It simply concatenates all the arrays found (as well as objects) into one large array.The change might however cause problems with arrays of arrays (such as geospatial objects). If you are certain that the value under a key is an array of arrays, use map(array, $..key) instead of just $..key.From now on $..book executed on:{"store":[ {"book":[ {"name":"a"}, {"name":"b"} ]}, "book":{"name":"c"} ]}results in [{"name": "a"}, {"name": "b"}, {"name":"c"}] rather than [[{"name": "a"}, {"name": "b"}], {"name": "c"}].In other words, $..book means "find all books in the document" where arrays of books are merged into one large array.The new behavior has been integrated with other operators to ensure seamless end-user experience.New function - slice() The slice() method extracts a section of a string and returns a new string. Here's how it works:slice(string, [start, end]) slice(string, [[start, end], [start2, end2], ...])The first argument is always a string. The second argument can be either an array of two numbers (start and end positions) or an array of arrays of two numbers (start and end positions). If a position is negative, then the position is counted backwards starting from the end of the string. Here's an example:slice("Hello world!", [6, 11]) -> "world"slice("Hello world!", [6, -1]) -> "world"slice("Hello world!", [[0, 5], [6, 11]]) -> ["Hello", "world"]No more XML supportObjectPath used to support XML by converting it to JSON before processing it. Maintaining XML to JSON converter is not a trivial task and since someone else provides just that, we decided to focus on developing the language itself.To convert XML to JSON, you may consider libraries such as ObjTree or Python-based xmltodict.all shell commandThe default shell behavior is to show only the first 5 elements of each array. To see all elements in all arrays in the result use all keyword before your query:>>> [1,2,3,4,5,6,7,8][1, 2, 3, 4, 5, <3 more objects>]>>> all [1,2,3,4,5,6,7,8][1, 2, 3, 4, 5, 6, 7, 8]Installation and End notesTo install ObjectPath 0.5 use the following command:$ sudo pip install objectpathReference manual is available at http://adriank.github.io/ObjectPath/reference.html.We'd love to hear your comments and thoughts about this new version of ObjectPath. Please, submit any bugs via Github. For everything else you may use our google group.

Read full article »
Est. Annual Revenue
$100K-5.0M
Est. Employees
1-25
Ela Bednarek's photo - CEO of Asyncode

CEO

Ela Bednarek

CEO Approval Rating

70/100

Read more