"""
import os.path as _os_path
-import types as _types
+import sys as _sys
import h5py as _h5py
import numpy as _numpy
from .. import LOG as _LOG
from .. import config as _config
from . import FileStorage as _FileStorage
+from . import is_string as _is_string
def pprint_HDF5(*args, **kwargs):
lines = []
lines.append(' '*depth + cwg.name)
depth += 1
- for key,value in cwg.iteritems():
+ for key,value in cwg.items():
if isinstance(value, _h5py.Group):
lines.extend(_pformat_hdf5(value, depth))
elif isinstance(value, _h5py.Dataset):
v = list(v)
except TypeError:
v = []
- else:
- v = str(v) # array('abc', dtype='|S3') -> 'abc'
+ if _sys.version_info >= (3,):
+ for i,v_ in enumerate(v):
+ if isinstance(v_, bytes):
+ v[i] = str(v_, 'utf-8')
+ else: # array('abc', dtype='|S3') -> 'abc'
+ if _sys.version_info >= (3,):
+ v = str(v, 'utf-8')
+ else:
+ v = str(v)
elif isinstance(s, _config.IntegerSetting):
v = int(v) # array(3, dtpe='int32') -> 3
elif isinstance(s, _config.FloatSetting):
raise NotImplementedError(type(s))
elif isinstance(s, _config.ListSetting):
v = list(v) # convert from numpy array
- if isinstance(v, _types.StringTypes):
+ if _is_string(v):
# convert back from None, etc.
v = s.convert_from_text(v)
config[s.name] = v
continue
if value is None: # not set yet, or invalid
value = s.convert_to_text(config[s.name])
+ if _sys.version_info >= (3,): # convert strings to bytes/
+ if isinstance(value, str):
+ value = value.encode('utf-8')
+ elif isinstance(value, list):
+ value = list(value) # shallow copy
+ for i,v in enumerate(value):
+ if isinstance(v, str):
+ value[i] = v.encode('utf-8')
try:
del group[s.name]
except KeyError:
pass
- group[s.name] = value
+ try:
+ group[s.name] = value
+ except TypeError:
+ raise ValueError((value, type(value)))
finally:
if f:
f.close()
from __future__ import absolute_import
import os.path as _os_path
-import types as _types
import yaml as _yaml # global PyYAML module
from .. import LOG as _LOG
from .. import config as _config
from . import FileStorage as _FileStorage
+from . import is_string as _is_string
class _YAMLDumper (_yaml.SafeDumper):
def represent_bool(self, data):
"Use yes/no instead of the default true/false"
if data:
- value = u'yes'
+ value = 'yes'
else:
- value = u'no'
- return self.represent_scalar(u'tag:yaml.org,2002:bool', value)
+ value = 'no'
+ return self.represent_scalar('tag:yaml.org,2002:bool', value)
_YAMLDumper.add_representer(bool, _YAMLDumper.represent_bool)
@staticmethod
def _from_dict(config, data):
settings = dict([(s.name, s) for s in config.settings])
- for key,value in data.iteritems():
+ for key,value in data.items():
setting = settings[key]
if isinstance(setting, (_config.BooleanSetting,
_config.NumericSetting,
_config.ListSetting,
_config.IntegerListSetting,
_config.FloatListSetting)):
- if isinstance(value, _types.StringTypes):
+ if _is_string(value):
# older versions of h5config
value = s.convert_from_text(value)
v = value
def _to_dict(config):
data = {}
settings = dict([(s.name, s) for s in config.settings])
- for key,value in config.iteritems():
+ for key,value in config.items():
if key in settings:
setting = settings[key]
if isinstance(setting, (_config.BooleanSetting,