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

Make folding for lists optional #64

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

wisotzky
Copy link

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
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

Looks as if this fixes #59

Copy link

@Simon-Campbell Simon-Campbell left a comment

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.

if fold_list:
item_name = item_func(parent)
else:
item_name = parent

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.

@@ -366,7 +372,7 @@ def convert_none(key, val, attr_type, attr={}, cdata=False):


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):
Copy link

@Simon-Campbell Simon-Campbell Dec 28, 2018

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 😄

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.

@@ -11,7 +11,7 @@

from __future__ import unicode_literals

__version__ = '1.7.4'
__version__ = '1.8.0'

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

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
Copy link

Simon-Campbell commented Jan 17, 2019

@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

@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
Copy link

mzmssg commented May 27, 2019

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

@beigna
Copy link

beigna commented Jan 31, 2020

Is @quandyfactory alive?

@imbue
Copy link

imbue commented Mar 3, 2021

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

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

Ousret commented Aug 13, 2022

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.