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?
Use JSON files as if they are Python modules
41–50 of 67 posts
Re: Use JSON files as if they are Python modules
#42From 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.)
Re: Use JSON files as if they are Python modules
#43The 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…
Re: Use JSON files as if they are Python modules
#44The 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
#45Earlier 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.
Re: Use JSON files as if they are Python modules
#46Some people are saying JSON ≠ Python Object Notation. Here we go! https://github.com/brhs/pon (I used OP's work as a reference. Amazing you can do this in so few lines of code.)
Re: Use JSON files as if they are Python modules
#47* Import XML? * Import any_structured_data!
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?
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.