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

Add expand_iter kw to unparse to expand iterables #213

Merged
merged 1 commit into from Sep 19, 2019

Conversation

claweyenuk
Copy link
Contributor

Add expand_iter kwarg to unparse that will expand lists into sets of XML elements rather than outputting the string representation of the list

This is more of a proposal than actual PR - I should probably update the README and add a test. But before doing that I wanted to submit this.

Using a simple dict:

test_dict = {'root': {'imgs': ((1, 2), (3, 4))}}

Output before the change/with out the flag:

print(xmltodict.unparse(test_dict, pretty=True))
<?xml version="1.0" encoding="utf-8"?>
<root>
	<imgs>(1, 2)</imgs>
	<imgs>(3, 4)</imgs>
</root>

After:

print(xmltodict.unparse(test_dict, expand_iter="item", pretty=True))
<?xml version="1.0" encoding="utf-8"?>
<root>
	<imgs>
		<item>1</item>
		<item>2</item>
	</imgs>
	<imgs>
		<item>3</item>
		<item>4</item>
	</imgs>
</root>

@yan-hic
Copy link

yan-hic commented Jul 7, 2019

Is this PR/enhancement stalled ? I have a similar requirement for a list of dicts.
Currently iterating and unparse each item, and joining the XML result. It would be cleaner - with less overhead - to do in one operation.

@martinblech
Copy link
Owner

This looks like a great addition, thanks for your contribution! Could you update the PR with tests and docs?

@claweyenuk
Copy link
Contributor Author

No stall really. I just avoided adding tests in case this was not a useful addition.

I am a bit tight on time for the next few weeks, so I won't be able to add tests just yet. Feel free to use the code or add tests.

Add `expand_iter` kwarg to unparse that will expand lists into sets of XML elements rather than outputting the string representation of the list
@claweyenuk
Copy link
Contributor Author

Much longer than 3 weeks, but I gave this another go with tests and some documentation. Please let me know if there's anything missing/incorrect!

@claweyenuk
Copy link
Contributor Author

@martinblech just a heads up to make sure this is on your radar

@martinblech
Copy link
Owner

Thank you so much for this contribution!

@martinblech martinblech merged commit 587a7c4 into martinblech:master Sep 19, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants