11import os
22import ruamel .yaml
3+ from ruamel .yaml import YAML
34import numpy as np
45from . import settings
56from . import citations
@@ -1041,12 +1042,10 @@ def update_from_file(self, param_file, extra_param_file=None):
10411042 loaded_dict = {}
10421043 if param_file is not None :
10431044 with open (param_file , encoding = "utf-8" ) as fl :
1044- loaded_dict = ruamel .yaml .load (fl ,
1045- ruamel .yaml .RoundTripLoader )
1045+ loaded_dict = YAML (typ = "rt" ).load (fl )
10461046 if extra_param_file is not None :
10471047 with open (extra_param_file , encoding = "utf-8" ) as fl :
1048- extra_dict = ruamel .yaml .load (fl ,
1049- ruamel .yaml .RoundTripLoader )
1048+ extra_dict = YAML (typ = "rt" ).load (fl )
10501049 loaded_dict = {** loaded_dict , ** extra_dict }
10511050
10521051 for key in loaded_dict :
@@ -1108,8 +1107,7 @@ def to_files(self, params_file, extra_params_file):
11081107 [PARAM_TEMPLATE , EXTRA_PARAM_TEMPLATE ]):
11091108 with open (template , encoding = "utf-8" ) as fl :
11101109 readed_template = fl .read ()
1111- loaded_template = ruamel .yaml .load (readed_template ,
1112- ruamel .yaml .RoundTripLoader )
1110+ loaded_template = YAML (typ = "rt" ).load (readed_template )
11131111 for key_name in loaded_template :
11141112 attr_name = key_name .lower ()
11151113 attr_name = attr_name .replace ("' " , '_' )
@@ -1146,14 +1144,14 @@ def my_represent_bool(self, data):
11461144 else :
11471145 value = u'False'
11481146 return self .represent_scalar (u'tag:yaml.org,2002:bool' , value )
1149- ruamel .yaml .RoundTripRepresenter .add_representer (type (None ),
1150- my_represent_none )
1151- ruamel .yaml .RoundTripRepresenter .add_representer (bool ,
1152- my_represent_bool )
1147+ yaml_rt = YAML (typ = "rt" )
1148+ yaml_rt .default_flow_style = True
1149+ yaml_rt .width = 4096 # do not wrap long values (e.g. paths)
1150+ yaml_rt .representer .add_representer (type (None ),
1151+ my_represent_none )
1152+ yaml_rt .representer .add_representer (bool , my_represent_bool )
11531153 with open (filename , 'w' , encoding = "utf-8" ) as fl :
1154- ruamel .yaml .dump (loaded_template , fl ,
1155- default_flow_style = True ,
1156- Dumper = ruamel .yaml .RoundTripDumper )
1154+ yaml_rt .dump (loaded_template , fl )
11571155 # save missed attributes at the end of extra file
11581156 final_dict = dict ()
11591157 with open (extra_params_file , 'a' , encoding = "utf-8" ) as fl :
@@ -1171,7 +1169,13 @@ def my_represent_bool(self, data):
11711169 if isinstance (value , np .ndarray ):
11721170 value = value .tolist ()
11731171 final_dict [attr_name ] = value
1174- fl .write (ruamel .yaml .dump (final_dict , default_flow_style = False ))
1172+ try :
1173+ # ruamel.yaml >= 0.18
1174+ yaml_py = YAML (typ = "full" , pure = True )
1175+ except NotImplementedError :
1176+ yaml_py = YAML (typ = "unsafe" , pure = True )
1177+ yaml_py .default_flow_style = False
1178+ yaml_py .dump (final_dict , fl )
11751179
11761180 def get_global_optimizer (self ):
11771181 """
0 commit comments