I must say that the idea of json-sempai is interesting for a JSON file like tester.json:
{ "hello": "world", "this": { "can": { "be": "nested" } } }
You can access JSON files as if they were Python imported module objects:
>>> from jsonsempai import magic >>> import tester >>> tester <module 'tester' from 'tester.json'> >>> tester.hello u'world' >>> tester.this.can.be u'nested'
It is nice that you can get the JSON objects via object attributes, the standard Python json library uses dictionary, which sometimes quite annoying when you have to type ['foobar'].
Nevertheless, regardless the convenience, there would be a price to pay, this library also relies on the standard JSON library, it would be reasonably to predict that it would run slower.
The question how big is the price, so I wrote a quite test code to get some numbers:
import timeit print(timeit.timeit( 'tester.this.can.be', 'from jsonsempai import magic; import tester' )) print(timeit.timeit( "j['this']['can']['be']", "import json; fp = open('tester.json');" "j = json.load(fp); fp.close()" ))
The results are, in seconds:
Library | 2.7.7 | 3.3.5 | 3.4.1 |
---|---|---|---|
json-sempai | 4.126 | 3.351 | 3.918 |
json | 0.852 | 0.245 | 0.190 |
As you can see, it can’t compete with standard library in performance, maybe it’s the attribute access cost that much time. Anyway, the selling point of json-sempai is:
import some_json_file
Is it worth? I’ll have to say no, there must be other JSON library which can allow you to access via attributes, if this library can work as C extension to boost the performance, it would be more practical, for now, it’s just a “Because I Can.”
Maybe that’s one of reasons why it warns with the following words:
Disclaimer: Only do this if you hate yourself and the rest of the world.
json-sempai is written by Louis Taylor, in Python 2 and 3, currently git-3d049e9 (2015-01-01, post v0.2.0), licensed under the MIT License.
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.