I have an object that can’t be dumped by simplejson, so I need to create a list from it first. Currently this is what I’m using:
messages = h.flash.pop_messages()
items = []
for message in messages:
item = {}
item['category'] = message.category
item['message'] = message.message
items.append(item)
I feel like there is a more pythonic way for me to be doing this, can anyone shed some light?
Edit:
As requested, this is the class for the Message object:
class Message(object):
"""A message returned by ``Flash.pop_messages()``.
Converting the message to a string returns the message text. Instances
also have the following attributes:
* ``message``: the message text.
* ``category``: the category specified when the message was created.
"""
def __init__(self, category, message):
self.category=category
self.message=message
def __str__(self):
return self.message
__unicode__ = __str__
def __html__(self):
return escape(self.message)
1 Answer