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

First edition of custom attributes. Work in Progress #66

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

elebumm
Copy link

@elebumm elebumm commented Jan 22, 2019

I needed something that would allow me to create custom attributes. This was discussed in #27. I added a hack that will allow you to create custom attributes in similar fashion to how @mstrcnvs mentioned in his solution.

Here is how it works:

my_dict = {
    "Family": {
        "@attrs": {
            "Tree": "Menelaws"
        },
        "Name": "Lewis Menelaws",
        "Occupation": "Programmer",
        "Programming Language": "Python"
    }
}

I create a dictionary as usual. I want my "Family" node to have a custom attribute of "Tree" with the value being "Menelaws". I do this by creating a key at the first position called "@attrs" and having a key/value of what I want there to be.

Currently, I am unsure if multiple key value will work. This is still a work in development. After you do a standard dicttoxml:

xml = dicttoxml.dicttoxml(
    my_dict,
    custom_root='FamilyTree',
    attr_type=False
)

You will get this as the output:

<?xml version="1.0" encoding="UTF-8" ?>
<FamilyTree>
    <Family Tree="Menelaws">
        <Name>Lewis Menelaws</Name>
        <Occupation>Programmer</Occupation>
        <Programming_Language>Python</Programming_Language>
    </Family>
</FamilyTree>

I would love to see some feedbacks/code review and some input on how I can make this better or what features to add.

Thanks for the awesome package. It's helped me a lot.

@DirkRichter
Copy link

question: why

"@attrs": { "a1": 1, "a2":2 }

instead of just

"@a1": 1, "@a2":2

@elebumm
Copy link
Author

elebumm commented Jan 23, 2019

This is a good approach as well. I just took the suggestion showed in the issue mentioned in my original post.

@elebumm
Copy link
Author

elebumm commented Jan 25, 2019

I reviewed your suggestion and I came to the conclusion that we could go in your direction, which means:

We allow the @ symbol to be the only deciding factor as to what makes an attribute. For example, we could use this:

my_dict = {
    "Family": {
        "@Tree": "Menelaws",
        "Name": "Lewis Menelaws",
        "Occupation": "Programmer",
        "Programming Language": "Python"
    }
}

Where anything after the @ symbol is considered an attribute with the following value being the value of the attribute.

In my opinion, I like the readability of my solution better but I would like the opinions of other contributors and users of this package. Let me know what you think guys.

@DirkRichter
Copy link

analysis of code showed: you @attr solution has better performance, thus i would stuck to your solution.

@jklemm
Copy link

jklemm commented Feb 4, 2019

So, can we merge this? It's an interesting feature!

@elebumm
Copy link
Author

elebumm commented Feb 5, 2019

Last commit was posted on 7 Jul 2016. Unsure if this package is still maintained.

@elebumm
Copy link
Author

elebumm commented Feb 28, 2019

@quandyfactory can we get a response from you? Would love to see this merged 😄

@elebumm
Copy link
Author

elebumm commented Aug 12, 2019

I've been using this package for a while now and opened this back in January. Depending on the needs, I may actively maintain my forked version of this repository if some people are requiring some bug fixes.

@DirkRichter
Copy link

Problem: What if Occupation should have an attribute? Seems that support for a new style Occupation@myattr or "@attrs": { "Occupation.myattr": val} is needed.

@elebumm
Copy link
Author

elebumm commented Oct 24, 2019

You're right. Having the key value method makes it hard to create single nodes with attributes. My solution seems to work with parent nodes. The solution you provide seems like it would work for naming the key of the attribute but not the value. Perhaps we can redefine how we add these custom attributes like such:

my_dict = {
    "Family": {
        "@meta": {
            "Tree": "Menelaws",
            "members": 50
        }, 
        "Name": "Lewis Menelaws",
        "Occupation": {
            "@meta": {
                "Level": "Senior"
            },
            "@val": "Programmer"
        },
        "Programming Language": "Python"
    }
}

Would give a result of:

<?xml version="1.0" encoding="UTF-8" ?>
<FamilyTree>
    <Family Tree="Menelaws" members=50>
        <Name>Lewis Menelaws</Name>
        <Occupation Level="Senior">Programmer</Occupation>
        <Programming_Language>Python</Programming_Language>
    </Family>
</FamilyTree>

Take note, that I created the @meta keyword, where it would hold a dict of the attribute key and the value.

Also note the @val keyword. By doing this, it overrides the dictionary and places it within the node itself rather than creating a parent node.

I don't mind this implementation, but I do have some issues with it.

Mainly, the false sense of hierarchy. I feel as if it's un-python-like to have it visualized in a way that doesn't resemble the final product. Having all of this:

"Occupation": {
            "@meta": {
                "Level": "Senior"
            },
            "@val": "Programmer"
        }

all to resemble:

<Occupation Level="Senior">Programmer</Occupation

Let me know if you have a solution you can work up. Would love to investigate further.

@iamzafar
Copy link

@quandyfactory do you know when this PR will be merged?

@Pslon
Copy link

Pslon commented Sep 25, 2020

@quandyfactory do you know when this PR will be merged?

Guys, xmltodict.unparse do the trick just fine :)

@BSpendlove
Copy link

Shame that I have to completely ignore this project due to this feature still not being supported...

@bfontaine
Copy link

@BSpendlove what did you end up using?

@DirkRichter
Copy link

dicttoxml was moved into https://github.com/vinitkumar/json2xml
have a look on unit tests method dict_with_attrs: https://github.com/vinitkumar/json2xml/blob/master/tests/test_dict2xml.py#L149

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.

7 participants