pythoncheetah

Looping through a collection has different behaviour if source has only one item with Python Cheetah3


I'm trying to write some code that will parse a collection/list/array etc and I had this working when the size is greater than 1. However when only 1 item is in the collection the code fails as the behaviour appears to be different. Using the below input

<?xml version="1.0" encoding="UTF-8"?>
<example code="HI">
    <collection1>
        <item>
            <name>item1</name>
        </item>
        <item>
            <name>item2</name>
        </item>
    </collection1>
    <collection2>
        <item>
            <name>item3</name>
        </item>
    </collection2>
</example>

and the following code

#!/usr/bin/env python

import xmltodict
from Cheetah.Template import Template


with open('example.xml', 'r') as f:
    example_xml = f.read()
example_dict = xmltodict.parse(example_xml)

templateDefinition = "Collection 1:\n" \
                     "#for $item in $example.collection1.item\n" \
                     "$item\n" \
                     "#end for\n" \
                     "Collection 2:\n" \
                     "#for $item in $example.collection2.item\n" \
                     "$item\n" \
                     "#end for\n"
template = Template(templateDefinition, searchList=[example_dict])
print(str(template))

I get this output

Collection 1:
{'name': 'item1'}
{'name': 'item2'}
Collection 2:
name

Why does collection 2 not return a single item of {'name': 'item3'}?


Solution

  • Not a problem in Cheetah at all. Try this:

    #!/usr/bin/env python
    
    import xmltodict
    
    with open('example.xml', 'r') as f:
        example_xml = f.read()
    example_dict = xmltodict.parse(example_xml)
    
    example = example_dict['example']
    items = example['collection2']['item']
    print(items)
    
    for item in items:
        print(item)
    

    In short: example['collection1']['item'] is a list of dicts while example['collection2']['item'] is just a dict. The bottom line: verify your data structures.