跳转到主要内容

yaml-table混合

项目描述

yamtable |🍠✨|

一个美观的✨格式化库,专为需要显示复杂或模糊结构数据的CLI设计,例如来自REST API的数据。

想法是,有时您拥有的数据最好以YAML的形式显示,因为它非常适合显示字典。有时您可能有一系列字典(例如,具有属性的实体列表),并以表格形式显示将更好,因为YAML会使您的数据表示几乎无法扫描。

有时您可能会有这两种类型的数据。例如,您可能有看起来最好作为YAML的一些顶级键,但在其中还包含表格数据。

关键是,如果能够在不重新编写和自定义数据格式化程序的情况下以良好的方式打印出这些混合数据,那不是很好吗?很多时候,您需要首先查看数据,才能了解正在发生什么以及如何进行格式化!

import yamtable
# some messy object
yamtable.dump({
    'blah0': {
        'something': [{'is_up': False, 'value_A': 0.5367493221202239, 'value_B': 100.0825738163817, 'id': 2588}, {'is_up': True, 'value_A': 0.4559654642937784, 'value_B': 100.76840781583714, 'id': 2259}], 
        'other thing': [{'is_up': False, 'value_A': 0.5881412394062349, 'value_B': 100.18253046678744, 'id': 4778}], 
        'third thing': [1, 2, 3]}, 
    'blah1': {
        'something': [{'is_up': False, 'value_A': 0.6647093697365306, 'value_B': 100.15798391354355, 'id': 2477}, {'is_up': True, 'value_A': 0.8701196743925997, 'value_B': 100.45672829258778, 'id': 4183}], 
        'other thing': [{'is_up': False, 'value_A': 0.9113837527694413, 'value_B': 100.05392635622654, 'id': 2553}], 
        'third thing': [1, 2, 3]}
})

啊哈哈哈!

blah0: 
  something: 
      value_A  is_up      value_B    id
    ---------  -------  ---------  ----
        0.894  🥀         100.515  3566
        0.379  🌹         100.345  4560
  other thing: 
      value_A  is_up      value_B    id
    ---------  -------  ---------  ----
        0.218  🥀         100.781  1311
  third thing: 
    - 1
    - 2
    - 3
blah1: 
  something: 
      value_A  is_up      value_B    id
    ---------  -------  ---------  ----
        0.203  🌹         100.998  4346
        0.995  🌹         100.094  3308
  other thing: 
      value_A  is_up      value_B    id
    ---------  -------  ---------  ----
        0.455  🥀         100.621  1686
  third thing: 
    - 1
    - 2
    - 3

安装

pip install yamtable

用法

import yamtable

# some test data
import random
def sensor_data(n=8):
    return [
        {
            'is_up': random.choice([False, True]), 
            'value_A': random.random(), 
            'value_B': random.random() + 100,
            'id': random.randint(1000, 5000)
        } 
        for i in range(n)
    ]

yamtable.dump(sensor_data())

作者笔记

这是一个有趣的宠物项目,以便我可以停止在CLI项目之间复制变动的代码。这不是为了成熟或对所有类型的数据都万无一失。

由于它处理深层嵌套的数据,因此难以找到从顶层直观自定义行为的方法。从字典级别定义多层配置可能不是最佳选择,但如果您想自定义行为,可以直接覆盖类方法。请参阅examples/custom.py中的示例。

待办事项

  • 添加限制显示数据的能力,有时表格可能太大
  • 如何在不同的系统和主题下使颜色易于阅读且美观

更多示例

$ python examples/nested.py

wow: 
  blah0: 
    something: 
      +---------+-----------+-----------+------+
      | is_up   |   value_B |   value_A |   id |
      +=========+===========+===========+======+
      | 🥀      |       101 |     0.597 | 4211 |
      +---------+-----------+-----------+------+
      | 🥀      |       101 |     0.11  | 2245 |
      +---------+-----------+-----------+------+
    other thing: 
      +---------+-----------+-----------+------+
      | is_up   |   value_B |   value_A |   id |
      +=========+===========+===========+======+
      | 🌹      |       100 |    0.0576 | 1272 |
      +---------+-----------+-----------+------+
    third thing: 
      - 1
      - 2
      - 3
    oooh: 
      +-----------------------+-------+-------+
      | aaa                   | bbb   |   ccc |
      +=======================+=======+=======+
      | okay: hm              | - 1   |     5 |
      | hmm: interesting      | - 2   |       |
      | more?: wow: srsly?    |       |       |
      +-----------------------+-------+-------+
      | okay: wow             | - 1   |     5 |
      | hmm: interesting      | - 2   |       |
      | more?: wow: srsly?    |       |       |
      +-----------------------+-------+-------+
      | okay: wowwow          | - 1   |     5 |
      | hmm: interesting      | - 2   |       |
      | more?:                |       |       |
      |   is it too much: umm |       |       |
      |   maybe: ...          |       |       |
      +-----------------------+-------+-------+
  blah1: 
    something: 
      +---------+-----------+-----------+------+
      | is_up   |   value_B |   value_A |   id |
      +=========+===========+===========+======+
      | 🥀      |       101 |     0.223 | 1315 |
      +---------+-----------+-----------+------+
      | 🥀      |       101 |     0.409 | 2428 |
      +---------+-----------+-----------+------+
    other thing: 
      +---------+-----------+-----------+------+
      | is_up   |   value_B |   value_A |   id |
      +=========+===========+===========+======+
      | 🌹      |       101 |      0.68 | 4227 |
      +---------+-----------+-----------+------+
    third thing: 
      - 1
      - 2
      - 3
    oooh: 
      +-----------------------+-------+-------+
      | aaa                   | bbb   |   ccc |
      +=======================+=======+=======+
      | okay: hm              | - 1   |     5 |
      | hmm: interesting      | - 2   |       |
      | more?: wow: srsly?    |       |       |
      +-----------------------+-------+-------+
      | okay: wow             | - 1   |     5 |
      | hmm: interesting      | - 2   |       |
      | more?: wow: srsly?    |       |       |
      +-----------------------+-------+-------+
      | okay: wowwow          | - 1   |     5 |
      | hmm: interesting      | - 2   |       |
      | more?:                |       |       |
      |   is it too much: umm |       |       |
      |   maybe: ...          |       |       |
      +-----------------------+-------+-------+
$ python examples/fancy_columns.py

Without fancy columns:
  value_C    id  desc                value_B    value_A  name      something    is_up
---------  ----  ----------------  ---------  ---------  --------  -----------  -------
    -99.1  4395  this is sensor 0        101      0.72   Sensor 0  asdfjasdf    🌹
    -99.1  3523  this is sensor 1        100      0.899  Sensor 1  asdfjasdf    🥀
    -99.1  4136  this is sensor 2        100      0.905  Sensor 2  asdfjasdf    🌹
    -99.6  2474  this is sensor 3        101      0.236  Sensor 3  asdfjasdf    🌹
    -99    3817  this is sensor 4        101      0.6    Sensor 4  asdfjasdf    🌹
    -99.2  1471  this is sensor 5        101      0.192  Sensor 5  asdfjasdf    🥀
    -99.2  3353  this is sensor 6        101      0.24   Sensor 6  asdfjasdf    🥀
    -99    2630  this is sensor 7        101      0.665  Sensor 7  asdfjasdf    🌹

With fancy columns: 'is_up, id|name/desc, ..., value_*'
is_up    id | name / desc    something    value_A | value_B | value_C
-------  ------------------  -----------  -----------------------------
🌹       1157 | Sensor 0     asdfjasdf    0.343 | 101 | -99.7
         this is sensor 0
🥀       4963 | Sensor 1     asdfjasdf    0.214 | 101 | -99.1
         this is sensor 1
🌹       3172 | Sensor 2     asdfjasdf    0.297 | 101 | -99
         this is sensor 2
🥀       1533 | Sensor 3     asdfjasdf    0.948 | 100 | -99.4
         this is sensor 3
🌹       4236 | Sensor 4     asdfjasdf    0.251 | 100 | -99
         this is sensor 4
🥀       4697 | Sensor 5     asdfjasdf    0.799 | 100 | -100
         this is sensor 5
🥀       4500 | Sensor 6     asdfjasdf    0.406 | 100 | -99.5
         this is sensor 6
🥀       3251 | Sensor 7     asdfjasdf    0.49 | 100 | -99.8
         this is sensor 7

更多示例

  • examples/bools.py:查看所有布尔值变化及其自定义方式
  • examples/custom.py:查看如何更改格式
  • examples/table_formats.py:查看所有表格格式变化
  • ...

项目详情


下载文件

下载适用于您平台的文件。如果您不确定选择哪个,请了解有关安装包的更多信息。

源代码分发

yamtable-0.0.1.tar.gz (11.4 kB 查看哈希值)

上传时间 源代码

由以下支持

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF 赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误日志 StatusPage StatusPage 状态页面