267 points · by bane · 10y ago · 65 comments
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.
1. https://tomforb.es/automatically-inline-python-function-call...
with IFDEF("DEBUG"):
I'll have to dig around and find my code, your inline project was a great help in understanding the import hooks and walking the ast.
> Ever since I saw this I have been unable to sleep. Please fix.
This is gold.
>>> from jsonsempai import magic
>>> from python_package import file
>>> file
<module 'python_package.file' from 'python_package/file.json'>
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.)
If this were to be used seriously, it would make sense to other devs that the import named magic is doing some crazy stuff.
it was fun I will check it tomorrow when I'm back from the pub
I mean, for "the same" I'd expect something like:
....
use test
say test->hello;
say test->this->could->be->a->bad;use BlessJSON qw(test.json);
say test->hello;
say test->this->could->be->a->bad;
close enough
Non obvious "magic" code like that tends to be frowned on in general, and especially in the python community
import json
with open("foo.json") as f:
foo = json.load(f)
For strings: import json
bar = '{"foo": "bar"}'
foo = json.loads(bar)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!
That does not mean it shouldn't be used anywhere else.
node was originally an implementation of it before eclipsing it. Between the original advocacy by CommonJS and the rising popularity of node, browserify, and Webpack, things like require() leaked out to become the pre-ES2015 de facto standard for importing modules.
require() is so widespread now, and the transform to the ES2015 syntax so trivial, that it's not going away anytime soon.
I'm curious why the author would do this. Just for the fun in it?
I nearly spit my drink out at the name, though.
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.
Like many things in Python import is actually just syntactic sugar for a dunder function.
my_json["this"]["can"]["be"] == "nested"
and is impossible to write defensively and cleanly after the first call:
my_json.get("this", {}).get("no way to not crash here")
Something that could do:
my_json.this.can.be == "nested"
With a default value would be very useful. Does anyone know something like it, or should I whip a module up in an hour?
https://github.com/skorokithakis/jsane
There's no code there, but the underlying class is pretty much spot on yours. I'm still trying to improve the API, but it's not looking great.
json.structure.that.i.want.to.access.default(True)
Or something like that. I don't really like the default item API I've come up... I'll comment here if I do give it a go, thanks for the snippet!
I almost died laughing at this. Feels like the author is aware that this scenario is probably the only one where such library can be mission critical.
* Import XML? * Import any_structured_data!
"JSONx is an IBM® standard format to represent JSON as XML. The appliance converts JSON messages that are specified as JSON message type to JSONx. The appliance provides a style sheet that you can use to convert JSONx to JSON."
https://www-01.ibm.com/support/knowledgecenter/SS9H2Y_7.1.0/...
(I used OP's work as a reference. Amazing you can do this in so few lines of code.)