content = '''!Config
title: config title
description: this is an example config. configyml support tree structure.
tree: !Config
a:
- 1
- !Config
b: 2
c: 3
d: 4
'''
with open('config.yml', 'w') as f:
f.write(content)
from configyml import set_path
set_path('config.yml')
from configyml import arg
print(arg)
print(arg.tree.a[1].b)
Config
Object¶from configyml import Config
Config
Object¶config = Config(
title='title example',
description='this is an example config.',
nest=Config(
description='also support nested structure.'
)
)
config
import yaml
s = yaml.dump(config)
from configyml import dump
s = yaml.dump(config)
dump == yaml.dump
print(len(config), '\n')
for key, val in config:
print(key)
print(val)
print()
argparse
¶The Config
Object inherit the argument structure Namespace
of our beloved argparse
module.
So, serialize your arguments with ease.
import argparse
argparse.Namespace = Config
# An example modified from https://docs.python.org/3/library/argparse.html
parser = argparse.ArgumentParser(description='Process some integers.')
parser.add_argument('integers', metavar='N', type=int, nargs='+',
help='an integer for the accumulator')
parser.add_argument('--sum', dest='accumulate', action='store_const',
const=sum, default=max,
help='sum the integers (default: find the max)')
args = parser.parse_args('1 2 3 4 5 --sum'.split())
print(args)
args.accumulate(args.integers)
Config
and write your own class¶from configyml import register
@register
class MyConfig(Config):
def __getitem__(self, item):
return self.__dict__[item]
content = '''!Config
title: config title
myconfig: !MyConfig
a: 3
b: 4
c: 5
'''
myconfig = yaml.load(content).myconfig
myconfig
myconfig['c']