+# Copyright
+
+"""Add representers to YAML to support Hooke.
+
+Without introspection, YAML cannot decide how to save some
+objects. By refusing to save these objects, we obviously loose
+that information, so make sure the things you drop are either
+stored somewhere else or not important.
+
+>>> import yaml
+>>> a = numpy.array([1,2,3])
+>>> print yaml.dump(a)
+null
+...
+<BLANKLINE>
+
+The default behavior is to crash.
+
+>>> yaml.Dumper.yaml_representers.pop(numpy.ndarray) # doctest: +ELLIPSIS
+<function ndarray_representer at 0x...>
+>>> print yaml.dump(a)
+Traceback (most recent call last):
+ ...
+ if data in [None, ()]:
+TypeError: data type not understood
+
+Restore the representer for future tests.
+
+>>> yaml.add_representer(numpy.ndarray, ndarray_representer)
+"""
+
+from __future__ import absolute_import
+import sys
+
+import numpy
+import yaml #from yaml.representer import Representer
+
+from ..curve import Data
+
+
+if False: # YAML dump debugging code
+ """To help isolate data types etc. that give YAML problems.
+
+ This is usually caused by external C modules (e.g. numpy) that
+ define new types (e.g. numpy.dtype) which YAML cannot inspect.
+ """
+ def ignore_aliases(data):
+ print data, type(data)
+ sys.stdout.flush()
+ if data in [None, ()]:
+ return True
+ if isinstance(data, (str, unicode, bool, int, float)):
+ return True
+ yaml.representer.SafeRepresenter.ignore_aliases = staticmethod(
+ ignore_aliases)
+
+def ndarray_representer(dumper, data):
+ return dumper.represent_none(None)
+yaml.add_representer(numpy.ndarray, ndarray_representer)
+yaml.add_representer(Data, ndarray_representer)