support for more types
- allow to directly use Config and its sublasses (cast to dict) - convert numpy arrays
This commit is contained in:
parent
93a791abd1
commit
5834d95012
|
@ -1,6 +1,7 @@
|
||||||
from io import StringIO
|
from io import StringIO
|
||||||
import abc
|
import abc
|
||||||
|
|
||||||
|
import numpy as np
|
||||||
import yaml
|
import yaml
|
||||||
|
|
||||||
class NiceDumper(yaml.SafeDumper):
|
class NiceDumper(yaml.SafeDumper):
|
||||||
|
@ -15,6 +16,11 @@ class NiceDumper(yaml.SafeDumper):
|
||||||
def increase_indent(self, flow=False, indentless=False):
|
def increase_indent(self, flow=False, indentless=False):
|
||||||
return super().increase_indent(flow, 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):
|
class Config(dict):
|
||||||
"""YAML-based configuration."""
|
"""YAML-based configuration."""
|
||||||
|
@ -64,7 +70,14 @@ class Config(dict):
|
||||||
kwargs['width'] = 256
|
kwargs['width'] = 256
|
||||||
if 'default_flow_style' not in kwargs:
|
if 'default_flow_style' not in kwargs:
|
||||||
kwargs['default_flow_style'] = None
|
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
|
@property
|
||||||
|
|
Loading…
Reference in New Issue