On 8/28/20 6:56 AM, Steve Barnes wrote:
This is just about the first part of your mail: There are external
libraries for reading and writing various formats while preserving
comments (and even more, e.g. spacing): For Python files, there is
RedBaron, for YAML there is ruamel.yaml,
This is the key point -- each text file format has its own structure, etc, there is no "standard" that could be generally supported.
And on the Python side, there is no standard way to represent arbitrary text files as Python objects. Sure, for the most part,most file readers build up a tree of dicts and lists, but where would comments fit into that -- how would you even know which object to attach the comment to? that specification would have to be part of the file format -- kind of like docstrings, actually. there is a spec in Python code for where to put strings that you want to preserve and "attach" to certain objects. But notably, they are NOT comments -- comments, by their very nature, are more free form.
and JSON doesn't allow
comments as far as I know.
It does not -- though many extensions to JSON do -- notably JSON-5 -- which I wish were supported in the stdlib. But this makes a good point, if you want conforming JSON, you can't use comments at all -- so maybe your own stqndard would be helpful:
an_object = {"__comment__": " an arbitrary comment about this object",
"this": "a regular attribute",
"that": [3,4,5,6]}
And then you could have a "CommentedDict" Python object as well.
But having said all that -- I have, in fact, written my own file format that looked a lot like INI files, but had comments thqt were preserved. It is nice to have a simple way to handle comments in the raw file format, while still capturing them. And tthe JSON example above is not simpel enough for easy text editor action.
Maybe a standard way to do it with YAML would be doable.
So *maybe* a set of "commented" versions of the core builtins would be useful.