Skip to main content

No project description provided

Project description

YAML配置文件读取工具

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

安装

git clone git@git.frontnode.net:liuyf/yingmi-yaml.git
cd yingmi-yaml
pip install -e .

TODO:编译后上传到私有pypi仓库

python setup.py bdist_whell register -r yingmiyaml upload -r 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.2-py3-none-any.whl (6.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: YingMiYAML-1.0.2-py3-none-any.whl
  • Upload date:
  • Size: 6.9 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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 c3f43078b3ec56cda51e80a0d8e948be3af34fd67fcbdc7f1d731d822d835007
MD5 02be5fe9e803dd08fccd28802a1cd08a
BLAKE2b-256 04b97839d1529a12e44eb4e79cfd38c6d93e686918173dca59f3e862adad3718

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