-
Notifications
You must be signed in to change notification settings - Fork 148
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
base: master
Are you sure you want to change the base?
Conversation
question: why
instead of just
|
This is a good approach as well. I just took the suggestion showed in the issue mentioned in my original post. |
I reviewed your suggestion and I came to the conclusion that we could go in your direction, which means: We allow the my_dict = {
"Family": {
"@Tree": "Menelaws",
"Name": "Lewis Menelaws",
"Occupation": "Programmer",
"Programming Language": "Python"
}
} Where anything after the 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. |
analysis of code showed: you @attr solution has better performance, thus i would stuck to your solution. |
So, can we merge this? It's an interesting feature! |
Last commit was posted on 7 Jul 2016. Unsure if this package is still maintained. |
@quandyfactory can we get a response from you? Would love to see this merged 😄 |
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. |
Problem: What if Occupation should have an attribute? Seems that support for a new style Occupation@myattr or "@attrs": { "Occupation.myattr": val} is needed. |
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 Also note the 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. |
@quandyfactory do you know when this PR will be merged? |
Guys, xmltodict.unparse do the trick just fine :) |
Shame that I have to completely ignore this project due to this feature still not being supported... |
@BSpendlove what did you end up using? |
dicttoxml was moved into https://github.com/vinitkumar/json2xml |
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:
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
:You will get this as the output:
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.