记录集格式化程序

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配置使用整数和浮点键进行排序。

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java Jetty是否有请求缓存?   数组中的java 2值与我的数据帧中的2列对应   对象序列化期间的java DbUtils类型转换问题   java根面板中不显示所有单独的面板   java通过代理或SSH隧道连接Hbase API   java困惑:与经典MVC控制器相比,JSF2中bean的角色   java在我的Triangle类中“找不到符号错误”   java在Android中设置从路径到自定义按钮的图像   java不绕轨道旋转椭圆   AES在socket上搞砸了序列化/反序列化。无效的流标头。JAVA   java在数组中搜索term和if语句   线程“main”java中出现异常。util。正则表达式。PatternSyntaxException:在索引0附近悬挂元字符“*”*   Websphere上的java Google反射无法打开url连接   java为什么inc/dec频道什么都不做?   java KeyListener无法访问keyPressed方法