Skip to content

Turn --flatten into a documented utility function #500

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

Closed
simonw opened this issue Oct 18, 2022 · 4 comments
Closed

Turn --flatten into a documented utility function #500

simonw opened this issue Oct 18, 2022 · 4 comments
Labels
enhancement New feature or request python-library

Comments

@simonw
Copy link
Owner

simonw commented Oct 18, 2022

The --flatten implementation isn't currently available to Python code - people have to roll their own implementation. Feedback from a conversation at DjangoCon.

@simonw simonw added enhancement New feature or request python-library labels Oct 18, 2022
@simonw
Copy link
Owner Author

simonw commented Oct 18, 2022

Here's how it works:

if flatten:
rows = (dict(_flatten(row)) for row in rows)

def _flatten(d):
for key, value in d.items():
if isinstance(value, dict):
for key2, value2 in _flatten(value):
yield key + "_" + key2, value2
else:
yield key, value

@simonw
Copy link
Owner Author

simonw commented Oct 18, 2022

@simonw
Copy link
Owner Author

simonw commented Oct 18, 2022

I think the public interface is a flatten(row) function that does dict(_flatten(row)).

@simonw simonw closed this as completed in 34e75ed Oct 18, 2022
@simonw
Copy link
Owner Author

simonw commented Oct 18, 2022

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request python-library
Projects
None yet
Development

No branches or pull requests

1 participant