support for more types

- allow to directly use Config and its sublasses (cast to dict)
- convert numpy arrays
This commit is contained in:
Martin Diehl 2020-10-26 21:38:24 +01:00
parent 93a791abd1
commit 5834d95012
1 changed files with 14 additions and 1 deletions

View File

@ -1,6 +1,7 @@
from io import StringIO
import abc
import numpy as np
import yaml
class NiceDumper(yaml.SafeDumper):
@ -15,6 +16,11 @@ class NiceDumper(yaml.SafeDumper):
def increase_indent(self, flow=False, indentless=False):
return super().increase_indent(flow, False)
def represent_data(self, data):
"""Cast Config objects and its qsubclasses to dict."""
return self.represent_data(dict(data)) if isinstance(data, dict) and type(data) != dict else \
super().represent_data(data)
class Config(dict):
"""YAML-based configuration."""
@ -64,7 +70,14 @@ class Config(dict):
kwargs['width'] = 256
if 'default_flow_style' not in kwargs:
kwargs['default_flow_style'] = None
fhandle.write(yaml.dump(dict(self),Dumper=NiceDumper,**kwargs))
def array_representer(dumper, data):
"""Convert numpy array to list of native types."""
return dumper.represent_list([d.item() for d in data])
NiceDumper.add_representer(np.ndarray, array_representer)
fhandle.write(yaml.dump(self,Dumper=NiceDumper,**kwargs))
@property