My favorite is this issue: https://github.com/kragniz/json-sempai/issues/7 > Ever since I saw this I have been unable to sleep. Please fix.
This is gold.
31–40 of 67 posts
My favorite is this issue: https://github.com/kragniz/json-sempai/issues/7 > Ever since I saw this I have been unable to sleep. Please fix.
This is gold.
Earlier quoted context omitted.
Because it monkey patches the way imports work. The context manager isn't bad really, although there are probably more natural ways to create a python object with the same structure as a JSON file without abusing the import system.
Right. JSON is almost valid Python to begin with. Off the top of my head, the main differences are that JSON true needs to be True in Python (likewise with false/False, null/None). It's not that difficult.
Earlier quoted context omitted.
Does that respect import search paths? Does the original?
Does 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.
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…
1. https://tomforb.es/automatically-inline-python-function-call...
Earlier quoted context omitted.
Right. JSON is almost valid Python to begin with. Off the top of my head, the main differences are that JSON true needs to be True in Python (likewise with false/False, null/None). It's not that difficult.
It's also slightly stricter with syntax; you can't have extra ending commas. {'foo' : 1, 'bar' : 2,} is valid Python but not valid json.
Earlier quoted context omitted.
It's also slightly stricter with syntax; you can't have extra ending commas. {'foo' : 1, 'bar' : 2,} is valid Python but not valid json.
Yes, I keep running into this. I leave trailing commas in my JSON as it evals fine in Python. Then my C++ code, using boost::property_tree chokes on it. property_tree also needs double quotes, not the single quotes in this example.
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…
>>> 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.)
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.)
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…
Import hooks are awesome, one of the cool things you can do is re-write the code as you load it. I made a toy loader that automagically inlines (some) Python function calls[1]. There are also cool projects like MacroPy[2] that do much more extensive things. 1. https://tomforb.es/automatically-inline-python-function-call... 2. https://github.com/lihaoyi/macropy
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.