6 ms·
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 e
by ben336 11y ago
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
- fbukevin 11y agoIs it be useful? Or there are some alternatives to import JSON as a module as easy as this one does?
- nickles 11y agoFor files: import json with open("foo.json") as f: foo = json.load(f) For strings: import json bar = '{"foo": "bar"}' foo = json.loads(bar)
- rileymat2 11y agoDoes that respect import search paths? Does the original?
- gh02t 11y agoDoes 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.
- sametmax 11y agoDo 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.
- gh02t 11y agoYeah, 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 files in the package_data argument to setup in your setup.py. You can then get the contents of that file (not its path) by importing the pkg_resources package, which provides a lookup table of loaded resources. This [apparently] works for binary dists but not source dists (the opposite of MANIFEST). I've used pkg_resources without issue myself, but it's possible I just have been lucky. In any event, here's a few links discussing the differences and also how to use pkg_resources: http://stackoverflow.com/questions/7522250/how-to-include-package-data-with-setuptools-distribute http://stackoverflow.com/questions/7522250/how-to-include-pa... http://blog.codekills.net/2011/07/15/lies,-more-lies-and-python-packaging-documentation-on--package_data-/ http://blog.codekills.net/2011/07/15/lies,-more-lies-and-pyt... http://peak.telecommunity.com/DevCenter/PythonEggs#accessing-package-resources http://peak.telecommunity.com/DevCenter/PythonEggs#accessing... If you want an example, here's a package I made that uses pkg_resources (I make no promises that this is the right way to do it, but it works for me): (setup.py, see line 10) https://github.com/jasonmhite/gefry2/blob/master/setup.py https://github.com/jasonmhite/gefry2/blob/master/setup.py (the data file I want to include) https://github.com/jasonmhite/gefry2/tree/master/gefry2/data https://github.com/jasonmhite/gefry2/tree/master/gefry2/data (where the code loads the data file, see line 67) https://github.com/jasonmhite/gefry2/blob/master/gefry2/materials.py https://github.com/jasonmhite/gefry2/blob/master/gefry2/mate...
- sametmax 11y agoThanks
- adrusi 11y agoThere is no good reason to load json from import paths. Your import paths should contain code, assets, not configuration.
- icebraining 11y agoA 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.
- onnoonno 11y agoCython does the same thing for ".pyx" files and I have to say it is very handy and works well.