Skip to content

Instantly share code, notes, and snippets.

Created February 25, 2011 19:54
Show Gist options
  • Save enaeseth/844388 to your computer and use it in GitHub Desktop.
Save enaeseth/844388 to your computer and use it in GitHub Desktop.
Load YAML mappings as ordered dictionaries
import yaml
import yaml.constructor
# included in standard lib from Python 2.7
from collections import OrderedDict
except ImportError:
# try importing the backported drop-in replacement
# it's available on PyPI
from ordereddict import OrderedDict
class OrderedDictYAMLLoader(yaml.Loader):
A YAML loader that loads mappings into ordered dictionaries.
def __init__(self, *args, **kwargs):
yaml.Loader.__init__(self, *args, **kwargs)
self.add_constructor(u',2002:map', type(self).construct_yaml_map)
self.add_constructor(u',2002:omap', type(self).construct_yaml_map)
def construct_yaml_map(self, node):
data = OrderedDict()
yield data
value = self.construct_mapping(node)
def construct_mapping(self, node, deep=False):
if isinstance(node, yaml.MappingNode):
raise yaml.constructor.ConstructorError(None, None,
'expected a mapping node, but found %s' %, node.start_mark)
mapping = OrderedDict()
for key_node, value_node in node.value:
key = self.construct_object(key_node, deep=deep)
except TypeError, exc:
raise yaml.constructor.ConstructorError('while constructing a mapping',
node.start_mark, 'found unacceptable key (%s)' % exc, key_node.start_mark)
value = self.construct_object(value_node, deep=deep)
mapping[key] = value
return mapping
if __name__ == '__main__':
import textwrap
sample = """
two: fish
red: fish
blue: fish
a: yes
b: no
c: null
data = yaml.load(textwrap.dedent(sample), OrderedDictYAMLLoader)
assert type(data) is OrderedDict
print data
Copy link

alborzs commented Jun 3, 2013

I have a big code base and so far I used vanilla yaml load like this yaml.load(s, Loader=yaml.BaseLoader)
I tried your OrderedDictYAMLLoader but I pretty much get nothing from load now.
what is the exact diffrences, have you tested this in a real application maybe with nested dictionaries of diffrent length?


Copy link

Is there a license for this code snippet? Can I use it?

Copy link

Licensing this with anything would sure help out the commercial Internet community.

Copy link

MIT license on oyaml here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment