Skip to main content

No project description provided

Project description

YAML配置文件读取工具

  • yaml语法支持
  • 支持多profile配置

安装

pip install yingmiyaml

示例

  • 配置文件
# 全局
app:
  name: spark-kyc
  server:
    port: 8082
  profile:
    active: prod
urls:
  - baidu.com
  - aliyun.com
  - tencent.com

# 不同profile
---
$profile: dev 
mysql:
  host: localhost
  user: root
  password: xxxx
  db: testing

---
$profile: prod
mysql:
  host: localhost
  user: root
  password: xxxx
  db: prod-db
# profile中的与全局相同key会按key路径覆盖
app:
  server:
    port: 80

这个yaml配置文件包含两个profile: prod, dev。

  1. 使用--- 三横杠分割profile,每个profile使用
  2. 每个profile通过$profile字段命名
  3. 支持通过命令行指定profile:--profile=dev
  • 实例代码参考example.py
import os
from yingmi.yaml import YAML, get_yaml_config, get_nested_dict_value

cur_dir = os.path.abspath(os.path.dirname(__file__))
yaml_file = os.path.join(cur_dir, 'app.yml')

if __name__ == '__main__':
    # YAML对象可以重复使用
    # 指定profile
    yaml = YAML(yaml_file)
    config = yaml.get_profile('prod')
    print(config.get('mysql'))
    config = yaml.get_profile('dev')
    print(config['mysql'])

    # 未指定profile参数时,会从命令行参数读取--profile,如果没有这个参数,则会读取配置文件中的app.profile.active字段
    print('--- cmd argument or app.profile.active---')
    config = yaml.get_profile()
    print(config)

    # 便利性函数 get_yaml_config(yaml_file, profile=None)
    config = get_yaml_config(yaml_file)
    print(config.get('mysql'))
    print(config['app']['server']['port'])
    print(get_nested_dict_value(config,'app.server.port'))
    
    #多级key直接读取
    print(config['app.server.port'])
    # 取list
    print(config['urls'])
    # 取list对象的某个下标
    print(config['urls.[1]'])

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

YingMiYAML-1.0.3-py3-none-any.whl (6.8 kB view details)

Uploaded Python 3

File details

Details for the file YingMiYAML-1.0.3-py3-none-any.whl.

File metadata

  • Download URL: YingMiYAML-1.0.3-py3-none-any.whl
  • Upload date:
  • Size: 6.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.8.2

File hashes

Hashes for YingMiYAML-1.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 a694a62cf1f54f6214757987c9212321c970e4eedb7a845e0e2335b5ae3bc1f8
MD5 9f392b50b80fb06a2800b6388ec593ff
BLAKE2b-256 9eccbb6c3c86a5e5dbc281366ddf4a667b10a5aa27738111673664b4ed42fb04

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page