Live data from Hacker News

Use JSON files as if they are Python modules

github.com

41–50 of 67 posts

Re: Use JSON files as if they are Python modules

#41
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?

There is no good reason to load json from import paths. Your import paths should contain code, assets, not configuration.

Re: Use JSON files as if they are Python modules

#42
post #38

From the examples: >>> from jsonsempai import magic >>> from python_package import file >>> file I believe it is not a good idea to teach people to import something named "file", as that overrides Python's builtin class "file". (On the other hand, that class is usually instantiated via calls to "open", so the class name "file" is unused is most programs dealing with files.)

Feel free to send a pull request changing it to a name that makes things clearer. Calling it 'file' probably isn't the best idea, but I think it makes it clear you're loading from some arbitrary json file.

Re: Use JSON files as if they are Python modules

#43

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…

This seems like a huge case of "implicit over explicit". Ideally if you import package foo, it shouldn't affect anything but foo.bar

Re: Use JSON files as if they are Python modules

#44
post #43

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…

This seems like a huge case of "implicit over explicit". Ideally if you import package foo, it shouldn't affect anything but foo.bar

Python itself never enforced that, though. It's the libraries' responsibility to have a "register_import_hook()" function or similar instead of just doing so unannounced.

Re: Use JSON files as if they are Python modules

#45
post #41

Earlier quoted context omitted.

Does that respect import search paths? Does the original?

There is no good reason to load json from import paths. Your import paths should contain code, assets, not configuration.

A JSON document might be an asset, such as static data. In fact, I'd much rather have a Python file as configuration and JSON as data than the opposite.

Re: Use JSON files as if they are Python modules

#48

> Disclaimer: Only do this if you hate yourself and the rest of the world. I'm curious why the author would do this. Just for the fun in it?

Fun is the only thing that can justify this blasphemy :)

Why do this instead of:

   myvar = read_json_file_as_object('path/to/file')
??

Ok, you can argue that function would hit the hard drive many times when called from multiple modules (while imports would not), but this can be fixed with simple memoization.

Post reply on HN