No project description provided
Project description
YAML Profiles Parser
- YAML syntax support
- Support multi profile configuration
Installation
pip install yaml.profiles
Examples
- yaml configuration file: app.yml
# global
app:
name: spark-kyc
server:
port: 8082
profile:
active: prod
urls:
- baidu.com
- aliyun.com
- tencent.com
# the profile
---
$profile: dev
mysql:
host: localhost
user: root
password: xxxx
db: testing
# the other profile
---
$profile: prod
mysql:
host: localhost
user: root
password: xxxx
db: prod-db
# The same key in profile will be overwrite global key
app:
server:
port: 80
This yaml configuration file contains two profile: prod, dev。
- Use
---single line split profiles - Use
$profilefor each profile segment - Determine activation priority:
- Pass the profile parameter when calling the function
- Command line argument
--profile=xxx - Check the key
app.profiles.activein the configuration file
- example.py :
import os
from yaml.profiles 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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
YamlProfiles-0.0.1.tar.gz
(5.1 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file YamlProfiles-0.0.1.tar.gz.
File metadata
- Download URL: YamlProfiles-0.0.1.tar.gz
- Upload date:
- Size: 5.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.24.0 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.8.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
698a5cd73325d76c0b1a82c8082fe89bf614140b57e8f81e3a937fb43ea3ced7
|
|
| MD5 |
42b549009de86af1800f0f24abd84eb0
|
|
| BLAKE2b-256 |
2651880b238381d33845683c63ddb9e5935247416c81f600337f6ebd3f1c7950
|
File details
Details for the file YamlProfiles-0.0.1-py3-none-any.whl.
File metadata
- Download URL: YamlProfiles-0.0.1-py3-none-any.whl
- Upload date:
- Size: 10.5 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.24.0 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.8.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bc32bb70aed268045b256aada20666dba33c21e26a5a7a41045bf8e99485a1b4
|
|
| MD5 |
4867e84e5793fbf4f6d5434ae52c9e01
|
|
| BLAKE2b-256 |
dd43749f680168d98ad657af6eb057ae4f5a9d5773ad67ecc26803ccd7479ebe
|