Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

flatten method should return a line for each element in a list #29

Open
Loreton opened this issue Sep 9, 2020 · 2 comments
Open

flatten method should return a line for each element in a list #29

Loreton opened this issue Sep 9, 2020 · 2 comments
Assignees
Labels
enhancement New feature or request

Comments

@Loreton
Copy link

Loreton commented Sep 9, 2020

I need that flatten method return {"key_path": value} also for elements in a list in a dictionary.
For the following dict:

test:
  d10:
    d11: single
    d12:
      crontab: ["cron_01", "cron_02"]
      minidlna: ["mini_01", "mini_02"]
    d13:
      - dict01: ciao_01
      - dict02: ciao_02

dict.flatten() return:

{
    "test/d10/d11": "single",
    "test/d10/d12/crontab": ["cron_01", "cron_02"],
    "test/d10/d12/minidlna": ["mini_01", "mini_02"],
    "test/d10/d13": [{"dict01": "ciao_01"}, {"dict02": "ciao_02"} ]
}

I would like to get something like:

{
    "test/d10/d11": "single",
    "test/d10/d12/crontab/[0]": "cron_01",
    "test/d10/d12/crontab/[1]": "cron_02",
    "test/d10/d12/minidlna/[0]": "mini_01",
    "test/d10/d12/minidlna/[1]": "mini_02",
    "test/d10/d13/[0]/dict01": "ciao_01",
    "test/d10/d13/[1]/dict02": "ciao_02"
}

where the list-index could be '[i]' or just 'ì' or anything easily identifiable.
Attached is a sample of flatten module, modified from the original, to return the above output if explode_lists=True is passed as argument.
Do you think this can be done?

My wish would go further and ask if benedict could be able to use the returned path to point to the value of the single element of list.

Regards
Loreto

flatten.zip

Fund with Polar
@Loreton Loreton added the enhancement New feature or request label Sep 9, 2020
@fabiocaccamo
Copy link
Owner

fabiocaccamo commented Sep 10, 2020

@Loreton thank you for this suggestion, this issue is related to list index support as many other issues. List index support must be implemented at a lower level in this library.

Anyway, the unflatten method should be reverse correctly the result of the flatten method.
It means that this should not raise an Exception: assert d == unflatten(flatten(d))

@Loreton
Copy link
Author

Loreton commented Sep 11, 2020

If the list index will be implemented I think that also my request will be covered. In this case my issue is redundant.
Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Status: Todo
Development

No branches or pull requests

2 participants