2

I have an issue with Python/Django where I have either a String or a List of Strings being passed to a Template. If it is a list I need to iterate through it and output each String on a separate line, and if it is a String I just need to output the string. I need to be able to differentiate between the value types and adjust accordingly.

Currently I have code similiar to this:

if isinstance(values, list):
    for value in values:
        html += value + "<br />"
else:
    html += values + "<br />"

My problem is twofold:

Firstly, I am looking for a better/more pythonic way (if possible) to do this which will have the same result. I know that for some situations the isinstance method is not an ideal solution, but would I be able to utilize something like hasattr and would it be an improvement in terms of efficiency?

Secondly, I would ideally want this to be implemented using the Django Template language. If I continue using isinstance or change to hasattr I would have to either make a custom Template filter or tag to be able to pass the proper arguments. Should I forget the template and just write a the code that generates HTML in the view (bad practice) or would the answers to one of these be the best approach for my situation? (Performing a getattr() style lookup in a django template or django template system, calling a function inside a model)

The current template code can be found here: http://pastebin.com/JK2PRrWv

Background:

I am currently working on some Python(Django) code that implements a simple REST/Json API used to handle queries. One of the requirements is to convert a list of Python dictionaries(parsed from JSON) into very simple HTML tables. To implement this functionality, I have utilized a Django template that takes the list of Python dictionaries and generates HTML from it.

Any help/constructive criticism would be greatly appreciated.

Community
  • 1
  • 1
Wert
  • 133
  • 1
  • 8
  • 5
    what makes you say it is not advisible to use `isinstance`? – karthikr Jun 25 '13 at 23:09
  • "I have either a String or a List of Strings being passed to a Template." I'm *preeeetty* sure this violates some programming principle somewhere; I just can't keep all the names in my head at once. – Ignacio Vazquez-Abrams Jun 26 '13 at 00:59
  • show us where you use it, maybe you need a good refactor. – Leandro Jun 26 '13 at 02:33
  • karthikr: I probably should have phrased that differently. I edited the post. Ignacio: Yes you're right. Unfortunately the problem stems from the list of JSON objects that is parsed in, of which I have no control over the contents. lalo: For more context you can look here: http://pastebin.com/JK2PRrWv. Thanks for the feedback! – Wert Jun 26 '13 at 16:22
  • You should have control over the view code that gives you the parsed json right? You should be able to modify that then. – Henrik Andersson Jun 27 '13 at 06:09
  • limelights: That is true, however in my situation efficiency is key as I have a large list of dictionaries being parsed in (raw JSON is on the order of ~2 mb). I would prefer to iterate through this large list as few times as possible. If you think there is a way that I could maintain efficiency while doing this that would be a much better solution. Thanks! – Wert Jun 27 '13 at 17:05

1 Answers1

0

isinstance is probably what you want to distinguish between a string and another iterable, but you should compare with basestring (from which all strings are derived) rather than list. See this question and answer.

if isinstance(values, basestring):
    html += values + "<br />"
else:
    for value in values:
        html += value + "<br />"
Community
  • 1
  • 1
Brenda J. Butler
  • 1,475
  • 11
  • 20
  • Thank you! I did look over that question previously but it turns out I should've read it a little more carefully. – Wert Jun 27 '13 at 17:51