Python yaml.add_representer() Examples

The following are 30 code examples of yaml.add_representer(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module yaml , or try the search function .
Example #1
Source File: output_formatter.py    From dsub with Apache License 2.0 6 votes vote down vote up
def __init__(self, full):
    super(YamlOutput, self).__init__(full)

    yaml.add_representer(six.text_type, self.string_presenter)
    yaml.add_representer(str, self.string_presenter)
    yaml.add_representer(collections.OrderedDict, self.dict_representer) 
Example #2
Source File: common.py    From ARC with MIT License 6 votes vote down vote up
def save_yaml_file(path: str,
                   content: list or dict,
                   ) -> None:
    """
    Save a YAML file (usually an input / restart file, but also conformers file)

    Args:
        path (str): The YAML file path to save.
        content (list, dict): The content to save.
    """
    if not isinstance(path, str):
        raise InputError(f'path must be a string, got {path} which is a {type(path)}')
    yaml.add_representer(str, string_representer)
    logger.debug('Creating a restart file...')
    content = yaml.dump(data=content)
    if '/' in path and os.path.dirname(path) and not os.path.exists(os.path.dirname(path)):
        os.makedirs(os.path.dirname(path))
    with open(path, 'w') as f:
        f.write(content) 
Example #3
Source File: testtools.py    From flocker with Apache License 2.0 6 votes vote down vote up
def extract_substructure_for_test(test_case, substructure, config):
    """
    Extract the keys from the config in substructure, which may be a nested
    dictionary.

    Raises a ``unittest.SkipTest`` if the substructure is not found in the
    configuration.

    This can be used to load credentials all at once for testing purposes.
    """
    try:
        return extract_substructure(config, substructure)
    except MissingConfigError as e:
        yaml.add_representer(
            Optional,
            lambda d, x: d.represent_scalar(u'tag:yaml.org,2002:str', repr(x)))
        test_case.skip(
            'Skipping test: could not get configuration: {}\n\n'
            'In order to run this test, add ensure file at $ACCEPTANCE_YAML '
            'has structure like:\n\n{}'.format(
                e.message,
                yaml.dump(substructure, default_flow_style=False))
        ) 
Example #4
Source File: __init__.py    From DynaPhoPy with MIT License 6 votes vote down vote up
def save_quasiparticle_data_to_file(quasiparticle_data, filename):

    import yaml

    def float_representer(dumper, value):
        text = '{0:.8f}'.format(value)
        return dumper.represent_scalar(u'tag:yaml.org,2002:float', text)

    yaml.add_representer(float, float_representer)

    output_dict = []
    for i, q_point in enumerate(quasiparticle_data['q_points']):
        q_point_dict = {'reduced_wave_vector': q_point.tolist()}
        q_point_dict.update({'frequencies': quasiparticle_data['frequencies'][i].tolist()})
        q_point_dict.update({'linewidths': quasiparticle_data['linewidths'][i].tolist()})
        q_point_dict.update({'frequency_shifts': quasiparticle_data['frequency_shifts'][i].tolist()})
        # output_dict.update({'q_point_{}'.format(i): q_point_dict})
        output_dict.append(q_point_dict)

    with open(filename, 'w') as outfile:
        yaml.dump(output_dict, outfile, default_flow_style=False) 
Example #5
Source File: base.py    From bridgy with MIT License 5 votes vote down vote up
def read(self):
        # ensure yaml uses a defaultdict(str)
        yaml.add_representer(collections.defaultdict,
                             Representer.represent_str)
        try:
            with open(os.path.expanduser(self.path), 'r') as fh:
                self.conf = yaml.load(fh)
        except Exception as ex:
            logger.error("Unable to read config (%s): %s" % (self.path, ex))
            sys.exit(1) 
Example #6
Source File: __init__.py    From bridgy with MIT License 5 votes vote down vote up
def _readConfig():
    # ensure yaml uses a defaultdict(str)
    yaml.add_representer(collections.defaultdict,
                         Representer.represent_str)
    try:
        with open(os.path.expanduser(ConfigBase.path), 'r') as fh:
            config = yaml.load(fh)
    except Exception as ex:
        logger.error("Unable to read config (%s): %s" % (ConfigBase.path, ex))
        sys.exit(1)

    return config 
Example #7
Source File: parse.py    From mayo with MIT License 5 votes vote down vote up
def register(cls):
        yaml.add_constructor(cls.tag, cls.constructor)
        yaml.add_representer(cls, cls.representer) 
Example #8
Source File: fwtool.py    From fwtool.py with MIT License 5 votes vote down vote up
def writeYaml(yamlData, file):
 yaml.add_representer(tuple, lambda dumper, data: dumper.represent_list(data))
 yaml.add_representer(dict, lambda dumper, data: dumper.represent_mapping(dumper.DEFAULT_MAPPING_TAG, data, flow_style=False))
 representInt = lambda dumper, data: dumper.represent_int('0x%X' % data if data >= 10 else data)
 yaml.add_representer(int, representInt)
 try:
  yaml.add_representer(long, representInt)
  yaml.add_representer(unicode, lambda dumper, data: dumper.represent_str(str(data)))
 except NameError:
  # Python 3
  pass
 yaml.dump(yamlData, file) 
Example #9
Source File: helpers.py    From poseidon with Apache License 2.0 5 votes vote down vote up
def yaml_out(config_file, obj_doc):
    stream = tempfile.NamedTemporaryFile(
        prefix=os.path.basename(config_file),
        dir=os.path.dirname(config_file),
        mode='w',
        delete=False)
    yaml.add_representer(type(None), represent_none)
    yaml.dump(obj_doc, stream, default_flow_style=False)
    stream.close()
    os.replace(stream.name, config_file)
    return True 
Example #10
Source File: __init__.py    From review-rot with GNU General Public License v3.0 5 votes vote down vote up
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 
Example #11
Source File: hot_resource.py    From heat-translator with Apache License 2.0 5 votes vote down vote up
def _handle_nested_template(self, scale_res, yaml_name,
                                hot_template_parameters,
                                parameters=None):
        template_dict = OrderedDict()
        template_dict['heat_template_version'] = HEAT_TEMPLATE_VERSION
        template_dict['description'] = HEAT_DESCRIPTION

        if parameters is not None:
            template_dict['parameters'] = parameters

        if hot_template_parameters:
            all_params = OrderedDict()
            for parameter in hot_template_parameters:
                all_params.update(parameter.get_dict_output())
            template_dict.update({'parameters': all_params})

        template_dict["resources"] = {}
        dict_res = OrderedDict()
        for res in scale_res:
            dict_res = res.get_dict_output()
            res_name = list(dict_res.keys())[0]
            template_dict["resources"][res_name] = \
                dict_res[res_name]

        yaml.add_representer(OrderedDict, self.represent_ordereddict)
        yaml.add_representer(dict, self.represent_ordereddict)
        yaml_string = yaml.dump(template_dict, default_flow_style=False)
        yaml_string = yaml_string.replace('\'', '').replace('\n\n', '\n')
        nested_template = {
            yaml_name: yaml_string
        }
        return nested_template 
Example #12
Source File: params_dict.py    From Live-feed-object-device-identification-using-Tensorflow-and-OpenCV with Apache License 2.0 5 votes vote down vote up
def save_params_dict_to_yaml(params, file_path):
  """Saves the input ParamsDict to a YAML file."""
  with tf.io.gfile.GFile(file_path, 'w') as f:

    def _my_list_rep(dumper, data):
      # u'tag:yaml.org,2002:seq' is the YAML internal tag for sequence.
      return dumper.represent_sequence(
          u'tag:yaml.org,2002:seq', data, flow_style=True)
    yaml.add_representer(list, _my_list_rep)
    yaml.dump(params.as_dict(), f, default_flow_style=False) 
Example #13
Source File: __init__.py    From DynaPhoPy with MIT License 5 votes vote down vote up
def save_mesh_data_to_yaml_file(mesh_data, filename):

    import yaml

    def float_representer(dumper, value):
        text = '{0:.8f}'.format(value)
        return dumper.represent_scalar(u'tag:yaml.org,2002:float', text)

    yaml.add_representer(float, float_representer)

    qpoints, multiplicity, frequencies, linewidths = mesh_data

    output_dict = []
    for i, qp in enumerate(qpoints):
        mesh_dict = {}
        mesh_dict['reduced_wave_vector'] = qp.tolist()
        mesh_dict['frequencies'] = frequencies[i].tolist()
        mesh_dict['linewidths'] = linewidths[i].tolist()
        mesh_dict['multiplicity'] = int(multiplicity[i])

        output_dict.append(mesh_dict)

    with open(filename, 'w') as outfile:
        yaml.dump(output_dict, outfile, default_flow_style=False) 
Example #14
Source File: __init__.py    From DynaPhoPy with MIT License 5 votes vote down vote up
def save_bands_data_to_file(bands_data, filename):
    import yaml

    def float_representer(dumper, value):
        text = '{0:.8f}'.format(value)
        return dumper.represent_scalar(u'tag:yaml.org,2002:float', text)

    yaml.add_representer(float, float_representer)

    with open(filename, 'w') as outfile:
        yaml.dump(bands_data, outfile, default_flow_style=False) 
Example #15
Source File: utils.py    From excelcy with MIT License 5 votes vote down vote up
def yaml_save(data, file_path: str):
    import yaml

    # link: https://stackoverflow.com/a/16782282/469954
    def yaml_represent_odict(dumper, data):
        value = []
        for item_key, item_value in data.items():
            node_key = dumper.represent_data(item_key)
            node_value = dumper.represent_data(item_value)
            value.append((node_key, node_value))
        return yaml.nodes.MappingNode(u'tag:yaml.org,2002:map', value)

    yaml.add_representer(odict, yaml_represent_odict)
    yaml.dump(data, open(file_path, 'w')) 
Example #16
Source File: monkey.py    From maas with GNU Affero General Public License v3.0 5 votes vote down vote up
def fix_ordereddict_yaml_representer():
    """Fix PyYAML so an OrderedDict can be dumped."""

    def dumper(dumper, data):
        return dumper.represent_mapping("tag:yaml.org,2002:map", data.items())

    yaml.add_representer(OrderedDict, dumper, Dumper=yaml.Dumper)
    yaml.add_representer(OrderedDict, dumper, Dumper=yaml.SafeDumper) 
Example #17
Source File: params_dict.py    From models with Apache License 2.0 5 votes vote down vote up
def save_params_dict_to_yaml(params, file_path):
  """Saves the input ParamsDict to a YAML file."""
  with tf.io.gfile.GFile(file_path, 'w') as f:
    def _my_list_rep(dumper, data):
      # u'tag:yaml.org,2002:seq' is the YAML internal tag for sequence.
      return dumper.represent_sequence(
          u'tag:yaml.org,2002:seq', data, flow_style=True)
    yaml.add_representer(list, _my_list_rep)
    yaml.dump(params.as_dict(), f, default_flow_style=False) 
Example #18
Source File: run.py    From stethoscope with Apache License 2.0 5 votes vote down vote up
def main():
  parser = argparse.ArgumentParser(
    description="""Pull records for a batch of users and submit to external services."""
  )
  parser.add_argument('--timeout', dest="timeout", type=int, default=10)
  parser.add_argument('--limit', dest="limit", type=int, default=10,
      help="""Retrieve data for at most this many users concurrently.""")

  parser.add_argument('--log-file', dest='logfile', default='batch.log')

  parser.add_argument('input', nargs='?', type=argparse.FileType('r'), default=None)

  parser.add_argument('--collect-only', dest="collect_only", action="store_true")
  parser.add_argument('--debug', dest="debug", action="store_true", default=False)

  config = stethoscope.api.factory.get_config()
  args = parser.parse_args()

  for plugin in ['BITFIT', 'JAMF']:
    config[plugin + '_TIMEOUT'] = args.timeout

  config['LOGBOOK'] = stethoscope.utils.setup_logbook(args.logfile)
  config['LOGBOOK'].push_application()

  config['DEBUG'] = args.debug
  config['TESTING'] = args.debug

  yaml.add_representer(arrow.arrow.Arrow, arrow_representer)
  yaml.SafeDumper.add_representer(arrow.arrow.Arrow, arrow_representer)

  task.react(_main, (args, config)) 
Example #19
Source File: run.py    From stethoscope with Apache License 2.0 5 votes vote down vote up
def arrow_representer(dumper, data):
  """Represent an `arrow.arrow.Arrow` object as a scalar in ISO format.

  >>> yaml.add_representer(arrow.arrow.Arrow, arrow_representer)
  >>> yaml.dump(arrow.get(1367900664))
  "! '2013-05-07T04:24:24+00:00'\\n"

  """
  return dumper.represent_scalar(u'!', six.text_type(data.isoformat(b'T' if six.PY2 else 'T'))) 
Example #20
Source File: ansible_inventory.py    From virlutils with MIT License 5 votes vote down vote up
def setup_yaml():
    """ https://stackoverflow.com/a/8661021 """
    represent_dict_order = lambda self, data:  self.represent_mapping('tag:yaml.org,2002:map', data.items()) # noqa
    yaml.add_representer(OrderedDict, represent_dict_order) 
Example #21
Source File: pyats_testbed.py    From virlutils with MIT License 5 votes vote down vote up
def setup_yaml():
    """ https://stackoverflow.com/a/8661021 """
    represent_dict_order = lambda self, data: self.represent_mapping('tag:yaml.org,2002:map', data.items()) # noqa
    yaml.add_representer(OrderedDict, represent_dict_order) 
Example #22
Source File: dump.py    From foreman-yml with GNU General Public License v3.0 5 votes vote down vote up
def dump(self):
        dumpdata = {}
        dumpdata['hosts']       = self.dump_hosts()
        dumpdata['hostgroup']   = self.dump_hostgroups()
        dumpdata['architecture'] = self.dump_arch()
        dumpdata['environment']  = self.dump_env()
        dumpdata['os']           = self.dump_os()
        dumpdata['model']        = self.dump_model()
        dumpdata['media']        = self.dump_media()
        dumpdata['domain']       = self.dump_domain()
        dumpdata['settings']     = self.dump_settings()
        dumpdata['subnet']       = self.dump_subnet()
        dumpdata['smart-proxy']  = self.dump_smartproxy()
        dumpdata['partition-table']  = self.dump_ptable()
        dumpdata['provisioning-template']  = self.dump_provisioningtpl()
        dumpdata['users']        = self.dump_users()
        dumpdata['users']        = self.dump_users()
        dumpdata['auth-source-ldap'] = self.dump_ldaps()
        dumpdata['usergroups'] = self.dump_usergroups()
        dumpdata['roles'] = self.dump_roles()

        # print the result
        fmyml = { 'foreman': dumpdata }

        def str_presenter(dumper, data):
            try:
                dlen = len(data.splitlines())
            except TypeError:
                return dumper.represent_scalar('tag:yaml.org,2002:str', data)
            if (dlen > 1):
                return dumper.represent_scalar('tag:yaml.org,2002:str', data, style='|')

            return dumper.represent_scalar('tag:yaml.org,2002:str', data)

        yaml.add_representer(unicode, str_presenter)
        yaml.add_representer(str, str_presenter)

        yml = yaml.dump(fmyml, allow_unicode=True, default_flow_style=False )
        print( (yml) ) 
Example #23
Source File: util.py    From rdm with MIT License 5 votes vote down vote up
def write_yaml(data, yml_file):
    Dumper = yaml.SafeDumper
    Dumper.ignore_aliases = lambda self, data: True
    yaml.add_representer(OrderedDict, represent_ordereddict, Dumper=Dumper)
    return yaml.dump(data, yml_file, default_flow_style=False, Dumper=Dumper) 
Example #24
Source File: kube-secret-editor.py    From kube-secret-editor with MIT License 5 votes vote down vote up
def main():
    NoDatesSafeLoader.remove_implicit_resolver('tag:yaml.org,2002:timestamp')
    yaml.SafeDumper.orig_represent_str = yaml.SafeDumper.represent_str
    yaml.add_representer(str, repr_str, Dumper=yaml.SafeDumper)
    fname = sys.argv[1]
    edit(fname) 
Example #25
Source File: prettystate.py    From nmstate with GNU Lesser General Public License v2.1 5 votes vote down vote up
def __init__(self, state):
        yaml.add_representer(dict, represent_dict)
        self.state = _sort_with_priority(state) 
Example #26
Source File: params_dict.py    From tpu_models with Apache License 2.0 5 votes vote down vote up
def save_params_dict_to_yaml(params, file_path):
  """Saves the input ParamsDict to a YAML file."""
  with tf.gfile.Open(file_path, 'w') as f:
    def _my_list_rep(dumper, data):
      # u'tag:yaml.org,2002:seq' is the YAML internal tag for sequence.
      return dumper.represent_sequence(
          u'tag:yaml.org,2002:seq', data, flow_style=True)
    yaml.add_representer(list, _my_list_rep)
    yaml.dump(params.as_dict(), f, default_flow_style=False) 
Example #27
Source File: params_dict.py    From tpu_models with Apache License 2.0 5 votes vote down vote up
def save_params_dict_to_yaml(params, file_path):
  """Saves the input ParamsDict to a YAML file."""
  with tf.gfile.Open(file_path, 'w') as f:
    def _my_list_rep(dumper, data):
      # u'tag:yaml.org,2002:seq' is the YAML internal tag for sequence.
      return dumper.represent_sequence(
          u'tag:yaml.org,2002:seq', data, flow_style=True)
    yaml.add_representer(list, _my_list_rep)
    yaml.dump(params.as_dict(), f, default_flow_style=False) 
Example #28
Source File: utfyaml.py    From pykit with MIT License 5 votes vote down vote up
def dump(s, encoding='utf-8', save_unicode=False):

    yaml.add_representer(str, represent_str)
    yaml.add_representer(dict, represent_mapping)

    # dump unicode as a str to remove '!!python/unicode' tag
    if not save_unicode:
        tag = u'tag:yaml.org,2002:str'
    else:
        tag = u'tag:yaml.org,2002:python/unicode'

    yaml.add_representer(unicode, lambda dumper,
                         data: dumper.represent_scalar(tag, data))

    if encoding is not None:
        dumped = yaml.dump(s, allow_unicode=True, encoding=encoding)

    else:
        dumped = yaml.dump(s, allow_unicode=True, encoding='utf-8')
        # unify encoding
        dumped = unicode(dumped, 'utf-8')

    # '...' is not necessary for yaml
    pattern = '\.\.\.\n$'

    return re.sub(pattern, '', dumped, 1) 
Example #29
Source File: util.py    From forge with Apache License 2.0 5 votes vote down vote up
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) 
Example #30
Source File: mdconf.py    From ARC with MIT License 5 votes vote down vote up
def main():
    """
    The main function for the Conformational FF optimization.
    Note: it is crucial that the attom mapping is conserved between the representation in the Gaussian file
    and the YAML coordinates file.

    Command line argument:
        '-f': The ESS output file (default: gaussian.out).
        '-s': The FF box size in Angstroms (default: 10). Thumb-rule: 4 * radius (or 2 * diameter).
        '-m': The custom Molecular Dynamics parameter .mdp filename (default: mdp.mdp).

    Returns:
        list: Entries are lists of coordinates (in array form) and energies (in kJ/mol).
    """
    # Parse the command-line arguments (requires the argparse module)
    args = parse_command_line_arguments()
    t0 = time.time()
    path = args.file[0]
    size = args.size[0]
    mdp_filename = args.mdp[0]
    with open('coords.yml', 'r') as f:
        coords = yaml.load(stream=f, Loader=yaml.FullLoader)

    output = list()
    for i, coord in enumerate(coords):
        opt_xyz, e = amber_to_gromacs(g_path=path, coord=coord, size=size, mdp_filename=mdp_filename)
        output.append([opt_xyz, e])
        if i % 10 == 0:
            purge()
    purge()

    # save YAML output
    yaml.add_representer(str, string_representer)
    content = yaml.dump(data=output, encoding='utf-8')
    with open('output.yml', 'w') as f:
        f.write(content)
    dt = time.time() - t0
    print(dt)