Live data from Hacker News

Use JSON files as if they are Python modules

github.com

61–67 of 67 posts

Re: Use JSON files as if they are Python modules

#61
> Have you ever been kept awake at night, desperately feeling a burning desire to do nothing else but directly import JSON files as if they were python modules

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.

Re: Use JSON files as if they are Python modules

#62

> Have you ever been kept awake at night, desperately feeling a burning desire to do nothing else but directly import JSON files as if they were python modules 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.

Funny cause it's true. It's really something I desperately seeked a few years ago

Re: Use JSON files as if they are Python modules

#63
post #62

> Have you ever been kept awake at night, desperately feeling a burning desire to do nothing else but directly import JSON files as if they were python modules 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.

Funny cause it's true. It's really something I desperately seeked a few years ago

... ??? why? What's wrong with json.loads()?

Re: Use JSON files as if they are Python modules

#64
I'm not sure which is the better/worse idea:

"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/...

Re: Use JSON files as if they are Python modules

#65
post #57

Earlier quoted context omitted.

Do you have any good tutorial on how to use pkg_resources ? I use pathlib.Path(__file__).absolute().parent but it's not zip safe. Problem is, I can't wrap my head around pkg_resources and the docs don't help.

Yeah, docs are a bit fuzzy and I'm not exactly sure what the right answer is. So there are actually two ways to go about it AFAIK and I'm not entirely sure which one is the best. One is to list files in MANIFEST.in and then access it via __file__, but I think this doesn't work if your module gets turned into an egg/wheel (it's for source dists). On the other hand is using pkg_resources, where you declare the list of…

Thanks

Re: Use JSON files as if they are Python modules

#66
post #9

Why do all the comments act like this is bad? Seems fine to me. Explanation would be appreciated :)

You're changing the behavior of import. So if somebody else were to remove the seemingly unused `from jsonsempai import magic` the file would stop working as expected. Non obvious "magic" code like that tends to be frowned on in general, and especially in the python community

Cython does the same thing for ".pyx" files and I have to say it is very handy and works well.

Re: Use JSON files as if they are Python modules

#67
post #54

Excepting the cringemarsh that is "import ", this is actually very useful. This looks very unpythonic: 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…

I've taken a crack at something similar, I'd love to see what you came up with. I ended up with this super-meh solution: https://github.com/zmap/ztag/blob/master/ztag/transform.py#L...

My thing turned out to be pretty much the same as yours, also equally meh:

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.

Post reply on HN