本文整理汇总了Python中invenio.modules.records.api.Record.dumps方法的典型用法代码示例。如果您正苦于以下问题:Python Record.dumps方法的具体用法?Python Record.dumps怎么用?Python Record.dumps使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类invenio.modules.records.api.Record
的用法示例。
在下文中一共展示了Record.dumps方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: formatter
# 需要导入模块: from invenio.modules.records.api import Record [as 别名]
# 或者: from invenio.modules.records.api.Record import dumps [as 别名]
def formatter(bwo, **kwargs):
"""Nicely format the record."""
from pprint import pformat
from invenio.modules.records.api import Record
data = bwo.get_data()
if not data:
return ''
formatter = kwargs.get("formatter", None)
of = kwargs.get("of", None)
if formatter:
# A separate formatter is supplied
return formatter(data)
if isinstance(data, collections.Mapping):
# Dicts are cool on its own, but maybe its SmartJson (record)
try:
data = Record(data.dumps()).legacy_export_as_marc()
except (TypeError, KeyError):
pass
if isinstance(data, string_types):
# We can try formatter!
# If already XML, format_record does not like it.
if of and of != 'xm':
try:
from invenio.modules.formatter import format_record
formatted_data = format_record(
recID=None,
of=of,
xml_record=data
)
except TypeError:
# Wrong kind of type
pass
else:
# So, XML then
from xml.dom.minidom import parseString
try:
unpretty_data = parseString(data)
formatted_data = unpretty_data.toprettyxml()
except TypeError:
# Probably not proper XML string then
return "Data cannot be parsed: %s" % (data,)
except Exception:
# Just return raw string
pass
if not formatted_data:
formatted_data = data
if isinstance(formatted_data, dict):
formatted_data = pformat(formatted_data)
return formatted_data
示例2: formatter
# 需要导入模块: from invenio.modules.records.api import Record [as 别名]
# 或者: from invenio.modules.records.api.Record import dumps [as 别名]
def formatter(bwo, **kwargs):
"""Return a formatted version of the data."""
from invenio.modules.formatter.engine import format_record
data = bwo.get_data()
if not data:
return ''
formatter = kwargs.get("formatter", None)
format = kwargs.get("format", None)
if formatter:
# A seperate formatter is supplied
return formatter(data)
from invenio.modules.records.api import Record
if isinstance(data, collections.Mapping):
# Dicts are cool on its own, but maybe its SmartJson (record)
try:
data = Record(data.dumps()).legacy_export_as_marc()
except (TypeError, KeyError):
# Maybe not, submission?
return data
if isinstance(data, string_types):
# Its a string type, lets try to convert
if format:
# We can try formatter!
# If already XML, format_record does not like it.
if format != 'xm':
try:
return format_record(recID=None,
of=format,
xml_record=data)
except TypeError:
# Wrong kind of type
pass
else:
# So, XML then
from xml.dom.minidom import parseString
try:
pretty_data = parseString(data)
return pretty_data.toprettyxml()
except TypeError:
# Probably not proper XML string then
return "Data cannot be parsed: %s" % (data,)
except Exception:
# Some other parsing error
pass
# Just return raw string
return data
if isinstance(data, set):
return list(data)
# Not any of the above types. How juicy!
return data