Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Branch: master
Fetching contributors…

Cannot retrieve contributors at this time

38 lines (34 sloc) 1.62 KB
import yaml
from yaml import CLoader as Loader
def construct_mapping_kludge(loader, node):
""" This constructor painfully steps through the node and checks
that each key is hashable. Actually, what it does is checks
whether it knows how to *make* it hashable, and if so, does that.
If not it just lets it through and hopes for the best. But the
common problem cases are handled here. If you're constructing
objects directly from YAML, just make them immutable and hashable! """
def anything(node):
if isinstance(node, yaml.ScalarNode):
return loader.construct_scalar(node)
elif isinstance(node, yaml.SequenceNode):
return loader.construct_sequence(node)
elif isinstance(node, yaml.MappingNode):
return construct_mapping_kludge(loader, node)
def make_hashable(value):
""" Reconstructs a non-hashable value. """
if isinstance(value, list):
return tuple(map(make_hashable, value))
elif isinstance(value, set):
return frozenset(value)
elif isinstance(value, dict):
return frozenset((make_hashable(key), make_hashable(val))
for key, val in value.items())
return value
def new_items():
for k, v in node.value:
yield (make_hashable(anything(k)), anything(v))
return dict(new_items())
yaml.add_constructor(u',2002:map', construct_mapping_kludge, Loader=Loader)
tricky=u""" !!map { [ New York Yankees, Atlanta Braves ]: [ 2001-07-02, 2001-08-12, 2001-08-14 ] } """
print(repr(yaml.load(tricky, Loader=Loader)))
Jump to Line
Something went wrong with that request. Please try again.