You've given some examples of what flatten() does, but I still don't really understand how it's expected to work. Could you provide a specification, rather than just examples? Also, I've never needed a function like this in anything I've ever done - that may mean nothing, but it does suggest that you need to demonstrate that it's a generally useful tool (maybe by pointing to examples of "real world" code in various contexts that would be improved by using it). There's a lot of pressure to keep the stdlib for things that are widely applicable, and the burden of demonstrating that lies with the proposer.
As the proposal stands here, I honestly don't see anything that suggests this wouldn't be better as a standalone function published on PyPI. Paul On Wed, 21 Jul 2021 at 13:43, Sven Voigt <svenpvo...@gmail.com> wrote: > > Hello Python-ideas, > > I am working with lots of JSON objects in my work and need to obtain JSON > pointers to particular components of a JSON object. These pointers are > defined as part of the JSON-LD specifications as relative IRIs and as part of > the JSON-Schema specifications. However, I am not aware of these pointers > being part of the JSON specification. The pointers are really straightforward > and follow the same syntax as other IRIs (URLs) or file paths. For example, > the following flatten function illustrates the mapping of nested dictionaries > and arrays to a single flat dictionary, where each nested key is mapped to a > unique path. > > d = {"a": 1, "b": {"ba": 2, "bb": [{"bba": 3, "bbb": None}]}} > flatten(d) > >> {'a': 1, 'b/ba': 2, 'b/bb/0/bba': 3, 'b/bb/0/bbb': None} > > Not only does this conversion help in generating JSON pointers, but it helps > with logic for nested data structures. Specifically, assume there is a > dictionary, which includes nested dictionaries, lists, and tuples, and any of > these elements are contained within another dictionary or that other > dictionaries elements. Then it is not sufficient to simply compare the items > of the two dictionaries to check whether the first is a subset of the other. > However, the flat structures can be compared directly. > > 1. A nested dictionary is a subset of another nested dictionary. The flatten > function ensures the nested dictionaries are not checked for equivalence but > subset of. > # Current > {"a": 1, "c": {"d": 4}}.items() <= {"a": 1, "b": 2, "c": {"d": 4, "e": > 5}}.items() > >> False > # Proposed > flatten({"a": 1, "c": {"d": 4}}).items() <= flatten({"a": 1, "b": 2, "c": > {"d": 4, "e": 5}}).items() > >> True > > 2. A nested list or tuple is a subset of a dictionary. > # Current > {"a": 1, "c": [3]}.items() <= {"a": 1, "b": 2, "c": [3,3]}.items() > >> False > # Proposed > flatten({"a": 1, "c": [3]}).items() <= flatten({"a": 1, "b": 2, "c": > [3,3]}).items() > >> True > > Note that these examples only have one level of nesting, but the flatten > function must handle any level of nesting. For the current version of the > flatten function, I essentially borrowed the JSON "dumps" source code. > However, the dumps code doesn't utilize any other functions and I believe > there may be a missing "nestedIter" function in python. This function could > be used for JSON dumps or flatten or anytime someone wants to visit all > nested elements of a dictionary and perform some operation. Therefore, I > think this function could be included in the JSON library if python-ideas > thinks that the pointers are very specific to JSON or the itertools library, > where the nested iteration function could be generally used for all nested > data types. > > Let me know what you think of this proposal and I am looking forward to your > responses. > > Best, > Sven > _______________________________________________ > Python-ideas mailing list -- python-ideas@python.org > To unsubscribe send an email to python-ideas-le...@python.org > https://mail.python.org/mailman3/lists/python-ideas.python.org/ > Message archived at > https://mail.python.org/archives/list/python-ideas@python.org/message/V2DNTLXII2QNYSE3FJXRO45ZFTCMBZ5G/ > Code of Conduct: http://python.org/psf/codeofconduct/ _______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-le...@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/4LMSOSMOIYN6263FWCFFQQ22C6A7PC5X/ Code of Conduct: http://python.org/psf/codeofconduct/