Live data from Hacker News

Use JSON files as if they are Python modules

github.com

21–30 of 67 posts

Re: Use JSON files as if they are Python modules

#22
post #14

As a JavaScript developer for the past 5 years or so, when jumping into Python I certainly miss the simple require( ) which this seems to replicate into Python pretty well! Having said that I don't think that I would use it for serious things since this isn't really the Python way and I would like my code to be most understood by others. Neat though!

JSON stands for JavaScript Object Notation , not Python Object Notation. Furthermore "require" is "proprietary" to nodejs, ES2015 has a different module system and it will be the official js module system.

> JSON stands for JavaScript Object Notation , not Python Object Notation.

That does not mean it shouldn't be used anywhere else.

Re: Use JSON files as if they are Python modules

#23
post #19

Earlier quoted context omitted.

Is it be useful? Or there are some alternatives to import JSON as a module as easy as this one does?

For files: import json with open("foo.json") as f: foo = json.load(f) For strings: import json bar = '{"foo": "bar"}' foo = json.loads(bar)

Does that respect import search paths? Does the original?

Re: Use JSON files as if they are Python modules

#24
The mechanism is called "import hooks":

https://docs.python.org/3/reference/import.html#import-hooks

https://www.python.org/dev/peps/pep-0302/

You can see the import hook added here:

https://github.com/kragniz/json-sempai/blob/master/jsonsempa...

Import hooks are a great feature in Python.

Take the `import` mechanism in the language and reduce it to its barest theoretical formulation - what does it really do? (Name binding.) Think of all the other features in your application that can be reduced to this. (e.g., configuration management) Use the constraints of the import mechanism to guide the design of this feature, and use import hooks to implement it. You'll end up with an implementation that is very "close" to the core language. I would assert that this closeness strongly suggests correctness and composability.

Less philosophically, think of the Python virtual machine as a system with lots of "safety valves" and "escape hatches." Import hooks are one such safety valve. Think about all the things you could do easily by hooking into module importing? (Real use-cases: loading code from bitemporal object databases, deprecation of modules, relocation of modules, configuration management, &c.)

Of course, there are languages that are much more flexible than Python in this respect. Python aims for a practical flexibility, and I find that, in practice, Python strikes a nice balance.

Re: Use JSON files as if they are Python modules

#25
post #19

Earlier quoted context omitted.

For files: import json with open("foo.json") as f: foo = json.load(f) For strings: import json bar = '{"foo": "bar"}' foo = json.loads(bar)

Does that respect import search paths? Does the original?

Does it respect import paths: no.

As for whether or not json-sempai does, I looked through the code and I am pretty sure the answer is yes.

Declaring pkg_resources is probably a better way to include static data files in your package but hey radical freedom and all that.

Re: Use JSON files as if they are Python modules

#26

The mechanism is called "import hooks": https://docs.python.org/3/reference/import.html#import-hooks https://www.python.org/dev/peps/pep-0302/ You can see the import hook added here: https://github.com/kragniz/json-sempai/blob/master/jsonsempa... Import hooks are a great feature in Python. Take the `import` mechanism in the language and reduce it to its barest theoretical formulation - what does it really do? (Name b…

A simple example of how import hooks use for module relocation/deprecation:

https://gist.github.com/dutc/0f7498451d98e3114268

Re: Use JSON files as if they are Python modules

#27

I did the same with Perl in 10 minutes: https://git.io/vzKDs it was fun I will check it tomorrow when I'm back from the pub

Is there any practical difference between that and just a normal new() function returning a hash based on that json? Does bless change anything?

I mean, for "the same" I'd expect something like:

    ....
    use test
    say test->hello;
    say test->this->could->be->a->bad;

Re: Use JSON files as if they are Python modules

#29

I did the same with Perl in 10 minutes: https://git.io/vzKDs it was fun I will check it tomorrow when I'm back from the pub

Is there any practical difference between that and just a normal new() function returning a hash based on that json? Does bless change anything? I mean, for "the same" I'd expect something like: .... use test say test->hello; say test->this->could->be->a->bad;

Ok, back from the pub and fixed it you can now use it like this:

use BlessJSON qw(test.json);

say test->hello;

say test->this->could->be->a->bad;

close enough

Re: Use JSON files as if they are Python modules

#30

Earlier quoted context omitted.

Because it monkey patches the way imports work. The context manager isn't bad really, although there are probably more natural ways to create a python object with the same structure as a JSON file without abusing the import system.

Right. JSON is almost valid Python to begin with. Off the top of my head, the main differences are that JSON true needs to be True in Python (likewise with false/False, null/None). It's not that difficult.

It's also slightly stricter with syntax; you can't have extra ending commas. {'foo' : 1, 'bar' : 2,} is valid Python but not valid json.
Post reply on HN