當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。