🦋
Butterfly 用户手册
  • Introduction
  • 一 前言
  • 二 开始
    • 安装部署
    • 五分钟体验指南
    • 单机使用手册
    • 应用规范
      • handler specs
      • middleware specs
      • xingqiao_plugin specs
      • yiqiu_program specs
  • 三 客户端功能
    • MySQL 原生协议
    • MySQL ORM
    • Redis 原生协议
      • redis_config
      • redis_tls
    • Redis ORM
    • Redis mcpack
    • Localcache
    • Kazoo
  • 四 应用(通用服务)
    • API JSON 规范
    • 异步任务 BaiChuan(百川)
    • 任务调度 RuQi(如期)
    • 任务编排 XingQiao(星桥)
    • 配置管理 WuXing(五行)
    • 运筹决策 BaiCe(百策)
  • 五 部署运维
    • 单机容器化部署
    • 监控
    • 异常排查
      • CPU Load spike every 7 hours
    • 升级
    • 安全
    • 其他
  • 六 前端
    • butterfly_template
    • butterfly_fe
    • butterfly-admin(json2web)
      • amis
      • sso
      • pangu
    • NoahV
    • PyWebIO
  • 七 潘多拉魔盒
    • 装饰器
      • localcache_decorator
      • retry_decorator
      • custom_decorator
      • command2http_decorator
    • 算法
      • 算法-分位数
      • 算法-变异系数
    • 实用工具
      • host_util
      • shell_util
      • http_util
      • time_util
      • random_util
      • concurrent
      • jsonschema
      • blinker
      • toml
      • command_util
      • config_util
      • picobox
      • 对称加密
        • des
        • aes
      • ascii_art
        • ttable
        • chart
      • business_rules
      • python-mysql-replication
      • dict_util
    • 中间件
      • middleware_status
      • middleware_whitelist
    • test_handler.py
  • 八 最佳实践
    • 分布式架构
    • Code practice
    • Log practice
    • Daemon process
  • 附录
Powered by GitBook
On this page
  • 1 toml 例子
  • 2 使用
  • 2.1 toml list
  1. 七 潘多拉魔盒
  2. 实用工具

toml

1 toml 例子

[owner]
name = "Tom Preston-Werner"
organization = "Github"
bio = "Github Cofounder & CEO\nLikes tater tots and beer."
dob = 1979-05-27T07:32:00Z # 日期时间是一等公民.为什么不呢?

[database]
server = "192.168.1.1"
ports = [ 8001, 8001, 8002 ]
connection_max = 5000
enabled = true

[servers]

  # 您可以依照您的意愿缩进.使用空格或Tab.TOML不会在意.
  [servers.alpha]
  ip = "10.0.0.1"
  dc = "eqdc10"

  [servers.beta]
  ip = "10.0.0.2"
  dc = "eqdc10"

[clients]
data = [ ["gamma", "delta"], [1, 2] ]

解析为 dict

{
    u'owner': {
        u'dob': datetime.datetime(1979, 5, 27, 7, 32, tzinfo=<xlib.util.toml.tz.TomlTz object at 0x7f2e2ba706d0>),
        u'organization': u'Github',
        u'name': u'Tom Preston-Werner',
        u'bio': u'Github Cofounder &amp; CEO\nLikes tater tots and beer.'
    },
    u'servers': {
        u'alpha': {u'ip': u'10.0.0.1', u'dc': u'eqdc10'},
        u'beta':  {u'ip': u'10.0.0.2', u'dc': u'eqdc10'}
    },
    u'clients': {
        u'data': [[u'gamma', u'delta'], [1, 2]]
    },
    u'database': {
        u'enabled': True,
        u'ports': [8001, 8001, 8002],
        u'connection_max': 5000,
        u'server': u'192.168.1.1'
    }
}

2 使用

from xlib.util import toml

# Parse a file or a list of files as TOML and return a dictionary.
toml.load(f, _dict=dict)

# Parse a TOML-formatted string to a dictionary.
toml.loads(s, _dict=dict)

# Write a dictionary to a file containing TOML-formatted data
toml.dump(o, f, encoder=None)

# Create a TOML-formatted string from an input object
toml.dumps(o, encoder=None)

# eg:
with open("./blb.toml", 'r') as f:
    toml_config = toml.load(f)
    print toml_config

with open("./blb2.toml", 'w') as f:
    toml.dump(toml_config, f)

2.1 toml list

[[user_list]]
    UserID = "user1"
[[user_list]]
    UserID = "user2"
[[user_list]]
    UserID = "user3"

转为 json

{
    "user_list": [
        {
            "UserID": "user1"
        },
        {
            "UserID": "user2"
        },
        {
            "UserID": "user3"
        }
    ]
}
PreviousblinkerNextcommand_util

Last updated 1 year ago