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

Unique identifier for <item> containers #7

Closed
cpetz opened this issue Apr 29, 2013 · 3 comments
Closed

Unique identifier for <item> containers #7

cpetz opened this issue Apr 29, 2013 · 3 comments
Assignees

Comments

@cpetz
Copy link

cpetz commented Apr 29, 2013

Add some way to uniquely identify consecutive tag pairs in the same nested level. As per your suggestion, an incremented tag attribute should do the trick.Should probably be optional with a flag passed to dicttoxml().

I assume the unique id will be akin to the item/array index number that the tag represents, and will reset for each nested level. (globally unique seems silly, but on that note..)

A longer term goal might be to accept an iterator from the user at the beginning of each sequence; leaving flexibility open without excessive complication. The trick might be embedding the iterator. Maybe perform a typecheck on the 0th index of any sequence, and if a custom class instance is found, pop it from the sequence and process it as an attibute value iterator.
We can let this last one ferment for a while ;) Thanks for your hard work.

@quandyfactory
Copy link
Owner

Excellent suggestion. As a preliminary stab at how to do this, consider the following example dict:

obj = {
    'mylist': [
        'foo', 
        'bar', 
        'baz'
    ], 
    'mydict': {
        'foo': 'bar', 
        'baz': 1
    }, 
    'ok': True
}

If we want to give each element a unique id and have the ids of list items increment, how about something like the following XML output:

<?xml version="1.0" ?>
<root>
        <mylist id="root_925614" type="list">
                <item id="mylist_572945_1" type="str">foo</item>
                <item id="mylist_572945_2" type="str">bar</item>
                <item id="mylist_572945_3" type="str">baz</item>
        </mylist>
        <mydict id="root_725410" type="dict">
                <foo id="mydict_951836" type="str">bar</foo>
                <baz id="mydict_194434" type="int">1</baz>
        </mydict>
        <ok id="root_215928" type="bool">true</ok>
</root>

The dicttoxml function generates a unique random ID for each element based on the parent element. For list items, the id is in the form parent_unique_index.

What do you think? Does this meet your needs/expectations? If so, I'll be happy to push this change to production.

To enable it, one would assign True to an optional ids argument, like so:

dicttoxml.dicttoxml(some_dict, ids=True)

The default behaviour will remain the same as it is today so as not to break compatibility for existing uses.

@ghost ghost assigned quandyfactory Apr 30, 2013
@cpetz
Copy link
Author

cpetz commented May 3, 2013

I think that sounds good. I will be implementing the bulk of my XML functions soon, so I will let you know if anything else comes to mind. Thanks again.

@quandyfactory
Copy link
Owner

This feature is added in v. 1.1. Thanks again for suggesting it!

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

No branches or pull requests

2 participants