YAML Input & Output

API

Utilities for doing IO to YAML files.

urbansim.utils.yamlio.convert_to_yaml(cfg, str_or_buffer)[source]

Convert a dictionary to YAML and return the string or write it out depending on the type of str_or_buffer.

Parameters
cfgdict or OrderedDict

Dictionary or OrderedDict to convert.

str_or_bufferNone, str, or buffer

If None: the YAML string will be returned. If string: YAML will be saved to a file. If buffer: YAML will be written to buffer using the .write method.

Returns
str or None

YAML string if str_or_buffer is None, otherwise None since YAML is written out to a separate destination.

urbansim.utils.yamlio.frame_to_yaml_safe(frame, ordered=False)[source]

Convert a pandas DataFrame to a dictionary that will survive YAML serialization and re-conversion back to a DataFrame.

Parameters
framepandas.DataFrame
ordered: bool, optional, default False

If True, an OrderedDict is returned.

Returns
safedict or OrderedDict
urbansim.utils.yamlio.ordered_yaml(cfg, order=None)[source]

Convert a dictionary to a YAML string with preferential ordering for some keys. Converted string is meant to be fairly human readable.

Parameters
cfgdict

Dictionary to convert to a YAML string.

order: list

If provided, overrides the default key ordering.

Returns
str

Nicely formatted YAML string.

urbansim.utils.yamlio.series_to_yaml_safe(series, ordered=False)[source]

Convert a pandas Series to a dict that will survive YAML serialization and re-conversion back to a Series.

Parameters
seriespandas.Series
ordered: bool, optional, default False

If True, an OrderedDict is returned.

Returns
safedict or OrderedDict
urbansim.utils.yamlio.to_scalar_safe(obj)[source]

Convert a numpy data type to a standard python scalar.

urbansim.utils.yamlio.yaml_to_dict(yaml_str=None, str_or_buffer=None, ordered=False)[source]

Load YAML from a string, file, or buffer (an object with a .read method). Parameters are mutually exclusive.

Parameters
yaml_strstr, optional

A string of YAML.

str_or_bufferstr or file like, optional

File name or buffer from which to load YAML.

ordered: bool, optional, default False

If True, an OrderedDict is returned.

Returns
dict

Conversion from YAML.