Skip to content

Make folding for lists optional#64

Open
wisotzky wants to merge 1 commit into
quandyfactory:masterfrom
wisotzky:fold_list
Open

Make folding for lists optional#64
wisotzky wants to merge 1 commit into
quandyfactory:masterfrom
wisotzky:fold_list

Conversation

@wisotzky

Copy link
Copy Markdown

Give users the ability to define dictionary or a list, where the dictionary key becomes the item name of list entries.

This pull request is merging PR#42 with the latest release 1.7.4 of dicttoxml.

Give users the ability to define dictionary or a list, where the dictionary key becomes the item name of list entries.
@wisotzky

Copy link
Copy Markdown
Author

For the sake of documentation, when using the option fold_list=False the parameter item_func is ignored. In case of nested lists, all list entries will use the same parent dictionary name as item name.

To control the list item names per hierarchy level, a combination of nested dicts and lists shall be used.

Example:

{'book': [{'title': 'Python Programming', 'license': 'GPL', 'author': ['Adam', 'Benny', 'Charlie']}, {'license': 'Apache 2.0', 'title': 'Business Modelling'}]}

Output:

<?xml version=\"1.0\" encoding=\"UTF-8\" ?>
<root>
  <book>
    <title>Python Programming</title>
    <license>GPL</license>
    <author>Adam</author>
    <author>Benny</author>
    <author>Charlie</author>
  </book>
  <book>
    <license>Apache 2.0</license>
    <title>Business Modelling</title>
  </book>
</root>

@BobStein

Copy link
Copy Markdown

Looks as if this fixes #59

@Simon-Campbell Simon-Campbell left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, thanks for taking the time!

I am not sure if the upstream repo is accepting PRs but have left some comments to consider.

I'll pull this code into my own fork so I can make use of it.

Comment thread dicttoxml.py
if fold_list:
item_name = item_func(parent)
else:
item_name = parent

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe you could explicitly fail-fast if item_func is explicitly input into dicttoxml API and fold_list is also explicitly defined to be False? Might be preferable instead of silently ignoring and causing a surprise.

Comment thread dicttoxml.py

def dicttoxml(obj, root=True, custom_root='root', ids=False, attr_type=True,
item_func=default_item_func, cdata=False):
item_func=default_item_func, cdata=False, fold_list=True):

@Simon-Campbell Simon-Campbell Dec 28, 2018

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An alternative design may be to vary the result of item_func for folding. What do you think about something along the lines of the following?

# Using a dict may be a bit lazy here, but it should communicate the idea. You'd have 
# to check the return type in places that use the result for backward compat.
def custom_item_func(parent):
    if parent == 'Special':
        return { 'name': parent, 'should_fold': True } 
    else:
        return { 'name': parent, 'should_fold': False }

dicttoxml(…, item_func = custom_item_func)

This would enable you to vary the behavior based on the element or some condition. It may also simplify the number of parameters being passed about in the implementation?

As another note: in VS Code that there is no doc comment for the new parameter, it might be worth adding it to help with visibility and documentation 😄

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually need this feature. because in a specification I'm implementing both cases are necessary.

Comment thread dicttoxml.py
from __future__ import unicode_literals

__version__ = '1.7.4'
__version__ = '1.8.0'

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might also want to bump this in setup.py so it's recognized by pip :-)

@sonaysevik

Copy link
Copy Markdown

Hi @BobStein and @Simon-Campbell,

fold_list feature looks useful for me as well. Do you know when it will be released to pip?

Thanks

@Simon-Campbell

Simon-Campbell commented Jan 17, 2019

Copy link
Copy Markdown

@sonaysevik I am not a maintainer, that question would have to be directed at @quandyfactory 👍 At the moment I have forked the branch I need and am pulling it into pip using a zip e.g.

pip install https://github.com/Simon-Campbell/dicttoxml/archive/b0777caf0ccf64c97a9c47a70b9ced6e0c51ddf7.zip

GitHub has a feature to pull zip based on commit hash. I'm using this as a poor man's version lock for the package.

@optik-aper

Copy link
Copy Markdown

@Simon-Campbell @quandyfactory I could use this functionality to meet schema requirements by a vendor but I can't in good conscience deploy production using a commit hash on a fork. Any plans to merge this into master?

@mzmssg

mzmssg commented May 27, 2019

Copy link
Copy Markdown

The same requirements as @optik-aper, is there any blocking issues of merging?

@devedetti

Copy link
Copy Markdown

Is @quandyfactory alive?

@imbue

imbue commented Mar 3, 2021

Copy link
Copy Markdown

Any possibility that this PR can be merged, or is there something I can help with to get it merged?

Ousret pushed a commit to Ousret/dicttoxml that referenced this pull request Aug 13, 2022
@Ousret

Ousret commented Aug 13, 2022

Copy link
Copy Markdown

Taken and fixed in (fork) https://github.com/Ousret/dicttoxml/releases/tag/2.1.0
Available on PyPi https://pypi.org/project/dicttoxml2

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.