Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Unique identifier for <item> containers #7

Closed
cpetz opened this Issue · 3 comments

2 participants

@cpetz

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

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.

@cpetz

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

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
Something went wrong with that request. Please try again.