当前位置: 首页>>代码示例>>Python>>正文


Python yaml.add_constructor方法代码示例

本文整理汇总了Python中yaml.add_constructor方法的典型用法代码示例。如果您正苦于以下问题:Python yaml.add_constructor方法的具体用法?Python yaml.add_constructor怎么用?Python yaml.add_constructor使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在yaml的用法示例。


在下文中一共展示了yaml.add_constructor方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: demo

# 需要导入模块: import yaml [as 别名]
# 或者: from yaml import add_constructor [as 别名]
def demo():
    constructor = CustodiaSimpleConstructor(
        'http+unix://%2E%2Fserver_socket/secrets'
    )
    constructor.client.headers['REMOTE_USER'] = 'user'

    # create entries
    try:
        c = constructor.client.list_container('test')
    except HTTPError:
        constructor.client.create_container('test')
        c = []
    if 'key' not in c:
        constructor.client.set_secret('test/key', 'secret password')

    yaml.add_constructor(CustodiaSimpleConstructor.yaml_tag,
                         constructor)
    yaml_str = 'password: !custodia/simple test/key'
    print(yaml_str)
    result = yaml.load(yaml_str)
    print(result) 
开发者ID:latchset,项目名称:custodia,代码行数:23,代码来源:yaml_ext.py

示例2: get_config

# 需要导入模块: import yaml [as 别名]
# 或者: from yaml import add_constructor [as 别名]
def get_config(dir='config/config.yaml'):
    # add direction join function when parse the yaml file
    def join(loader, node):
        seq = loader.construct_sequence(node)
        return os.path.sep.join(seq)

    # add string concatenation function when parse the yaml file
    def concat(loader, node):
        seq = loader.construct_sequence(node)
        seq = [str(tmp) for tmp in seq]
        return ''.join(seq)

    yaml.add_constructor('!join', join)
    yaml.add_constructor('!concat', concat)
    with open(dir, 'r') as f:
        cfg = yaml.load(f)

    check_dirs(cfg)

    return cfg 
开发者ID:iMoonLab,项目名称:HGNN,代码行数:22,代码来源:config.py

示例3: get_config

# 需要导入模块: import yaml [as 别名]
# 或者: from yaml import add_constructor [as 别名]
def get_config(dir):
    # add direction join function when parse the yaml file
    def join(loader, node):
        seq = loader.construct_sequence(node)
        return os.path.sep.join(seq)

    # add string concatenation function when parse the yaml file
    def concat(loader, node):
        seq = loader.construct_sequence(node)
        return ''.join(seq)

    yaml.add_constructor('!join', join)
    yaml.add_constructor('!concat', concat)
    with open(dir, 'r') as f:
        cfg = yaml.load(f)

    return cfg 
开发者ID:iMoonLab,项目名称:DHGNN,代码行数:19,代码来源:config.py

示例4: _config_loader

# 需要导入模块: import yaml [as 别名]
# 或者: from yaml import add_constructor [as 别名]
def _config_loader():
    envvar_matcher = re.compile(r"\${([^}^{]+)\}")

    def envvar_constructor(_loader, node):  # pragma: no cover
        """Extract the matched value, expand env variable, and replace the match."""
        node_value = node.value
        match = envvar_matcher.match(node_value)
        env_var = match.group()[2:-1]

        # check for defaults
        var_name, default_value = env_var.split(":")
        var_name = var_name.strip()
        default_value = default_value.strip()
        var_value = os.getenv(var_name, default_value)
        return var_value + node_value[match.end() :]

    yaml.add_implicit_resolver("!envvar", envvar_matcher, None, SafeLoader)
    yaml.add_constructor("!envvar", envvar_constructor, SafeLoader)


# TODO: instead of this, create custom loader and use it
#       by wrapping yaml.safe_load to use it 
开发者ID:fetchai,项目名称:agents-aea,代码行数:24,代码来源:loader.py

示例5: initialize

# 需要导入模块: import yaml [as 别名]
# 或者: from yaml import add_constructor [as 别名]
def initialize():
    """
    Initialize the configuration system by installing YAML handlers.
    Automatically done on first call to load() specified in this file.
    """
    global is_initialized

    # Add the custom multi-constructor
    yaml.add_multi_constructor('!obj:', multi_constructor_obj)
    yaml.add_multi_constructor('!pkl:', multi_constructor_pkl)
    yaml.add_multi_constructor('!import:', multi_constructor_import)

    yaml.add_constructor('!import', constructor_import)
    yaml.add_constructor("!float", constructor_float)

    pattern = re.compile(SCIENTIFIC_NOTATION_REGEXP)
    yaml.add_implicit_resolver('!float', pattern)

    is_initialized = True


###############################################################################
# Callbacks used by PyYAML 
开发者ID:zchengquan,项目名称:TextDetector,代码行数:25,代码来源:yaml_parse.py

示例6: setup_yaml

# 需要导入模块: import yaml [as 别名]
# 或者: from yaml import add_constructor [as 别名]
def setup_yaml():
    _mapping_tag = yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG
    yaml.add_representer(collections.OrderedDict, dict_representer)
    yaml.add_representer(os._Environ, dict_representer)
    yaml.add_representer(unicode, unicode_representer)
    yaml.add_constructor(_mapping_tag, dict_constructor) 
开发者ID:datawire,项目名称:forge,代码行数:8,代码来源:util.py

示例7: testIfTableIsAMap

# 需要导入模块: import yaml [as 别名]
# 或者: from yaml import add_constructor [as 别名]
def testIfTableIsAMap(self):

        from yaml.constructor import ConstructorError

        try:
            from yaml import CLoader as Loader
        except ImportError:
            from yaml import Loader

        # Credit https://gist.github.com/pypt/94d747fe5180851196eb
        def no_duplicates_constructor(loader, node, deep=False):
            """Check for duplicate keys."""

            mapping = {}
            for key_node, value_node in node.value:
                key = loader.construct_object(key_node, deep=deep)
                value = loader.construct_object(value_node, deep=deep)
                if key in mapping:
                    raise ConstructorError(
                        "while constructing a mapping", node.start_mark,
                        "found duplicate key (%s)" % key, key_node.start_mark)
                mapping[key] = value

            return loader.construct_mapping(node, deep)

        yaml.add_constructor(yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG, no_duplicates_constructor)

        file_path = '../translationtable/GLOBAL_TERMS.yaml'
        if os.path.exists(os.path.join(os.path.dirname(__file__), file_path)):
            tt_file = open(os.path.join(os.path.dirname(__file__), file_path), 'r')
            try:
                translation_table = yaml.safe_load(tt_file)
            except yaml.constructor.ConstructorError as e:
                tt_file.close()
                self.assertTrue(False)
                print(e)
            tt_file.close() 
开发者ID:monarch-initiative,项目名称:dipper,代码行数:39,代码来源:test_trtable.py

示例8: load

# 需要导入模块: import yaml [as 别名]
# 或者: from yaml import add_constructor [as 别名]
def load(s, encoding='utf-8'):

    # override to set output encoding
    def construct_yaml_str(loader, node):
        value = loader.construct_scalar(node)

        if encoding is not None:
            return value.encode(encoding)

        return value

    yaml.add_constructor(u'tag:yaml.org,2002:str', construct_yaml_str)

    return yaml.load(s) 
开发者ID:bsc-s2,项目名称:pykit,代码行数:16,代码来源:utfyaml.py

示例9: LoadConfigDict

# 需要导入模块: import yaml [as 别名]
# 或者: from yaml import add_constructor [as 别名]
def LoadConfigDict(config_paths, model_params):
  """Loads config dictionary from specified yaml files or command line yaml."""

  # Ensure that no duplicate keys can be loaded (causing pain).
  yaml.add_constructor(yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG,
                       NoDuplicatesConstructor)

  # Handle either ',' or '#' separated config lists, since borg will only
  # accept '#'.
  sep = ',' if ',' in config_paths else '#'

  # Load flags from config file.
  final_config = {}
  if config_paths:
    for config_path in config_paths.split(sep):
      config_path = config_path.strip()
      if not config_path:
        continue
      config_path = os.path.abspath(config_path)
      tf.logging.info('Loading config from %s', config_path)
      with tf.gfile.GFile(config_path.strip()) as config_file:
        config_flags = yaml.load(config_file)
        final_config = DeepMergeDict(final_config, config_flags)
  if model_params:
    model_params = MaybeLoadYaml(model_params)
    final_config = DeepMergeDict(final_config, model_params)
  tf.logging.info('Final Config:\n%s', yaml.dump(final_config))
  return final_config 
开发者ID:rky0930,项目名称:yolo_v2,代码行数:30,代码来源:util.py

示例10: read

# 需要导入模块: import yaml [as 别名]
# 或者: from yaml import add_constructor [as 别名]
def read(self, filepath):
        stream = False

        # Setup environment variables replacement
        def env_var_single_replace(match):
            return os.environ[match.group(1)] if match.group(1) in os.environ else match.group()

        def env_var_multi_replacer(loader, node):
            value = loader.construct_scalar(node)

            return re.sub(ENV_VAR_REPLACER_PATTERN, env_var_single_replace, value)

        yaml.add_implicit_resolver("!envvarreplacer", ENV_VAR_MATCHER_PATTERN)
        yaml.add_constructor('!envvarreplacer', env_var_multi_replacer)

        # Read file
        try:
            stream = open(filepath, "r")
            self.config = yaml.load(stream)

            # Handle an empty configuration file
            if not self.config:
                self.config = {}
        finally:
            if stream:
                stream.close() 
开发者ID:spreaker,项目名称:prometheus-pgbouncer-exporter,代码行数:28,代码来源:config.py

示例11: register_tag

# 需要导入模块: import yaml [as 别名]
# 或者: from yaml import add_constructor [as 别名]
def register_tag(tag, classpath):
    yaml.add_constructor(u'!'+tag, metaloader(classpath))
    yaml.add_constructor(u'tag:nltk.org,2011:'+tag,
                         metaloader(classpath)) 
开发者ID:blackye,项目名称:luscan-devel,代码行数:6,代码来源:yamltags.py

示例12: register

# 需要导入模块: import yaml [as 别名]
# 或者: from yaml import add_constructor [as 别名]
def register(cls):
        yaml.add_constructor(cls.tag, cls.constructor)
        yaml.add_representer(cls, cls.representer) 
开发者ID:deep-fry,项目名称:mayo,代码行数:5,代码来源:parse.py

示例13: constructor

# 需要导入模块: import yaml [as 别名]
# 或者: from yaml import add_constructor [as 别名]
def constructor(tag=None, pattern=None):
    """Register custom constructor with pyyaml."""
    def decorator(f):
        if tag is None or f is tag:
            tag_ = '!{}'.format(f.__name__)
        else:
            tag_ = tag
        yaml.add_constructor(tag_, f)
        if pattern is not None:
            yaml.add_implicit_resolver(tag_, re.compile(pattern))
        return f
    if callable(tag):  # little convenience hack to avoid empty arg list
        return decorator(tag)
    return decorator 
开发者ID:hchasestevens,项目名称:bellybutton,代码行数:16,代码来源:parsing.py

示例14: setup_yaml

# 需要导入模块: import yaml [as 别名]
# 或者: from yaml import add_constructor [as 别名]
def setup_yaml():
    _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.SafeDumper.add_representer(collections.OrderedDict, dict_representer)
    yaml.add_constructor(_mapping_tag, dict_constructor) 
开发者ID:airbnb,项目名称:knowledge-repo,代码行数:13,代码来源:post.py

示例15: load_ordered_config

# 需要导入模块: import yaml [as 别名]
# 或者: from yaml import add_constructor [as 别名]
def load_ordered_config(config_path):
    """
      Loads the configuration in the same order as it's defined in yaml file,
      so that, while saving it in new format, order is maintained
      Args:
            config_path (str): Path to the configuration file
      Returns:
            config(dict): Returns the configurations in the defined ordered
    """

    #  To load data from yaml in ordered dict format
    _mapping_tag = yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG

    def dict_representer(dumper, data):
        return dumper.represent_mapping(_mapping_tag, iteritems(data))

    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)

    #  format the output to print a blank scalar rather than null
    def represent_none(self, _):
        return self.represent_scalar('tag:yaml.org,2002:null', u'')

    yaml.add_representer(type(None), represent_none)

    # read input from home directory for pull requests
    with open(config_path, 'r') as f:
        config = yaml.safe_load(f)
    return config 
开发者ID:redhat-aqe,项目名称:review-rot,代码行数:34,代码来源:__init__.py


注:本文中的yaml.add_constructor方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。