记录集格式化程序
rsformat的Python项目详细描述
格式
基于集合和行的结果筛选器和格式化程序
概述
rformat是一个嵌套的列表处理程序,有助于管理:
- 多个集合或列表
- 有序输出
- 列函数
- 默认值
基于配置,它重新格式化并以新结构返回数据。rformat可以用作运行时格式化程序,在调用时提供配置,也可以进行预配置,将新的结果集传递给已配置的rformat对象。rformat可以合并到流处理器中,用于流水线。
示例
当您有一个标准查询结果格式或一个非规范化的行时,rformat非常好,但是不同的使用者需要自定义视图。这将格式转换为基于配置的操作,而不是对底层数据服务器的显式发布。
请考虑在请求包含多个部分的报告时可能会收到的以下数据结构:
report = [
[
{ "_id": 123, "first": "Jane", "middle": None, "last": "Smith"}
],
[
{ "_id": 201, "account_no": "2984039756", "acct_type": "Checking", "name": "Checking", "branch_id": "1024309", "branch_name": "Chase North Clybourn", "address": "2790 N Clybourn Ave", "balance": 4280.80, "open_date": "20060512T00:00:00Z" },
{ "_id": 202, "account_no": "4528929834", "acct_type": "Savings", "name": "Rainy Day", "branch_id": "3490002", "branch_name": "Chase Lakeview", "address": "3215 N Lincoln", "balance": 23802.27, "opened_on": "20030305T00:00:00Z"}
],
[
{ "_id": 10980, "account_no": "2984039756", "acct_type": "Checking", "name": "Checking", "branch_id": "1024309", "branch_name": "Chase North Clybourn", "address": "2790 N Clybourn Ave", "debit_credit": "debit", "amt": 430.30 },
{ "_id": 10981, "account_no": "4528929834", "acct_type": "Savings", "name": "Rainy Day Fund", "branch_id": "3490002", "branch_name": "Chase Lakeview", "address": "3215 N Lincoln", "debit_credit": "credit", "amt": 1250.00 },
{ "_id": 10982, "account_no": "2984039756", "acct_type": "Checking", "name": "Checking", "branch_id": "1024309", "branch_name": "Chase North Clybourn", "address": "2790 N Clybourn Ave", "debit_credit": "debit", "amt": 102.12 },
{ "_id": 10984, "account_no": "2984039756", "acct_type": "Checking", "name": "Checking", "branch_id": "1024309", "branch_name": "Chase North Clybourn", "address": "2790 N Clybourn Ave", "debit_credit": "debit", "amt": 17.65 }
]
]
使用下面的rformat配置,我们只显示我们关心的字段,并与配置相比重新排序
set0:
0:
col: first
alias: first_name
1:
col: last
alias: last_name
set1:
0:
col: acct_type
alias: type
format:
- type: string
func: upper
- type: string
func: format
tmpl: "{0} Account"
1:
col: account
alias: name
2:
col: account_number
alias: account_no
3:
col: opened_on
alias: opened
formats:
- type: date
from: "%Y%m%dT%H%M%SZ"
to: "%Y-%m-%d"
set3:
0:
col: _id
alias: transaction_id
1:
col: account_no
alias: account_number
2:
col: debit_credit
format:
- type: mapping
maps:
from: "credit", to: "CREDIT (+)"
from: "debit", to: "DEBIT (-)"
3:
alias: amount
col: amt
然后数据以下面的结构返回,可以直接传递给负责写入数据的任何人。
[
[
{ "first_name": "Jane", "last_name": "Smith"}
],
[
{ type": "CHECKING Account", "account": "Checking", "account_number": "2984039756", "opened": "2006-05-12"},
{ type": "SAVINGS Account", "account": "Rainy Day", "account_number": "4528929834", "opened": "2003-03-05"}
],
[
{ "transaction_id": 10980, "account_number": "2984039756", "debit_credit": "DEBIT (-)", "amount": 430.30 },
{ "transaction_id": 10981, "account_number": "4528929834", "debit_credit": "CREDIT (+)", "amount": 1250.00 },
{ "transaction_id": 10982, "account_number": "2984039756", "debit_credit": "DEBIT (-)", "amount": 102.12 },
{ "transaction_id": 10984, "account_number": "2984039756", "debit_credit": "DEBIT (-)", "amount": 17.65 }
]
]
ordermap配置使用整数和浮点键进行排序。