Live data from Hacker News

Use JSON files as if they are Python modules

github.com

31–40 of 67 posts

Re: Use JSON files as if they are Python modules

#32

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.

To note: Python 3, due to the whole string vs unicode literals thing (\u)

Re: Use JSON files as if they are Python modules

#33
post #25

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.

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.

Re: Use JSON files as if they are Python modules

#34

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

Re: Use JSON files as if they are Python modules

#35

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.

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.

Re: Use JSON files as if they are Python modules

#36

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.

There's a module for this: json. The error messages might not be very good, but it does check for trailing commas.

Re: Use JSON files as if they are Python modules

#37

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…

For what it's worth - modern JavaScript does it too, it separates the syntax (import/export) and semantics of binding the reference from the actual loading mechanism. Not only can you "hook" into imports, you can replace them altogether if you'd like.

Re: Use JSON files as if they are Python modules

#38
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.)

Re: Use JSON files as if they are Python modules

#39
post #38

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.)

Also, 'file' doesn't exist in Python 3.

Re: Use JSON files as if they are Python modules

#40
post #34

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

Awesome, I saw your post a while back and it triggered me to write a full inliner for python, also added some IFDEF macros using context managers e.g.

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.

Post reply on HN