2
\$\begingroup\$

For some time now, I've been using a pattern which I think could be done more elegantly. I have a lot of XML files that need to be represented in a Django views. My current approach is like this:

I get a XML response from an API which gives me several documents.

First I define a mock class like this:

class MockDocument:
 pass

Then I write an XPath expression that gives me a list of the document nodes:

from lxml import etree 
with open("xmlapiresponse.xml", "r") as doc:
 xml_response = doc.read() 
tree = etree.fromstring(xml_response) 
document_nodes = tree.xpath("//documents/item") 

Now I iterate over the different documents and their fields using nested for-loops:

# list for storing the objects
document_objects = []
for document in document_nodes:
 # create a MockDocument for each item returned by the API
 document_object = MockDocument() 
 for field in document:
 # store the corresponding fields as object attributes
 if field.tag == "something":
 document.something = field.text
 document_objects.append(document) 

Now I can pass the list of mock objects to a Django view and represent them in the template in whichever way I want. Could this be done in a simpler fashion, though?

Jamal
35.2k13 gold badges134 silver badges238 bronze badges
asked Jul 22, 2011 at 10:48
\$\endgroup\$

1 Answer 1

4
\$\begingroup\$

Using a generator function gives you some flexibility. You don't have to create a list.

def generate_documents( document_nodes ):
 for document in document_nodes:
 # create a MockDocument for each item returned by the API
 document_object = MockDocument() 
 for field in document:
 # store the corresponding fields as object attributes
 if field.tag == "something":
 document.something = field.text
 yield document_object 

You can do this of the list object is somehow important.

document_objects = list( generate_documents( document_nodes ) )

Or, more usefully, this

for d in generate_documents( document_nodes ):
 ... process the document ...

Which never actually creates a list object.

answered Jul 22, 2011 at 15:24
\$\endgroup\$
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.