python类resolver()的实例源码

persistence.py 文件源码 项目:configmanager 作者: jbasko 项目源码 文件源码 阅读 30 收藏 0 点赞 0 评论 0
def __init__(self, **options):
        super(YamlReaderWriter, self).__init__(**options)

        try:
            import yaml
            import yaml.resolver
        except ImportError:
            raise RuntimeError('To use YAML, please install PyYAML first')

        #
        # The code to preserve order of items is taken from here:
        # https://stackoverflow.com/a/21048064/38611
        #

        _mapping_tag = yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG

        def dict_representer(dumper, data):
            return dumper.represent_dict(data.items())

        def dict_constructor(loader, node):
            return collections.OrderedDict(loader.construct_pairs(node))

        yaml.add_representer(collections.OrderedDict, dict_representer)
        yaml.add_constructor(_mapping_tag, dict_constructor)

        self.yaml = yaml

        self.default_dump_options = {
            'indent': 2,
            'default_flow_style': False,
        }
kubeconfig.py 文件源码 项目:seedbox 作者: nailgun 项目源码 文件源码 阅读 26 收藏 0 点赞 0 评论 0
def _dict_representer(dumper, data):
    return dumper.represent_mapping(yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG, data.items())


问题


面经


文章

微信
公众号

扫码关注公众号