Oslo命令行解析

转自http://www.muzixing.com/pages/2014/12/19/ryuxue-xi-oslo.html
首先安装python-virtualenv,此python库可以用于创建一个虚拟的,与外界隔离的运行环境,听起来和docker好像有点像。

sudo apt-get install python-virtualenv
virtualenv example-app
cd example-app
source bin/activate
pip install oslo.config
touch app.py
touch app.conf

然后修改app.conf。添加了两个group:simple和morestuff。simple组中有一个BoolOpt:enable。morestuff组有StrOpt, ListOpt, DictOpt, IntOpt,和FloatOpt。

[simple]

enable = True

[morestuff]

# StrOpt
message = Hello World

# ListOpt
usernames = ['Licheng', 'Muzixing', 'Distance']

# DictOpt
jobtitles = {'Licheng': 'Manager', 'Muzixing': 'CEO', 'Distance': 'Security Guard'}

# IntOpt
payday = 20

# FloatOpt
pi = 3.14
修改app.py文件。首先定义两个group,再对两个group的option进行定义。最后使用register_group和register_opts函数来完成group和option的注册。
from __future__ import print_function
from oslo_config import cfg


opt_simple_group = cfg.OptGroup(name='simple',
                         title='A Simple Example')

opt_morestuff_group = cfg.OptGroup(name='morestuff',
                         title='A More Complex Example')

simple_opts = [
    cfg.BoolOpt('enable', default=False,
                help=('True enables, False disables'))
]

morestuff_opts = [
    cfg.StrOpt('message', default='No data',
               help=('A message')),
    cfg.ListOpt('usernames', default=None,
                help=('A list of usernames')),
    cfg.DictOpt('jobtitles', default=None,
                help=('A dictionary of usernames and job titles')),
    cfg.IntOpt('payday', default=30,
                help=('Default payday monthly date')),
    cfg.FloatOpt('pi', default=0.0,
                help=('The value of Pi'))
]

CONF = cfg.CONF

CONF.register_group(opt_simple_group)
CONF.register_opts(simple_opts, opt_simple_group)

CONF.register_group(opt_morestuff_group)
CONF.register_opts(morestuff_opts, opt_morestuff_group)


if __name__ == "__main__":
    CONF(default_config_files=['app.conf'])
    print('(simple) enable: {}'.format(CONF.simple.enable))
    print('(morestuff) message :{}'.format(CONF.morestuff.message))
    print('(morestuff) usernames: {}'.format(CONF.morestuff.usernames))
    print('(morestuff) jobtitles: {}'.format(CONF.morestuff.jobtitles))
    print('(morestuff) payday: {}'.format(CONF.morestuff.payday))
    print('(morestuff) pi: {}'.format(CONF.morestuff.pi))
完成之后,运行app.py文件。可以查看到相关输出。

这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值