我有一个无法转储的对象simplejson,所以我需要先从它创建一个列表.目前这就是我正在使用的:
messages = h.flash.pop_messages()
items = []
for message in messages:
item = {}
item['category'] = message.category
item['message'] = message.message
items.append(item)
Run Code Online (Sandbox Code Playgroud)
我觉得有更多的pythonic方式让我做这个,有人可以解决一些问题吗?
编辑:
根据要求,这是Message对象的类:
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)
Run Code Online (Sandbox Code Playgroud)
items = [{'category': m.category, 'message': m.message}
for m in h.flash.pop_messages()]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
130 次 |
| 最近记录: |